Absolute scale
You can set an absolute scale by replacing the current transform with ctx.setTransform
All the other transformation functions ctx.scale
, ctx.translate
, ctx.rotate
, ctx.transform
apply the transform to the existing transform. That is why your scale doubles each time (2,4,8,16)
Thus to set an absolute scale
scale = 1;
ctx.setTransform(scale, 0, 0, scale, 0, 0);
To scale by 3
scale += 2;
ctx.setTransform(scale, 0, 0, scale, 0, 0);
To scale by 5 add 2 again
scale += 2;
ctx.setTransform(scale, 0, 0, scale, 0, 0);
Note this replaces any existing transformations.
To set a complete uniform transform
function setTransform(ctx, x, y, scale, rotate) {
ctx.setTransform(
Math.cos(rotate) * scale, Math.sin(rotate) * scale, // x axis
-Math.sin(rotate) * scale, Math.cos(rotate) * scale, // y axis is 90deg CW from x axis
x, y // translation as absolute sets origin in px from top left of canvas
);
}