Unit IV
Unit IV
HTML5 element <canvas> gives you an easy and powerful way to draw
graphics using JavaScript. It can be used to draw graphs, make photo
compositions or do simple (and not so simple) animations.
The HTML <canvas> element is used to draw graphics, on the fly, via scripting
(usually JavaScript).
The <canvas> element is only a container for graphics. You must use a script to
actually draw the graphics.
Canvas has several methods for drawing paths, boxes, circles, text, and adding
images.
Canvas has great features for graphical data presentation with an imagery of
graphs and charts.
Canvas can respond to any user action (key clicks, mouse clicks, button clicks,
finger movement).
HTML Canvas Can be Used in Games
Canvas' methods for animations, offer a lot of possibilities for HTML gaming
applications.
<canvas id="myCanvas" width="200" height="100"></canvas>
The width and height attribute is necessary to define the size of the canvas.
<html>
<head>
<style>
</style>
</head>
<body>
</body>
</html>
The Rendering Context
The <canvas> is initially blank, and to display something, a script first
needs to access the rendering context and draw on it.
The canvas element has a DOM method called getContext, used to
obtain the rendering context and its drawing functions. This function
takes one parameter, the type of context2d.
Following is the code to get required context along with a check if your
browser supports <canvas> element −
var canvas = document.getElementById("mycanvas");
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
// drawing code here
} else {
// canvas-unsupported code here
}
Browser Support
The latest versions of Firefox, Safari, Chrome and Opera all support for
HTML5 Canvas but IE8 does not support canvas natively.
You can use ExplorerCanvas to have canvas support through Internet
Explorer. You just need to include this JavaScript as follows −
<!--[if IE]><script src = "excanvas.js"></script><![endif]-->
HTML5 Canvas - Drawing Rectangles
There are three methods that draw rectangles on the canvas −
Sr.No. Method and Description
fillRect(x,y,width,height)
1
This method draws a filled rectangle.
strokeRect(x,y,width,height)
2
This method draws a rectangular outline.
clearRect(x,y,width,height)
3 This method clears the specified area and makes it fully
transparent
<!DOCTYPE html>
<html>
<body>
<h1>HTML5 Canvas</h1>
<script>
ctx.beginPath();
ctx.stroke();
</script>
</body>
</html>
Param Description
beginPath()
1
This method resets the current path.
2
moveTo(x, y)
This method creates a new subpath with the given point.
closePath()
3 This method marks the current subpath as closed, and starts a new
subpath with a point the same as the start and end of the newly
closed subpath.
fill()
4
This method fills the subpaths with the current fill style.
stroke()
5
This method strokes the subpaths with the current stroke style.
<!DOCTYPE HTML>
<html>
<head>
<style>
#test {
width: 100px;
height:100px;
</style>
function drawShape() {
if (canvas.getContext) {
// Draw shapes
ctx.beginPath();
ctx.moveTo(110,75);
ctx.arc(75,75,35,0,Math.PI,false); // Mouth
ctx.moveTo(65,65);
ctx.moveTo(95,65);
ctx.stroke();
} else {
alert('You need Safari or Firefox 1.5+ to see this demo.');
</script>
</head>
<canvas id = "mycanvas"></canvas>
</body>
</html>
Line Methods
We require the following methods to draw lines on the canvas –
The Methods
The beginPath() method starts a new path. It does not draw anything, it just d
efines a new path.
The moveTo() defines the starting point of the line. It does not draw anything, it
just sets a start point.
The lineTo() method defines the end point of the line. It does not draw
anything, just sets an end point.
closePath()
This method marks the current subpath as closed, and starts a new
subpath with a point the same as the start and end of the newly closed
subpath.
fill()
This method fills the subpaths with the current fill style
<!DOCTYPE html>
<html>
<body>
<h1>HTML5 Canvas</h1>
<h2>The lineWidth Propery</h2>
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(200,100);
ctx.lineWidth = 10;
ctx.stroke();
</script>
</body>
</html>
Line Properties
There are several properties which allow us to style lines.
S.No. Property and Description
lineWidth [ = value ]
1 This property returns the current line width and can be set, to
change the line width.
lineCap [ = value ]
2 This property returns the current line cap style and can be set, to
change the line cap style. The possible line cap styles are butt,
round, and square
lineJoin [ = value ]
3 This property returns the current line join style and can be set, to
change the line join style. The possible line join styles are bevel,
round, and miter.
miterLimit [ = value ]
4 This property returns the current miter limit ratio and can be set,
to change the miter limit ratio.
<!DOCTYPE HTML>
<html>
<head>
<style>
#test {
width: 100px;
height:100px;
</style>
function drawShape() {
if (canvas.getContext) {
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 {
</script>
</head>
<canvas id = "mycanvas"></canvas>
</body>
</html>
beginPath()
1
This method resets the current path.
moveTo(x, y)
2
This method creates a new subpath with the given point.
3
closePath()
This method marks the current subpath as closed, and starts a new
subpath with a point the same as the start and end of the newly
closed subpath.
fill()
4
This method fills the subpaths with the current fill style.
stroke()
5
This method strokes the subpaths with the current stroke style.
<html>
<head>
<style>
#test {
width: 100px;
height:100px;
</style>
function drawShape() {
if (canvas.getContext) {
ctx.beginPath();
ctx.moveTo(75,40);
ctx.bezierCurveTo(75,37,70,25,50,25);
ctx.bezierCurveTo(20,25,20,62.5,20,62.5);
ctx.bezierCurveTo(20,80,40,102,75,120);
ctx.bezierCurveTo(110,102,130,80,130,62.5);
ctx.bezierCurveTo(130,62.5,130,25,100,25);
ctx.bezierCurveTo(85,25,75,37,75,40);
ctx.fill();
} else {
</script>
</head>
<canvas id = "mycanvas"></canvas>
</body>
</html>
beginPath()
1
This method resets the current path.
2
moveTo(x, y)
This method creates a new subpath with the given point.
closePath()
3 This method marks the current subpath as closed, and starts a new
subpath with a point the same as the start and end of the newly
closed subpath.
fill()
4
This method fills the subpaths with the current fill style.
stroke()
5
This method strokes the subpaths with the current stroke style.
quadraticCurveTo(cpx, cpy, x, y)
6 This method adds the given point to the current path, connected to
the previous one by a quadratic Bezier curve with the given
control point.
<html>
<head>
<style>
#test {
width: 100px;
height:100px;
</style>
function drawShape() {
if (canvas.getContext) {
// Draw shapes
ctx.beginPath();
ctx.moveTo(75,25);
ctx.quadraticCurveTo(25,25,25,62.5);
ctx.quadraticCurveTo(25,100,50,100);
ctx.quadraticCurveTo(50,120,30,125);
ctx.quadraticCurveTo(60,120,65,100);
ctx.quadraticCurveTo(125,100,125,62.5);
ctx.quadraticCurveTo(125,25,75,25);
ctx.stroke();
} else {
</script>
</head>
<canvas id = "mycanvas"></canvas>
</body>
</html>