Last active
January 25, 2016 04:32
-
-
Save louisbros/b6e1060c4c93bcd36ea3 to your computer and use it in GitHub Desktop.
SVG Targeted Zooming - https://jsfiddle.net/louisbros/rosfdrj5/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <div> | |
// <svg viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg" version="1.2"> | |
// <circle cx="200" cy="200" r="100" fill="red" stroke="blue" stroke-width="10" /> | |
// </svg> | |
// </div> | |
// svg { | |
// outline: 1px solid red; | |
// width: 400px; | |
// height: 400px; | |
// } | |
const svg = document.querySelector('svg'); | |
function createPoint(x, y) { | |
return { | |
x: x, | |
y: y | |
}; | |
} | |
function calculateTarget(diff, centerPoint, mousePoint) { | |
return createPoint( | |
diff / 2 * (mousePoint.x / centerPoint.x) * -1, | |
diff / 2 * (mousePoint.y / centerPoint.y) * -1 | |
); | |
} | |
function calculateZoom(svg, diff, mousePoint) { | |
const svgRect = svg.getBoundingClientRect(), | |
centerPoint = createPoint(svgRect.width / 2, svgRect.height / 2), | |
viewRect = svg.viewBox.baseVal, | |
vw = viewRect.width, | |
vh = viewRect.height, | |
vx = viewRect.x, | |
vy = viewRect.y, | |
diffPoint = calculateTarget(diff, centerPoint, mousePoint); | |
return { | |
x: vx + diffPoint.x, | |
y: vy + diffPoint.y, | |
width: vw + diff, | |
height: vh + diff | |
}; | |
} | |
function zoom(svg, diff, mousePoint) { | |
const viewBoxZoom = calculateZoom(svg, diff, mousePoint); | |
svg.setAttribute('viewBox', `${viewBoxZoom.x} ${viewBoxZoom.y} ${viewBoxZoom.width} ${viewBoxZoom.height}`); | |
} | |
svg.addEventListener('mousewheel', (event) => { | |
event.preventDefault(); | |
zoom(svg, event.deltaY > 0 ? 10 : -10, createPoint(event.offsetX, event.offsetY)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment