i think you're trying to rewrite 2d affine transforms for which canvas already supports (look up `ctx.translate`, `ctx.rotate`, `ctx.scale`)