I recently shared how you can create a line with the JS Sketch API here
https://sketchplugins.com/d/1360-how-do-i-draw-ovals-circles-and-lines-with-the-js-api/
But I’ll answer your question directly here as well. To add an element relative to an artboard’s coordinates all you need to do is specify the parent like so.
let sketch = require('sketch')
let document = sketch.getSelectedDocument()
let page = document.selectedPage
let Artboard = sketch.Artboard
let Rectangle = sketch.Rectangle
let myArtboard = new Artboard({
name: 'myArtboard',
frame: new Rectangle(0,0,400,400),
parent: page
})
let ShapePath = sketch.ShapePath
let myRect = new ShapePath({
name: 'myRect',
frame: new Rectangle(10,10,60,60),
parent: myArtboard,
style: { fills: ['#000'] }
})
You can see that the artboard’s coordinates are at {x:0,y:0}
relative to the page and then the rectangle that I drew is at {x:10,y:10}
relative to the artboard.
Lastly, since you were wanting to draw a line you can do so with this:
const myLine = new ShapePath({
name: 'myLine',
frame: new Rectangle(0,0,40,100),
style: { borders: ['#FF0000']},
parent: myArtboard,
points: [
{ point: { x: 0, y: 0 }},
{ point: { x: 1, y: 1 }}
]
})