Zooming the Canvas Context to the Mouse Cursor
In a HTML5 canvas project involving image zooming using the scroll wheel, you may encounter the challenge of zooming toward the cursor, similar to the functionality in Google Maps. To achieve this effect, a series of calculations are necessary.
Calculating the Movements
Determining the Canvas Context Transformation:
<code class="js">ctx.translate(pt.x, pt.y); ctx.scale(factor, factor); ctx.translate(-pt.x, -pt.y);</code>
Transforming the Cursor Position:
Example Demonstration
Visit the following link for a working example that includes:
Demo: http://phrogz.net/tmp/canvas_zoom_to_cursor.html
Note: In Safari, the zooming behavior may differ from Chrome or Firefox.
The above is the detailed content of How do you Zoom a Canvas Context to the Mouse Cursor in HTML5?. For more information, please follow other related articles on the PHP Chinese website!