A&M Unit-3 Notes
A&M Unit-3 Notes
HTML5 - Canvas
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.
Here is a simple <canvas> element which has only two specific
attributes width and height plus all the core HTML5 attributes like id, name and
class, etc.
<canvas id = "mycanvas" width = "100" height = "100"></canvas>
You can easily find that <canvas> element in the DOM
using getElementById() method as follows −
var canvas = document.getElementById("mycanvas");
Let us see a simple example on using <canvas> element in HTML5 document.
<!DOCTYPE HTML>
<html>
<body>
<canvas id = "mycanvas" width = "100" height = "100"></canvas>
</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.
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.
HTML5 Canvas Examples
1. HTML5 Canvas - Drawing Rectangles
• fillRect(x,y,width,height)
This method draws a filled rectangle.
• strokeRect(x,y,width,height)
1
This method draws a rectangular outline.
• clearRect(x,y,width,height)
This method clears the specified area and makes it fully transparent
<!DOCTYPE html>
<html>
<body>
<h1>HTML5 Canvas</h1>
<h2>The rect() Method</h2>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid
grey"></canvas>
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.rect(20, 20, 150, 100);
ctx.stroke();
</script>
</body>
</html>
2
<canvas id="myCanvas" width="300" height="150" style="border:1px solid
grey"></canvas>
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.beginPath();
ctx.lineWidth = "5";
ctx.strokeStyle = "green";
ctx.moveTo(0, 75);
ctx.lineTo(250, 75);
ctx.stroke();
</script>
</body>
</html>
3
</body>
</html>
5
8. HTML5 Canvas – Rotation
HTML5 canvas provides rotate(angle) method which is used to rotate the
canvas around the current origin.
This method only takes one parameter and that's the angle the canvas is
rotated by. This is a clockwise rotation measured in radians.
Syntax
context.rotate(angle)
<html>
<body>
<h1>HTML5 Canvas</h1>
<h2>The rotate() Method</h2>
<canvas id="myCanvas" width="300" height="150" style="border:1px
solid grey"></canvas>
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.rotate(20 * Math.PI / 180);
ctx.fillRect(50, 20, 100, 50);
</script>
</body>
</html>
6
Syntax
context.scale(scalewidth, scaleheight)
<html>
<body>
<h1>HTML5 Canvas</h1>
<h2>The scale() Method</h2>
<canvas id="myCanvas" width="300" height="150" style="border:1px
solid grey"></canvas>
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.strokeRect(5, 5, 25, 15);
ctx.scale(2, 2);
ctx.strokeRect(5, 5, 25, 15);
</script>
</body>
</html>
• The transform() method scales, rotates, moves, and skews the context.
• Each object on the canvas has a transformation matrix.
• The transform() method replaces the transformation matrix, and
multiplies the it with a matrix described by:
ace
bdf
001
Syntax
context.transform(a, b, c, d, e, f)
7
a Scales the drawing horizontally
<html>
<body>
<h1>HTML5 Canvas</h1>
<h2>The transform() Method</h2>
<canvas id="myCanvas" width="300" height="150" style="border:1px
solid grey"></canvas>
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.fillStyle = "yellow";
ctx.fillRect(0, 0, 250, 100)
ctx.transform(1, 0.5, -0.5, 1, 30, 10);
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 250, 100);
ctx.transform(1, 0.5, -0.5, 1, 30, 10);
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, 250, 100);
</script>
</body>
</html>
8
11.HTML5 Canvas – Composition
Syntax
context.globalCompositeOperation = "value"
source-over Default
Displays the source over the destination
9
destination- Displays the destination on top of the source. The part
atop of the destination that is outside the source is not
shown
<html>
<body>
<h1>HTML5 Canvas</h1>
<h2>The globalCompositeOperation Property</h2>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid
grey"></canvas>
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(20, 20, 75, 50);
ctx.fillStyle = "blue";
ctx.globalCompositeOperation = "source-over";
ctx.fillRect(50, 50, 75, 50);
ctx.fillStyle = "red";
ctx.fillRect(150, 20, 75, 50);
ctx.fillStyle = "blue";
ctx.globalCompositeOperation = "destination-over";
ctx.fillRect(180, 50, 75, 50);
</script>
</body>
</html>
10
12.HTML5 Canvas - Text and Fonts
• The font property sets or returns the font properties for canvas text.
• The font property uses the same syntax as the CSS front property.
Syntax
<html>
<body>
<h1>HTML5 Canvas</h1>
<h2>The font Property</h2>
<canvas id="myCanvas" width="300" height="150" style="border:1px
solid grey"></canvas>
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Hello World", 10, 50);
</script>
</body>
</html>
• The shadowBlur property sets or returns the blur level for shadows.
Syntax
context.shadowBlur = number
11
<html>
<body>
<h1>HTML5 Canvas</h1>
<h2>The shadowBlur Property</h2>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid
grey"></canvas>
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.shadowBlur = 20;
ctx.shadowColor = "black";
ctx.fillStyle = "red";
ctx.fillRect(20, 20, 100, 80);
</script>
</body>
</html>
12
13