Instructions: Mousewheel over canvas.
Konva Zoom Image on Hover Demoview raw<!DOCTYPE html> <html> <head> <script src="https://unpkg.com/[email protected]/konva.min.js"></script> <meta charset="utf-8" /> <title>Konva Zoom Relative to Stage Demo</title> <style> body { margin: 0; padding: 0; overflow: hidden; background-color: #f0f0f0; } </style> </head>
<body> <div id="container"></div> <script> var width = window.innerWidth; var height = window.innerHeight;
var stage = new Konva.Stage({ container: 'container', width: width, height: height, });
var layer = new Konva.Layer(); stage.add(layer);
var circle = new Konva.Circle({ x: stage.width() / 2, y: stage.height() / 2, radius: 50, fill: 'green', }); layer.add(circle);
var scaleBy = 1.01; stage.on('wheel', (e) => { e.evt.preventDefault();
var oldScale = stage.scaleX(); var pointer = stage.getPointerPosition();
var mousePointTo = { x: (pointer.x - stage.x()) / oldScale, y: (pointer.y - stage.y()) / oldScale, };
let direction = e.evt.deltaY > 0 ? 1 : -1;
if (e.evt.ctrlKey) { direction = -direction; }
var newScale = direction > 0 ? oldScale * scaleBy : oldScale / scaleBy;
stage.scale({ x: newScale, y: newScale });
var newPos = { x: pointer.x - mousePointTo.x * newScale, y: pointer.y - mousePointTo.y * newScale, }; stage.position(newPos); }); </script> </body> </html>
|