6 Html5 Multimedia And Canvas

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.

HTML5 Multimedia and Canvas: notes

HTML5 video element

The <video> tag plays video directly in the browser. Older pages needed plug-ins such as Flash. The <source> tag offers several file formats, and the browser picks the first one it supports.

Common formats are MP4 (H.264), WebM and Ogg.

Useful attributes are given below.

  • controls: shows play, pause and volume buttons.
  • autoplay: starts playing on load. Browsers often need muted with it.
  • loop: repeats the video.
  • muted: starts without sound.
  • poster: an image shown before play.
  • width and height: size of the player.
  • preload: how much to load in advance.
<video width="320" height="240" controls poster="cover.jpg">
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.webm" type="video/webm">
  Your browser does not support the video tag.
</video>

The text inside the tag is a fallback for old browsers. JavaScript can control the player with play(), pause() and the currentTime property.

<video id="v" src="movie.mp4" width="300"></video>
<button onclick="document.getElementById('v').play()">Play</button>
<button onclick="document.getElementById('v').pause()">Pause</button>

The similar <audio> tag plays sound in the same way.

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 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

StepCodePurpose
1getElementById("cv1")Selects the canvas element
2getContext("2d")Gets the 2D rendering context
3beginPath()Starts a new drawing path
4arc(x, y, r, 0, 2*Math.PI)Defines a full circle
5fill()Fills the circle with color
6stroke()Draws the circle border

Note: arc(x, y, radius, startAngle, endAngle) - angles are in radians. A full circle = 2 * Math.PI radians (360°).

Open this answer on its own page →

HTML5 video element

0 · 5 marks

Write HTML script to show the use of Video and Canvas elements. [5]

Below is an HTML script demonstrating the use of both the <video and <canvas elements. --- Attribute Purpose -------------------- controls Displays play, pause, volume controls autoplay Video starts playing automatically muted Starts video without sound loop Video repeats after it ends <source Pr...

Open this answer on its own page →