Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Canvas: Zooming

Is there any easy way how to zoom in and back out in canvas (JavaScript)? Basically I have a 400x400px canvas and I'd like to be able to zoom in with 'mousedown' (2x) and go back with 'mouseup'.

Spent last two days googling, but no luck so far. :(

like image 226
jack moore Avatar asked Aug 06 '10 04:08

jack moore


People also ask

How do I zoom out in HTML canvas?

Shift-click to zoom out. Mousewheel up/down over the canvas to zoom in to/out from that location.

How do you zoom in and out on canvas?

On a Touch Screen On your canvas, use your thumb and index finger to zoom. Move your thumb and index finger away from each other to zoom in, and pinch two fingers towards each other to zoom out.

Is HTML5 canvas still used?

The HTML5 canvas has the potential to become a staple of the web, enjoying ubiquitous browser and platform support in addition to widespread webpage support, as nearly 90% of websites have ported to HTML5.

What to do when canvas is big or zoomed in?

Sometimes this can be cleared easily by holding shift and pressing the refresh button in your browser.


1 Answers

Building on the suggestion of using drawImage you could also combine this with scale function.

So before you draw the image scale the context to the zoom level you want:

ctx.scale(2, 2) // Doubles size of anything draw to canvas. 

I've created a small example here http://jsfiddle.net/mBzVR/4/ that uses drawImage and scale to zoom in on mousedown and out on mouseup.

like image 120
Castrohenge Avatar answered Sep 21 '22 01:09

Castrohenge