Sign in to follow this  

Zooming in/out for objects in x:y positions

This topic is 820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So i have been a bit confused on the logic of how i would approach this.

I have a bunch of objects in a fixed x:y position on my canvas. And i want to zoom in and out with my mouse wheel. Then i was wondering how do I move the objects from their position to make seem like I am zooming in and out. Obviously they change in size, but they would also move position - it's the position on the output that has me a bit confused in terms of the logic.

 

I use JavaScript to program my application, but i'm just looking the logical approach to how i would do this. Most of my objects are in an array which contain and x:y "pixel position" not a grid position.

Any advice would be welcome :)

Share this post


Link to post
Share on other sites
Using a floating point value, make 1.0 be your "normal" scale, and when you want to position something onscreen, you multiply the "real" position by the scale to get the visual position.
float scale = 1.0;
scaledPosition = (scale * objectPosition); //i.e. scaledPosX = (scale * posX); scaledPosY = (scale * posY);
scaledSize = (scale * objectSize);
Then just adjust 'scale'. If you want to zoom in to double the normal, set 'scale' to 2.0.
If you want to zoom out, set 'scale' to 0.5, and etc...
 
Do the same to the widths/heights of the objects - use the same 'scale' variable.

Share this post


Link to post
Share on other sites
Assuming you are using a canvas, don't move the objects, just use transformations to zoom in on the canvas
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Transformations

function drawScene(context)
{
    context.save();
    context.scale(zoomLevel, zoomLevel);
    context.translate(-cameraOrigin.x, -cameraOrigin.y);

    // draw your objects here

    context.restore();
}
If you aren't using context directly, whatever graphics library you are using should have similar operations.

Share this post


Link to post
Share on other sites

Using a floating point value, make 1.0 be your "normal" scale, and when you want to position something onscreen, you multiply the "real" position by the scale to get the visual position.

float scale = 1.0;
scaledPosition = (scale * objectPosition); //i.e. scaledPosX = (scale * posX); scaledPosY = (scale * posY);
scaledSize = (scale * objectSize);
Then just adjust 'scale'. If you want to zoom in to double the normal, set 'scale' to 2.0.
If you want to zoom out, set 'scale' to 0.5, and etc...
 
Do the same to the widths/heights of the objects - use the same 'scale' variable.

 

Ah this is good advice thank you :)
 

 

Assuming you are using a canvas, don't move the objects, just use transformations to zoom in on the canvas
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Transformations
 

function drawScene(context)
{
    context.save();
    context.scale(zoomLevel, zoomLevel);
    context.translate(-cameraOrigin.x, -cameraOrigin.y);

    // draw your objects here

    context.restore();
}
If you aren't using context directly, whatever graphics library you are using should have similar operations.

 


Doesn't scale just alter size but not distance between objects? If for example something is at 5:10 and the other is 10:10 and you scale, would the distance between be the same but the objects be drawn larger ? 

Share this post


Link to post
Share on other sites

 

Doesn't scale just alter size but not distance between objects? If for example something is at 5:10 and the other is 10:10 and you scale, would the distance between be the same but the objects be drawn larger ? 

 

 

Nope, using scale will also increase the output distance between objects. You can think of it as zoom.

Share this post


Link to post
Share on other sites

 

 

Doesn't scale just alter size but not distance between objects? If for example something is at 5:10 and the other is 10:10 and you scale, would the distance between be the same but the objects be drawn larger ? 

 

 

Nope, using scale will also increase the output distance between objects. You can think of it as zoom.

 

Thank you, this was good to know and simple to implement :)

Share this post


Link to post
Share on other sites

This topic is 820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this