Canvas element in the html5 allows us to render dynamic 2d shapes inside webpage using Javascript. In this Html canvas tutorial we will cover create different shapes with html canvas tag. The html <canvas> element tag is used to draw graphics.
We will cover
Let's create different canvas tutorial examples
Draw a HTML Canvas
<!DOCTYPE HTML> <html> <head> <head> <meta charset="UTF-8"> <title>Html5 Canvas Tutorial</title> </head> <style> #canvas_id{border:1px solid blue;} </style> </head> <body style="text-align:center;"> <h2> HTML5 Canvas Element</h2> <canvas id="canvas_id" width="200" height="200"></canvas> </body> </html> |
This will give below output
HTML5 Drawing Rectangles
To draw rectangle on the canvas has provided methods
fillRect(x,y,width,height): This canvas method will used to draw a rectangle with fill shape
strokeRect(x,y,width,height): This method will used to create outline rectangle
clearRect() : This canvas method will used to create transparent rectangle
In the above canvas methods x and y params are the position of the canvas from top-left position and width and height are the Canvas height and width respectively.
Canvas Example to Draw simple Rectangle
<!DOCTYPE HTML> <html> <head> <style> #test { width: 200px; height:200px; margin: 20px auto;border:1px solid blue; } </style> <script type="text/javascript"> function drawRectangleShape(){ // Get the canvas element using the DOM var canvas = document.getElementById('canvas_id'); // Check browser support canvas or not if (canvas.getContext){ // use getContext to use the canvas for drawing var ctx = canvas.getContext('2d'); // Draw shapes ctx.fillStyle = "#FF0000"; ctx.fillRect(25,25,100,100); ctx.clearRect(45,45,60,60); ctx.strokeStyle = "#228B22"; ctx.strokeRect(50,50,50,50); } else { alert('You need Safari or Firefox 1.5+ to see this demo.'); HTML5 69 } } </script> </head> <body id="test" onload="drawRectangleShape();" style="padding:10px; text-align:center; color:#000;" > <canvas id="canvas_id"></canvas> </body> </html> |
Output:
Canvas Draw Paths:
fill() : This canvas method will used to Fills the current drawing (path) |
Canvas path example to draw a shape
<!DOCTYPE HTML> <html> <head> <style> body { width: 100px; height:100px; margin: 0px auto; } </style> <script type="text/javascript"> function drawShape(){ // get the canvas element using the DOM var canvas = document.getElementById('canvas_id'); // Make sure we don't execute when canvas isn't supported if (canvas.getContext){ // use getContext to use the canvas for drawing var ctx = canvas.getContext('2d'); // Draw shapes ctx.strokeStyle = "#FF0000"; ctx.beginPath(); ctx.arc(75,75,50,0,Math.PI*2,true); // Outer circle ctx.moveTo(110,75); ctx.arc(75,75,35,0,Math.PI,false); // Mouth ctx.moveTo(65,65); ctx.arc(60,65,5,0,Math.PI*2,true); // Left eye ctx.moveTo(95,65); ctx.arc(90,65,5,0,Math.PI*2,true); // Right eye ctx.stroke(); } else { alert('You need Safari or Firefox 1.5+ to see this demo.'); } } </script> </head> <body onload="drawShape();"> <canvas id="canvas_id"></canvas> </body> </html> |
output:
Canvas Draw Lines
lineCap() This canvas method used to Sets or returns the style of the end caps for a line |
Example to draw lines on canvas
<!DOCTYPE HTML> <html> <head> <style> body { text-align:center; height:100px; margin: 20px auto; } </style> <script type="text/javascript"> function drawShape(){ // get the canvas element using the DOM var canvas = document.getElementById('canvas_id'); // Make sure we don't execute when canvas isn't supported if (canvas.getContext){ // use getContext to use the canvas for drawing var ctx = canvas.getContext('2d'); ctx.strokeStyle = "#FF0000"; for (i=0;i<10;i++){ ctx.lineWidth = 1+i; ctx.beginPath(); ctx.moveTo(5+i*14,5); ctx.lineTo(5+i*14,140); ctx.stroke(); } } else { alert('You need Safari or Firefox 1.5+ to see this demo.'); } } </script> </head> <body onload="drawShape();"> <h2>HTML5 Canvas Tutorial to draw lines</h2> <canvas id="canvas_id"></canvas> </body> </html> |
Output:
Conclusion: In this HTML canvas tutorial we covered how to use html canvas tag to draw rectangle, lines and paths on canvas.
Article Contributed By :
|
|
|
|
689 Views |