开发者

Implementing zoom on a fixed point, javascript/canvas [duplicate]

This question already has answers here: Closed 10 years a开发者_如何学Pythongo.

Possible Duplicate:

Zoom in on a point (using scale and translate)

I want to implement zooming on the mouse pointer with the mouse wheel. That is scaling the image while the point under the mouse pointer stays fixed.

Here is my code, which doesn't work very well

var scala = 1 + event.wheelDelta / 1000;
canvas.context.translate(-canvas.mouse.x * ( scala - 1 ) / canvas.scale,-canvas.mouse.y * ( scala - 1 ) / canvas.scale);
canvas.context.scale(scala,scala);
canvas.scale *= scala;
//canvas.scale is my variable that is initially set to 1.
//canvas.mouse is my variable that represents the mouse position relative to the canvas


Without looking at anything else, you'll need 2 translates: one before to move the mouse point to (0,0), and one after to move (0,0) (now with the zoomed picture) to where the mouse was.


Solved it and the answer is here: same question


My guess is that you need to do a canvas.context.restore() after each redraw, if you are saving the zoom level.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜