Why do you need javascript here?
If the origin point on the canvas is set at top-left, you can simply subtract Canvas coordinate from mouse position. For example to draw a 10px rectangle:
Canvas Fill Rectangle Left:(Mouse.x-Canvas.x), Top:(Mouse.y-Canvas.y), Right((Mouse.x-Canvas.x+10), Bottom:(Mouse.y-Canvas.y+10)
Make sure to set fixed canvas resolution mode.