1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!DOCTYPE html> <html> <body> <canvas id="myCanvas" width="500" height="500" style="border:1px solid #d3d3d3;"></canvas> <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var scale = 1; var scaleFactor = 0.1; var img = new Image(); img.src = "image/1.jpg"; img.onload = function() { ctx.drawImage(img, 0, 0, canvas.width, canvas.height); };
canvas.addEventListener('wheel', function(e) { e.preventDefault(); var mouseX = e.clientX - canvas.offsetLeft; var mouseY = e.clientY - canvas.offsetTop;
if (e.deltaY < 0) { scale *= 1 + scaleFactor; } else { scale *= 1 - scaleFactor; }
canvas.width = canvas.width; ctx.scale(scale, scale); ctx.drawImage(img, 0, 0, canvas.width, canvas.height); }); </script> </body> </html>
|