Jump to content

Zooming in on canvas algorithm only works with a factor of 2


Recommended Posts

I'm trying to create an HTML canvas where you can zoom in, but whilst zooming, the point that's being zoomed upon remains in the same spot on the viewport. The algorithm I wrote works if I set the factor to 2:

    var mousePos = mousePosInCanvas();
    var factor = 2;

    var xPan = (viewport.xPan / factor) - ((mousePos.x / factor));
    var yPan = (viewport.yPan / factor) - ((mousePos.y / factor));

    viewport.xPan = xPan;
    viewport.yPan = yPan;
    viewport.xScale = viewport.xScale * factor;
    viewport.yScale = viewport.yScale * factor;

But if I set

var factor = 1.1

the algorithm breaks. How can I make this work with a variable factor?

Code with factor 2: https://jsfiddle.net/x7s9zLys/

Code with factor 1.1: https://jsfiddle.net/x7s9zLys/1/

I've coded the scrollwheel to zoom in.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...