Introduction
In this article, we are going to understand the concept of Canvas Shear Transform Using HTML 5. In this section to shear the HTML5 canvas, we can use the transform() method with the transformation matrix below. The horizontal shear is defined by sx and the vertical shear is defined by sy.
Shear matrix
1 sx 0
sy 1 0
0 0 1
Here we will use some JavaScript and some styles along with HTML code. Just use the steps to see how to create this application.
Let's see how the CanvasShearTransforms application can be created. To do so use the following steps.
Step 1 : Open a HTML editor or Visual Studio.


- Go to Solution Explorer
- Right-click on the Application name
- Select Add-->add new item
- Now in the window that opens, select an HTML page or new Webform
- Rename it to sheartransform.aspx

CSS Script
- <style>
- body
- {
- margin: 0px;
- padding: 0px;
- }
- #myCanvas
- {
- border: 2px solid #FFFFCC;
- margin-top: 50px;
- margin-left: 50px;
- background-color: #CCCCCC;
- box-shadow: 5px 5px 8px #222;
- }
- .title
- {
- text-align: center;
- font-family: Segoe UI Light, Arial, Helvetica;
- font-size: 2.2em;
- margin: 1em;
- }
- .info
- {
- text-align: center;
- font-family: Segoe UI Light, Arial, Helvetica;
- font-size: 1.2em;
- margin: 0.25em;
- }
- </style>
Step 3 : In this part we need to work on some JavaScript. To fully understanding how the JavaScript works, download the attached .rar file and run the CanvasShearTransforms application.
The whole JavaScript looks as in the following.
- <script>
- window.onload = function ()
- {
- var canvas = document.getElementById("myCanvas");
- var context = canvas.getContext("2d");
- var rectWidth = 150;
- var rectHeight = 75;
- var sx = 0.75;
- var sy = 0;
- context.translate(canvas.width / 2, canvas.height / 2);
- // apply custom transform
- context.transform(1, sy, sx, 1, 0, 0);
- context.fillStyle = "red";
- context.fillRect(-rectWidth / 2, -rectHeight / 2, rectWidth, rectHeight);
- };
- </script>


Join the conversation! Your thoughts help the community grow.