The canvas context drawing methods asks for us to provide some coordinates and lengths in their methods.
For instance, the x
, y
, width
, height
of the fillRect(x y, width, height)
method.
All these values are unit-less, so let's name it magical-unit.
Since the canvas is a raster image, it has been decided that at first, each magical-unit represents one pixel of this raster image. When we set the canvas width
or height
, we are just changing the number of pixels that the canvas can hold.
So from this, we can build a nice magical-unit-grid of canvas.width * canvas.height * 1.
In this scenario, when passing (10, 10)
as arguments to a method expecting coordinates in magical-units, the pointer will get moved to the pixels at coordinates 10, 10
.
Now, there are some methods, like scale()
, but also translate()
, transform()
etc. that will modify our magical-unit-grid.
translate(5, 10)
for instance will move our magical-unit-grid by 5 magical-units horizontally and 10 vertically. So now coordinates (10,10)
will actually point to the pixels at coordinates 15, 20
(10 + 5, 10 + 10).
And after a call to scale(2, 2)
, one magical-unit will now be equal to two pixels on the canvas. So, if we pass (10, 10)
to the same method as above, our pointer will actually move to the pixels at coordinates 20, 20
.
Our previous drawings won't have changed, the quality of our canvas is no-different, the only thing that did change is the value of our magical-unit.