HTML5 Graphics Canvas
canvas Element
The canvas element provides an easy and powerful way to draw graphics on a web page. This element only acts as a container for graphics. Therefore, to actually draw graphics, you must use a scripting language such as JavaScript.
The major web browser versions that support the canvas element are as follows.
| element | ie | chrome | firefox | safari | opera |
|---|---|---|---|---|---|
| canvas | 9.0 | 4.0 | 2.0 | 3.1 | 9.0 |
Attributes of the canvas Element
The canvas element is a simple rectangular area in a web page with no border and no content. Therefore, you must set the canvas size with the style attribute.
To access a canvas element from a script, use the id attribute of that element.
Example
<canvas id="drawCanvas" style="width:300px; height:200px; border: 1px solid #993300;">
This sentence appears when the user's web browser does not support the canvas element!
</canvas>
Drawing Rectangles
After defining the canvas, you can draw graphics on the canvas element by using a script.
The following example draws rectangles on a canvas element by using a script.
Example
context.strokeRect(10, 10, 250, 130);
context.fillStyle = "rgba(255,0,0,1)";
context.fillRect(20, 20, 200, 100);
context.clearRect(30, 30, 150, 50);
The functions used to draw rectangles in the example above receive arguments in the following order.
- x-coordinate of the upper-left corner of the rectangle
- y-coordinate of the upper-left corner of the rectangle
- Width of the rectangle
- Height of the rectangle
The script functions used to draw rectangles are as follows.
| function | description |
|---|---|
| fillStyle() | Sets the color used to fill the rectangular area. You can specify only a color value or include transparency as well. |
| fillRect() | Sets the x and y coordinates of the starting point where the rectangle begins, as well as the rectangle width and height. |
| strokeRect() | Draws a border around a rectangular area. |
| clearRect() | Makes the specified rectangular area transparent. |
Drawing Lines
The following example draws lines on a canvas element by using a script.
context.moveTo(0, 0);
context.lineTo(300, 100);
context.lineTo(150, 150);
context.stroke();
The script functions used to draw lines are as follows.
| function | description |
|---|---|
| moveTo() | Sets the coordinates where the line starts. |
| lineTo() | Sets the coordinates where the line ends. |
| lineTo() | When this function is used repeatedly, the start position is automatically set to the end position of the previous line. |
| stroke() | Starts drawing the line. |
Example
You can use line drawing to create shapes, and you can also fill the shapes you create with color.
context.moveTo(0, 0);
context.lineTo(300, 200);
context.lineTo(150, 0);
context.lineTo(0, 0);
context.fillStyle = "#0099FF";
context.fill();
context.stroke();
In the example above, the moveTo() and lineTo() functions are first used to create a shape by drawing lines. After that, the desired color is specified with fillStyle(), and the fill() function fills the created shape with that color.
Drawing Circles
The following example draws a circle on a canvas element by using a script.
Example
context.beginPath();
context.arc(150, 100, 50, 0, 2 * Math.PI);
context.stroke();
The arc() function used in the example above receives arguments in the following order.
- x-coordinate of the circle center
- y-coordinate of the circle center
- Radius of the circle
- Angle where drawing the arc starts
- Angle where drawing the arc ends
The script functions used to draw circles are as follows.
| function | description |
|---|---|
| beginPath() | Starts drawing a shape. |
| arc() | Sets the center coordinates and radius of the circle, the start and end positions for drawing it, the drawing direction, and other values. |
| closePath() | Finishes drawing the shape. |
Example
You can also use circle drawing to easily create arcs.
context.beginPath();
context.moveTo(100, 100);
context.arc(100, 100, 120, 0, 45 * Math.PI / 180);
context.closePath();
context.stroke();
Math.PI is the ratio of a circle's circumference to its diameter, known as pi, and is approximately 3.14159.
Drawing Text
The following example draws text on a canvas element by using a script.
Example
context.font = "40px Arial";
context.fillText("CANVAS", 50, 90);
context.strokeText("HTML5", 80, 150);
The functions used to draw text in the example above receive arguments in the following order.
- Text to draw on the canvas element
- x-coordinate of the upper-left corner of the text
- y-coordinate of the upper-left corner of the text
The script functions used to draw text are as follows.
| function | description |
|---|---|
| font() | Sets the text size, font, color, and other properties. |
| fillText() | Sets the text content and the coordinates of the start position where the text begins. |
| strokeText() | Draws text with only an outline. |
Drawing Gradients
The following example draws a linear gradient on a canvas element by using a script.
Example
var gradient = context.createLinearGradient(0, 0, 200, 0);
gradient.addColorStop(0, "#FFCC00");
gradient.addColorStop(1, "#FFCCCC");
context.fillStyle = gradient;
context.font = "45px Arial black";
context.fillText("CANVAS", 15, 120);
The createLinearGradient() function used in the example above receives arguments in the following order.
- x-coordinate of the point where the linear gradient starts
- y-coordinate of the point where the linear gradient starts
- x-coordinate of the point where the linear gradient ends
- y-coordinate of the point where the linear gradient ends
In this way, createLinearGradient() creates a linear gradient. At this time, you can use addColorStop() to specify the colors used in the gradient. The created gradient can then be drawn by using the fillStyle or strokeStyle property.
The following example draws a radial gradient on a canvas element by using a script.
Example
var gradient = context.createRadialGradient(100, 100, 200, 150, 150, 30);
gradient.addColorStop(0, "black");
gradient.addColorStop(1, "white");
context.fillStyle = gradient;
context.fillRect(0, 0, 300, 300);
The createRadialGradient() function used in the example above receives arguments in the following order.
- x-coordinate of the center of the circle where the radial gradient starts
- y-coordinate of the center of the circle where the radial gradient starts
- Radius of the circle where the radial gradient starts
- x-coordinate of the center of the circle where the radial gradient ends
- y-coordinate of the center of the circle where the radial gradient ends
- Radius of the circle where the radial gradient ends
The script functions used to draw gradients are as follows.
| function | description |
|---|---|
| createLinearGradient() | Sets the start and end coordinates for drawing a linear gradient. |
| createRadialGradient() | Sets the center coordinates and radius of the larger circle where the radial gradient starts, and the center coordinates and radius of the smaller circle where the gradient ends. |
| addColorStop() | Sets the colors of the gradient. Various colors can be specified from the starting point 0 to the ending point 1. |
The createLinearGradient() and createRadialGradient() functions are not supported in Internet Explorer 8 and earlier.
Drawing Images
The following example draws an image on a canvas element by using a script.
Example
<p><button onclick="drawImage()">Draw image</button></p>
...
<script>
function drawImage() {
var srcImg = document.getElementById("Monalisa");
context.drawImage(srcImg, 10, 10);
}
</script>
The drawImage() function used in the example above receives arguments in the following order.
- Address of the image to draw on the canvas element
- x-coordinate of the upper-left corner of the image
- y-coordinate of the upper-left corner of the image
The script function used to draw images is as follows.
| function | description |
|---|---|
| drawImage() | Sets the address of the image to draw on the canvas element and the starting position where the image will be drawn. |