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

HTML5 Tutorial FeedBurner

Adding and drawing on canvas in HTML5  
You can add canvas to your HTML5 page with the help of HTML5 tag called canvas. See example below

<canvas height="100" width="100"> </canvas>

This HTML5 element can have attributes just like any older HTML tag. In order to put a border around this canvas, we’ll need to add style to this tag

<canvas height="100" width="100"  style="border: 1px solid;"> </canvas>

HTML5 canvas needs to be identified in order for our JavaScript find canvas as object on our page.

<canvas id="sample" height="100" width="100"  style="border: 1px solid;"> </canvas>

Let’s draw something on our canvas in order to make it useful to our users. We’ll draw a simple line for this example.

<script>

 

  function drawLine()

  {

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

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

    myContext.beginPath();

    myContext.moveTo(30, 90);

    myContext.lineTo(90, 30);

    myContext.stroke();

 

  }

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

 

</script>

This code is not overly complicated if you already know JavaScript. However, if you are new to JavaScript then you may read about it online. In brief, we set two points on our canvas and called on stroke() method to draw a line between two points.
Print Adding and drawing on canvas in HTML5 Bookmark Adding and drawing on canvas in HTML5

Related Articles  
HTML5 Gradient
Applying HTML5 gradient requires three steps to be completed. Creation of the gradient object, applying colors and call ...
Using HTML5 Canvas
HTML5 canvas element creates rectangular area on your HTML5 webpage. It is defaulted at 350 by 150 pixels which can be ...
HTML5 Canvas Coordinates
The HTML5 canvas has X-Axis and Y-Axis and is two dimensional. It has starting point in the upper-left corner which ...
Working with HTML5 Paths
The HTML5 Paths are designed to represent any shape that you may want to render. You would use beginPath call to start ...
HTML5 canvas browser compatibility
HTML5 is not official and mandated version of HTML and not every browser vendor supports it. You need to be aware of ...
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 ...
More