The CanvasRenderingContext2D.transform()
method of the Canvas 2D API multiplies the current transformation with the matrix described by the arguments of this method. This lets you scale, rotate, translate (move), and skew the context.
Note: See also the setTransform()
method, which resets the current transform to the identity matrix and then invokes transform()
.
transform(a, b, c, d, e, f)
The transformation matrix is described by:
This example skews a rectangle both vertically (.2
) and horizontally (.8
). Scaling and translation remain unchanged.
HTML
<canvas id="canvas"></canvas>
JavaScript
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.transform(1, .2, .8, 1, 0, 0);
ctx.fillRect(0, 0, 100, 100);
Result