Logo
  Sunday, May 20, 2012
Sign-In  |  Sign-Up  |  Contact Us  | Bookmark |  RSS Feed

HTML5 Tutorial FeedBurner

HTML5 canvas transformation  
Canvas transformation is generally used to set up comprehensive canvas. It can be overkill if you want to create simple canvas. You would use drawing on canvas in those cases.

HTML5 canvas transformation rely on modification layer that is always present as part of your canvas. You may choose not to use but it will always be there for you.

It is good practice, when using canvas transformation, to start your drawing at the origin of the canvas and then apply all of the required transformations as you go with your task at hand.

Let’s look at how to apply transformation for drawing a line on canvas.

// JScript source code

 

<script>

 

  function drawLine() {

 

    var myCanvas = document.getElementById('canvas');

    var myContext = canvas.getContext('2d');

    myContext.save();

    myContext.translate(30,90);

    myContext.beginPath();

    myContext.moveTo(0, 0);

    myContext.lineTo(90, -90);

    myContext.stroke();

    myContext.restore();

 

  }

 

   window.addEventListener("load", drawLine, true);

 

</script>

Important points about this code are: saving context before you apply changes to canvas will help you restore it at the end of the drawing and transformation operations. In addition, we start at (0,0) coordinates which is a good practice.
Print HTML5 canvas transformation Bookmark HTML5 canvas transformation

Related Articles  
Using HTML5 Stroke Styles
HTML5 provides with new styling capabilities when it comes to canvas. We can improve the way lines are drawn on canvas ...
HTML5 Background Patterns
Images can be used throughout web application, but sometimes it is useful to set images as backgrounds.
How to insert Images into a canvas in HTML5?
HTML5 allows adding and manipulating images inside a canvas. Images can be modified, stretched and stamped with the ...
HTML5 Canvas Transforms context.rotate(x) function
HTML5 provides several transform operations: scale, translate, rotate and others. Let’s look at the context.rotate(x) ...
Creating Shadow Effect with HTML5
HTML5 shadows are controlled by several operations available in HTML5 arsenal of operations. The shadow effect can be ...
Adding and drawing on canvas in HTML5
You can add canvas to your HTML5 page with the help of HTML5 tag called canvas.
More