Computer >> Computer tutorials >  >> Programming >> Javascript

How to create a transformation matrix with HTML5?


HTML5 canvas provides methods that allow modifications directly to the transformation matrix. The transformation matrix must initially be the identity transform. It may then be adjusted using the transformation methods.

S No.
Method and Description
1
transform(m11, m12, m21, m22, dx, dy)
This method changes the transformation matrix to apply the matrix given by the arguments.
2
setTransform(m11, m12, m21, m22, dx, dy)
This method changes the transformation matrix to the matrix given by the arguments.

The transform(m11, m12, m21, m22, dx, dy) method must multiply the current transformation matrix with the matrix described by −

m11   m21   dx
m12   m22   dy
0     0     1

To create a transformation matrix, use MathML.

Example

Following is a simple example which makes use of transform() and setTransform() methods:

<!Doctype html>
<html>
   <head>
      <meta charset = "UTF-8">
      <title>Pythagorean theorem</title>
   </head>

   <body>
      <math xmlns = "https://www.w3.org/1998/Math/MathML">
         <mrow>
            <msup>
               <mi>a</mi>
               <mn>2</mn>
            </msup>
            <mo> + </mo>
            <msup>
               <mi>b</mi>
               <mn>2</mn>
            </msup>
            <mo> = </mo>
            <msup>
               <mi>c</mi>
               <mn>2</mn>
            </msup>
         </mrow>
      </math>
   </body>
</html>


<!doctype html>
<html>
   <head>
      <meta charset = "UTF-8">
      <title>Pythagorean theorem</title>
   </head>

   <body>
      <math xmlns = "https://www.w3.org/1998/Math/MathML">
         <mrow>
            <msup>
               <mi>a</mi>
               <mn>2</mn>
            </msup>
            <mo> + </mo>
            <msup>
               <mi>b</mi>
               <mn>2</mn>
            </msup>
            <mo> = </mo>
            <msup>
               <mi>c</mi>
               <mn>2</mn>
            </msup>
         </mrow>
      </math>
   </body>
</html>