HTML5 introduced new Canvas element to Draw 2D graphics by scripting. It is a rectangular area on web page.
of the browsers. To set-up 2D context has to be included. width and height are the only two attributes which specifies the canvas width and height. Initially canvas will be 300px wide and 150px in height.
var canvas = document.getElementById('canvas'); - by calling getElementById it retrieves the node representing canvas.
var c = canvas.getContext('2D'); - to render the context and functions.
fillRect(x,y,width,height) - It draws a rectangle.
strokeRect(x,y,width,height) - It draws a outline for the rectangle.
clearrect(x,y,width,height) - It makes the area transparent and clears the rectangular area.
All other shapes other than rectangle can be created by combining one or more paths. Below are the functions.
beginPath() - It creates a new path
closepath() - It closes the path which was created before.
stoke() - It is used to draw the shape by its outline.
fill() - It draws the solid shape.
lineTo(x,y) - It draws a line as specified by x and y.
movesTo(x, y) - It moves to particular co-ordinates as specified by x and y.
Below is the sample code to create canvas.
Output of the above code is
ZenRays provide the following to make you expert
zenrays providing html5 training in Bangalore in Bangalore
Fully practical and project-based sessions from first class.
Training by Experienced Consultants, not regular Trainers
Friendly and enthusiastic faculty to clear your doubts 24X7
Free Live project after the training to get you industry experience
If you want more details please visit us:
Zenrays.com Reach us at [email protected]
var canvas = document.getElementById('canvas'); - by calling getElementById it retrieves the node representing canvas.
var c = canvas.getContext('2D'); - to render the context and functions.
fillRect(x,y,width,height) - It draws a rectangle.
strokeRect(x,y,width,height) - It draws a outline for the rectangle.
clearrect(x,y,width,height) - It makes the area transparent and clears the rectangular area.
All other shapes other than rectangle can be created by combining one or more paths. Below are the functions.
beginPath() - It creates a new path
closepath() - It closes the path which was created before.
stoke() - It is used to draw the shape by its outline.
fill() - It draws the solid shape.
lineTo(x,y) - It draws a line as specified by x and y.
movesTo(x, y) - It moves to particular co-ordinates as specified by x and y.
Below is the sample code to create canvas.
Canvas
#canvas{
border: 3px solid green;
}
var canvas = document.getElementById('canvas');
var c = canvas.getContext('2d');
c.fillStyle = 'red';
c.beginPath();
c.moveTo(50,500);
c.fillRect(50,50,800,500);
c.fill();
c.lineWidth=2;
c.strokeStyle="red";
c.stroke();
ZenRays provide the following to make you expert
zenrays providing html5 training in Bangalore in Bangalore
Fully practical and project-based sessions from first class.
Training by Experienced Consultants, not regular Trainers
Friendly and enthusiastic faculty to clear your doubts 24X7
Free Live project after the training to get you industry experience
If you want more details please visit us:
Zenrays.com Reach us at [email protected]
This post first appeared on Best Software Training Institute In Bangalore, please read the originial post: here