I have a variable, context, which is the 2d context of my canvas.
var canvas = document.getElementById('canvas');
context = canvas.getContext('2d');
Calling context.scale(x,y)
multiple times scales the context depending on what the previous scales did. For example, context.scale(2,2); context.scale(2,2)
is equivalent to context.scale(4,4)
. How can I reset the context's scale?
From the course navigation menu, select Settings. In the "Settings" sidebar at the right, select Delete All Course Content. You will be prompted to confirm. To proceed, click Reset Course Content, or click Cancel to cancel.
The scale() method scales the current drawing, bigger or smaller. Note: If you scale a drawing, all future drawings will also be scaled. The positioning will also be scaled. If you scale(2,2); drawings will be positioned twice as far from the left and top of the canvas as you specify.
The ctx. restore() method of the Canvas 2D API restores the most recently saved canvas state by popping the top entry in the drawing state stack. If there is no saved state, this method does nothing.
To clear the Canvas, you can use the clearRect() method. This method performs pretty well than others for clearing the canvas (such as resetting the width/height, destroying the canvas element and then recreating it, etc..) const context = canvas. getContext('2d'); context.
scale
will multiply the current transform matrix by a scale matrix, so indeed, these scale factors multiply. You can use the state stack to save and restore the current transform:
context.save();
context.scale(2, 2);
... // anything drawn here is twice as big
context.restore();
Alternatively, you can reset the transform by loading the identity matrix directly:
context.scale(2, 2);
...
context.setTransform(1, 0, 0, 1, 0, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With