Public Group

# Zooming in/out for objects in x:y positions

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

## 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 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 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);

context.restore();
}

If you aren't using context directly, whatever graphics library you are using should have similar operations.

##### 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);

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 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 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 :)

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 35
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631702
• Total Posts
3001812
×