javascript - Mouse Wheel Zooming -
so i'm working on canvas can drag image side side , use mouse wheel re size image zooming in , out on here code have far please feel free ask more details. i'm trying find way that's compact , require minimum code.
<!doctype html> <html> <head> <style> body { margin: 0px; padding: 0px; } </style> <script src="kinetic-v5.1.0.min.js" type="text/javascript"></script> </head> <body> <div id="container"></div> <script type="text/javascript"> function drawimage(imageobj) { var stage = new kinetic.stage({ container: "container", width: 1800, height: 800 }); var layer = new kinetic.layer(); // image var image1 = new kinetic.image({ image: imageobj, x: stage.getwidth() / 2 - 896 / 1, y: stage.getheight() / 2 - 255 / 2, width: 200, height: 157, draggable: true, dragboundfunc: function (pos) { if (pos.x < this.minx) { } this.minx = pos.x; return { x: pos.x, y: this.getabsoluteposition().y } } }); // add cursor styling image1.on('mouseover', function() { document.body.style.cursor = 'pointer'; }); image1.on('mouseout', function() { document.body.style.cursor = 'default'; }); layer.add(image1); stage.add(layer); } var imageobj = new image(); imageobj.onload = function() { drawimage(this); }; imageobj.src = 'image1.png'; </script> </body> </html>
one way use zoom helper. example:
var zoomhelper = { stage: null, scale: 1, zoomfactor: 1.1, origin: { x: 0, y: 0 }, zoom: function(event) { event.preventdefault(); var delta; if (navigator.useragent.tolowercase().indexof('firefox') > -1) { if (event.originalevent.detail > 0) { //scroll down delta = 0.2; } else { //scroll delta = 0; } } else { if (event.originalevent.wheeldelta < 0) { //scroll down delta = 0.2; } else { //scroll delta = 0; } } var evt = event.originalevent, mx = evt.clientx - zoomhelper.stage.getx(), = evt.clienty - zoomhelper.stage.gety(), zoom = (zoomhelper.zoomfactor - delta), newscale = zoomhelper.scale * zoom; zoomhelper.origin.x = mx / zoomhelper.scale + zoomhelper.origin .x - mx / newscale; zoomhelper.origin.y = / zoomhelper.scale + zoomhelper.origin .y - / newscale; zoomhelper.stage.setoffset({ x: zoomhelper.origin.x, y: zoomhelper.origin.y }); zoomhelper.stage.setscale({ x: newscale, y: newscale }); zoomhelper.stage.draw(); zoomhelper.scale *= zoom; } };
here demo code provided in example changed.
Comments
Post a Comment