We’ve covered so many topics in the previous series; from basic object manipulations to animations, events, filters, groups, and subclasses. But there’s still couple of very interesting and useful things to discuss!
Zoom and panning
Let’s see how we can implement a basic system of zoom and pan with the mouse interactions. We will use the mouse wheel to zoom up to 20X ( 2000% ) on a canvas and a alt + click action to drag around.
We start be hooking up the basic controls:
This is a basic zoom control, limited between 1% and 2000%. we want now to add dragging of the canvas. We will use ALT + DRAG, but you can change to another combination. The idea is that a mousedown with alt will set a boolean to true, so that a mouse move event can then understand that is time for dragging.
Ok, this is a basic setup that will allow you to control zoom and panning. There are still a couple of possible enhancement.
For example we can make the wheel-zoom to center the canvas around the point where the cursor is:
As a final touch we can limit the panning area to avoid view to go infinity in one direction. We stroke a rect of 1000x1000 pixels that will represent our panning area. And we add the code to limit the movements in that boundaries: