Skip to content

Instantly share code, notes, and snippets.

@bernatfortet
Created April 11, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bernatfortet/e474d171ba1dfef7035aa2ccaeb8b27f to your computer and use it in GitHub Desktop.
Save bernatfortet/e474d171ba1dfef7035aa2ccaeb8b27f to your computer and use it in GitHub Desktop.
Zoom Problem
// Zoom an increment towards the position of my mouse
export function zoomIn(mousePosition: Object) { return (dispatch, getState) => {
const currentZoom = getState().graphEditor.zoom
const nextZoom = currentZoom+ZOOM_INCREMENT < MAX_ZOOM ? currentZoom+ZOOM_INCREMENT : MAX_ZOOM
dispatch( storeZoom(nextZoom) )
const currentPan = selectors.getPan(getState())
const graphElement = document.getElementById("graph")
const graphOffset = {
x: graphElement.getBoundingClientRect().left,
y: graphElement.getBoundingClientRect().top,
}
// current pan + mouse position/zoom gives me the relative position of
const scaleChange = nextZoom - currentZoom
const nextPan = {
x: currentPan.x + (mousePosition.x/currentZoom) * scaleChange,
y: currentPan.y + (mousePosition.y/currentZoom) * scaleChange,
}
dispatch( storePan(nextPan) )
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment