HTML Canvas Get Certified
What is HTML Canvas?
The <canvas> element in HTML5 allows developers to draw graphics using JavaScript. It is widely used for creating charts, games, animations, and interactive visual content.
Types of Canvas Applications
- Static Graphics: Draw shapes, text, and images.
- Animations: Create moving objects using
requestAnimationFrame. - Interactive Applications: Respond to mouse or keyboard input.
HTML Canvas Examples
1. Drawing Shapes and Text on Canvas
This is the most basic and reliable example. It draws a rectangle, a circle, a line, and some text.
What this code does: Draws colored shapes and text on the canvas so you can clearly see the result in the preview.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Canvas Drawing Example</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
background: #f5f5f5;
}
canvas {
border: 2px solid #333;
background: white;
display: block;
margin-top: 10px;
}
</style>
</head>
<body>
<h2>HTML Canvas Drawing</h2>
<canvas id="myCanvas" width="400" height="250"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
// Draw blue rectangle
ctx.fillStyle = "#3498db";
ctx.fillRect(30, 30, 120, 80);
// Draw green circle
ctx.beginPath();
ctx.arc(250, 70, 40, 0, 2 * Math.PI);
ctx.fillStyle = "#2ecc71";
ctx.fill();
// Draw orange triangle
ctx.beginPath();
ctx.moveTo(100, 180);
ctx.lineTo(50, 230);
ctx.lineTo(150, 230);
ctx.closePath();
ctx.fillStyle = "#e67e22";
ctx.fill();
// Draw text
ctx.fillStyle = "#2c3e50";
ctx.font = "20px Arial";
ctx.fillText("Hello Canvas!", 200, 200);
</script>
</body>
</html>
2. Simple Moving Rectangle Animation
This example shows a basic animation where a rectangle moves continuously across the canvas.
What this code does: A red rectangle moves from left to right and restarts when it reaches the end.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Canvas Animation Example</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
background: #f5f5f5;
}
canvas {
border: 2px solid #333;
background: #ecf0f1;
display: block;
margin-top: 10px;
}
</style>
</head>
<body>
<h2>Simple Canvas Animation</h2>
<canvas id="animCanvas" width="400" height="150"></canvas>
<script>
var canvas = document.getElementById("animCanvas");
var ctx = canvas.getContext("2d");
var x = 0;
function draw() {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the moving rectangle
ctx.fillStyle = "#e74c3c";
ctx.fillRect(x, 50, 60, 50);
// Move the rectangle
x += 3;
// Reset position when it goes off screen
if (x > canvas.width) {
x = -60;
}
// Continue animation
requestAnimationFrame(draw);
}
// Start the animation
draw();
</script>
</body>
</html>
Additional Tips
Always set the width and height attributes directly on the <canvas> element. Use requestAnimationFrame for smooth animations instead of setInterval.
Web Resources on HTML Canvas
1. MDN Web Docs : Canvas Tutorial
2. University of Washington : HTML Canvas Notes
3. Weber State University : Interactive Web Simulations with HTML5 Canvas
Questions and Answers related to HTML Canvas
The HTML <canvas> element is used to draw graphics, animations, charts, and interactive content using JavaScript. It provides a blank drawing surface that can be controlled programmatically.
First get the canvas element, then call getContext("2d") to obtain the drawing context. Use methods like fillRect(), arc(), fillText(), and path methods (beginPath, moveTo, lineTo) to draw shapes and text.
Use requestAnimationFrame to create a loop. Inside the loop, clear the canvas with clearRect(), update the position of objects, redraw them, and request the next frame. This produces smooth animations.
Setting width and height via CSS only scales the canvas visually and can distort drawings. Setting them as HTML attributes defines the actual drawing buffer size, which is the correct way to control the resolution of the canvas.
Yes. You can use WebGL (by calling getContext("webgl")) for 3D graphics. Libraries like Three.js make working with WebGL much easier.
🧪 Test Your HTML Code
Edit the HTML code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.