 
 ms.screenPixelToScale = ({x, y}) ->
   if @svg.el.viewBox.animVal?
-    x = x / @magnification
-    y = y / @magnification
+    x = x / @scaleX
+    y = y / @scaleY
     viewBox = @svg.el.viewBox.animVal
     x += viewBox.x
     y += viewBox.y

  root = @root.el.getBoundingClientRect()
  @scaleX = root.width / subjectViewer.maxWidth
  @scaleY = root.height / subjectViewer.maxHeight
  @magnification = root.width / subjectViewer.maxWidth
  
  # recalculate the viewbox so that the aspect ratio matches the SVG element
  w = parseInt @svg.attr 'width'
  h = parseInt @svg.attr 'height'
  w = w / @magnification
  h = h / @magnification
  x = x + .5 * (width - w)
  y = y + .5 * (height - h)
  x = Math.max x, 0

    viewBox = @svg.el.viewBox.animVal
    x -= viewBox.x
    y -= viewBox.y
    x = x * @scaleX
    y = y * @scaleY
  {x, y}

# set the image scale if not already set  
ms.on 'marking-surface:add-tool', (tool) ->
  @rescale() if @scaleX is 0

LAST_TASK = false
INITIAL_STEPS = 2 # number of initial steps before annotating rectangles
