const image = {
object: new Image(),
x: 0,
y: 0,
scale: 1,
scaleMultiplier: 0.9
}
function draw() {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
ctx.save();
ctx.translate(image.x, image.y);
ctx.scale(image.scale, image.scale);
const imageWidth = image.object.width;
const imageHeight = image.object.height;
const centerCoordsX = (canvasWidth - imageWidth) / 2;
const centerCoordsY = (canvasHeight - imageHeight) / 2;
const t1 = (centerCoordsX + image.x) / (image.scale * 2);
const t2 = (centerCoordsY + image.y) / (image.scale * 2);
ctx.drawImage(image.object, t1, t2, imageWidth, imageHeight);
ctx.fill();
ctx.restore();
}
canvas.onwheel = function(e) {
if (e.deltaY < 0)
image.scale /= image.scaleMultiplier;
else
image.scale *= image.scaleMultiplier;
draw();
};
滚动鼠标滚轮时,图像现在缩放到左上角,但是如何使其缩放到光标坐标?
他们在这里回答了我的问题。我需要同样的东西,但只有一张照片。稍微简化了代码。