Web Technology I · Unit 6
HTML5 Multimedia and Canvas
Exam-focused notes for HTML5 Multimedia and Canvas (Web Technology I, BIT251): what the TU syllabus asks and how it has actually been tested, with 2 solved past questions from this unit.
HTML5 Multimedia and Canvas in short
HTML5 lets pages play video and draw graphics without plug-ins. This chapter covers the video tag and the canvas element for drawing with JavaScript.
What this unit covers
- HTML5 video element
- Canvas element and drawing
- Canvas shapes circles and rectangles
- Canvas context and methods
Canvas element and drawing
2079 · 5 marks
What is canvas element? Write a HTML script to create a canvas field having id cv1 and draw a circle inside the canvas. [5]
Show solved answerHide answer
Canvas Element in HTML
What is Canvas Element?
The <canvas> element is an HTML5 element used to draw graphics on a web page using JavaScript. It acts as a drawing container (a rectangular area/field) on which graphics, shapes, images, animations, and other visual content can be rendered dynamically through scripting (usually JavaScript).
Key Points:
- It is defined using the
<canvas>tag. - By default, it has no border and no content.
- The actual drawing is done using JavaScript with the Canvas API.
- Common attributes:
id,width,height.
Syntax:
<canvas id="myCanvas" width="value" height="value"></canvas>
HTML Script: Canvas with id="cv1" and a Circle
<!DOCTYPE html>
<html>
<head>
<title>Canvas Circle Example</title>
</head>
<body>
<h2>Drawing a Circle on Canvas</h2>
<!-- Canvas field with id cv1 -->
<canvas id="cv1" width="400" height="300"
style="border: 2px solid black;">
Your browser does not support the canvas element.
</canvas>
<script>
// Step 1: Get the canvas element by its id
var canvas = document.getElementById("cv1");
// Step 2: Get the 2D drawing context
var ctx = canvas.getContext("2d");
// Step 3: Begin drawing the circle
ctx.beginPath();
// Step 4: arc(x, y, radius, startAngle, endAngle)
// x=200, y=150 is the center; radius=80
// 0 to 2*Math.PI draws a full circle
ctx.arc(200, 150, 80, 0, 2 * Math.PI);
// Step 5: Set fill color and fill the circle
ctx.fillStyle = "skyblue";
ctx.fill();
// Step 6: Set stroke (border) color and draw the border
ctx.strokeStyle = "navy";
ctx.lineWidth = 3;
ctx.stroke();
</script>
</body>
</html>
Explanation of Key Steps
| Step | Code | Purpose |
|---|---|---|
| 1 | getElementById("cv1") | Selects the canvas element |
| 2 | getContext("2d") | Gets the 2D rendering context |
| 3 | beginPath() | Starts a new drawing path |
| 4 | arc(x, y, r, 0, 2*Math.PI) | Defines a full circle |
| 5 | fill() | Fills the circle with color |
| 6 | stroke() | Draws the circle border |
Note:
arc(x, y, radius, startAngle, endAngle)- angles are in radians. A full circle =2 * Math.PIradians (360°).
HTML5 video element
0 · 5 marks
Write HTML script to show the use of Video and Canvas elements. [5]
Make Unit 6 stick
Practice BIT251 with flashcards & quizzes