Skip to content

Instantly share code, notes, and snippets.

@felipenmoura
Created December 15, 2012 14:25
Show Gist options
  • Save felipenmoura/4295546 to your computer and use it in GitHub Desktop.
Save felipenmoura/4295546 to your computer and use it in GitHub Desktop.
Could anyone help me with this algorithm?
/*
Goal:
The idea here is to apply a zoom effect to the current slide, centering the mouse position when the user uses the scroll.
Problem:
The first zoom works fine, but after the first zoom, the visual reference of the pointer is different from the real mouse position, what makes the user apply the zoom effect in different coordinates!
How it is done:
PowerPolygon offers a method called PPW.viewport() which receives an object to apply zoom centering the given x and y coordinates, described here: https://github.com/braziljs/power-polygon/wiki/API%20-%20Power%20Polygon
This method sets the transofmr-origin to the given coordinates and applies the given zoom as a transform:scale() on the css of the current slide.
When the user scrolls the mouse, it should call the private method _zoomBy, which is basically an alias to increment or decrement the current zoom using the viewport method.
What happens is that, after zooming the fisrt time, if the user moves the cursor to a different point, the cursor is now visually on a point, but its coordinates are in a different point!
The method which takes care of this is at:
https://github.com/braziljs/power-polygon/blob/master/ppw/ppw.js#L1105
*/
@jcemer
Copy link

jcemer commented Dec 18, 2012

var targetX = clickPositionInCanvasX || canvasWidth / 2
  , targetY = clickPositionInCanvasY || canvasHeight / 2
  , newScale = scale + step
  , ratio = newScale / scale

obj = {
    x: targetX - (targetX - obj.x) * ratio
  , y: targetY - (targetY - obj.y) * ratio
}

scale = newScale

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment