📗 Things made of pieces, pieces made of smaller pieces.
➩ Each piece should be drawn relative to its parent, not the origin of the HTML Canvas.
➩ Each piece should be transformed relative to its parent, not the origin of the HTML Canvas.
➩ For pieces with multiple children, the transformations should be saved using context.save() before making each child and restored using context.restore().
📗 This uses the fact that context is a stack, and context.save() behaves like "push" and context.restore() behaves like "pop".
📗 Transformations are functions that apply to points: \(x' = f\left(x\right)\).
📗 Composition of functions are combination of function, the last function is applied first: \(\left(h \circ g \circ f\right)\left(x\right) = h\left(g\left(f\left(x\right)\right)\right)\).
📗 For the same reason, transformations on context can be read backwards when applied on the objects.
📗 Rotating a point \(\left(x, y\right)\) with angle \(r\) counterclockwise is equivalent to multiplying the points by \(\begin{bmatrix} \cos\left(r\right) & -\sin\left(r\right) \\ \sin\left(r\right) & \cos\left(r\right) \end{bmatrix}\).
➩ 0 degree rotation matrix is the identity matrix \(\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\) (not the zero-matrix).
📗 Scaling a point \(\begin{bmatrix} x \\ y \end{bmatrix}\) with scale \(\left(s_{x}, s_{y}\right)\) is equivalent to multiplying the points by \(\begin{bmatrix} s_{x} & 0 \\ 0 & s_{y} \end{bmatrix}\).
📗 Shearing (shear transformation) is another linear transformation: Wikipedia.
📗 X-shearing (horizontal shearing) can be achieved through multiplication by \(\begin{bmatrix} 1 & s_{x} \\ 0 & 1 \end{bmatrix}\), and Y-shearing (vertical shearing) can be achieved through multiplication by \(\begin{bmatrix} 1 & 0 \\ s_{y} & 1 \end{bmatrix}\). HTML Canvas does not have a function for the shear transform.
📗 A 2D point \(\begin{bmatrix} x \\ y \end{bmatrix}\) can be represented by 3D point with \(z = 1\), \(\begin{bmatrix} x \\ y \\ 1 \end{bmatrix}\).
📗 Arbitrary 3D linear transformations can be applied to \(\begin{bmatrix} x \\ y \\ 1 \end{bmatrix}\), and the result can be projected back onto \(z = 1\) plane (\(\begin{bmatrix} x' \\ y' \\ z' \end{bmatrix}\) can be projected back as \(\begin{bmatrix} \dfrac{x'}{z'} \\ \dfrac{y'}{z'} \\ 1 \end{bmatrix}\)).
📗 HTML Canvas does not allow arbitrary 3D linear transformations for its 2D context.
📗 Canvas has a general transformation function context.transform(a, b, c, d, e, f): Doc.
📗 This is the affine transformation represented by multiplication by the matrix \(\begin{bmatrix} a & c & e \\ b & d & f \\ 0 & 0 & 1 \end{bmatrix}\) (since the last row is not needed for affine transformations in 2D).
📗 The matrix has three columns:
➩ \(\begin{bmatrix} a \\ b \end{bmatrix}\) is where the x-axis (or the vector \(\begin{bmatrix} 1 \\ 0 \end{bmatrix}\)) is transformed to.
➩ \(\begin{bmatrix} c \\ d \end{bmatrix}\) is where the y-axis (or the vector \(\begin{bmatrix} 0 \\ 1 \end{bmatrix}\)) is transformed to.
➩ \(\begin{bmatrix} e \\ f \end{bmatrix}\) is where the origin (or the point \(\begin{bmatrix} 0 \\ 0 \end{bmatrix}\)) is transformed to.