Introduction

Welcome back. This is the second part of the HTML5 Canvas Advanced series. In my previous article, I talked about basic transformations, like rotation, translation, and scaling. Now in today's article, we cover some advanced topics of transformations, like custom transformations, shearing, mirroring and so on. Before starting our article, I would like to talk about the grid we used in the previous article and we will also use it in this article. Let us see how to make a grid with an example.
Grid Example: Live Demo
- <body>
- <form id="form1" runat="server">
- <div>
- <input type="button" id="button" value="Generate Grid" onclick="generateGrid()" /><br />
- <canvas id="drawCanvas" width="500" height="400"></canvas>
- <script>
- var drawCanvas = document.getElementById('drawCanvas');
- var ctx = drawCanvas.getContext('2d');
- var gridDraw = function (ctx, color, x_axis, y_axis) {
- ctx.strokeStyle = color;
- ctx.lineWidth = 0.5;
- for (var i = x_axis + 0.5; i < ctx.canvas.width; i += x_axis) {
- ctx.beginPath();
- ctx.moveTo(i, 0);
- ctx.lineTo(i, ctx.canvas.height);
- ctx.stroke();
- }
- for (var i = y_axis + 0.5; i < ctx.canvas.height; i += y_axis) {
- ctx.beginPath();
- ctx.moveTo(0, i);
- ctx.lineTo(ctx.canvas.width, i);
- ctx.stroke();
- }
- }
- var generateGrid = function () {
- gridDraw(ctx, '#000', 10, 10);
- }
- </script>
- </div>
- </form>
- </body>
Output

Custom Transform
In HTML5 Canvas, the transform() method applies a custom transformation matrix to the canvas context. The "transform" method requires six parameters that are as follows:
- m11 - It scales the drawing horizontally
- m12 - It skews the drawing horizontally
- m21 - It skews the drawing vertically
- m22 - It scales the drawing vertically
- dx - It moves the drawing horizontally
- dy - It moves the drawing vertically
Now we can use the "transform(m11, m12, m21, m22, dx, dy)" method. It multiplies the current transformation matrix with the matrix described by:
m11 m21 dx
m12 m22 dy
0 0 1
It is important to know that the transformation will affect only those shapes drawn after the "transform()" method. Here, the scale m11 and scale m22 are decimal percentages, so 1 = 100%, where as .5 = 50% and 2 = 200%. The other values will add a transformation, so 0 means no change and then a positive or negative number will make a change.
Example: Live Demo
- <body>
- <form id="form1" runat="server">
- <div>
- <input type="button" id="button" value="Transform" onclick="effect()" /><br />
- <canvas id="drawCanvas" width="500" height="400"></canvas>
- <script>
- var drawCanvas = document.getElementById('drawCanvas');
- var ctx = drawCanvas.getContext('2d');
- gridDraw(ctx, '#333', 15, 15);
- ctx.fillStyle = "#ff0000";
- ctx.fillRect(200, 10, 200, 100);
- var effect = function () {
- ctx.transform(1, 0.5, -0.5, 1, -10, -10);
- ctx.fillStyle = "#0000ff";
- ctx.fillRect(200, 10, 200, 100);
- ctx.transform(1, 0.5, -0.5, 1, -10, -10);
- ctx.fillStyle = "#00ff00";
- ctx.fillRect(200, 10, 200, 100);
- }
- </script>
- </div>
- </form>
- </body>
Output





Join the conversation! Your thoughts help the community grow.