Canvas API in JavaScript
Draw an attendance bar in JavaScript with a 2D canvas, fillRect, and fillText. Map 24 students to 240 pixels; canvas charts also need a readable text summary.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<canvas id="chart" width="320" height="180">A blue attendance bar represents 24 students.</canvas>7<script>8const canvas = document.querySelector('#chart');9const ctx = canvas.getContext('2d');10if (ctx) {11 ctx.fillStyle = '#ffffff';12 ctx.fillRect(0, 0, canvas.width, canvas.height);13 ctx.fillStyle = '#145cba';14 ctx.fillRect(30, 50, 240, 40); // 24 students at 10 pixels each15 ctx.fillStyle = '#111111';16 ctx.font = '16px sans-serif';17 ctx.fillText('Present: 24 students', 30, 120);18}19</script>20</body>21</html>
Sample Output
Blue bar at (30, 50), width 240, height 40
The bar represents 24 students at ten pixels each. Coordinates start at the top left.
Present: 24 students
fillText draws the label at x 30 and y 120.
In this one
- Create a fixed-size drawing area
- Draw a rectangle with coordinates
- Change colors before drawing
- Add a text label to a chart
How it works
- 1
Set width 320 and height 180 on the canvas element.
- 2
Get a 2D drawing context and check that it exists.
- 3
Paint the background white.
- 4
Draw a blue 240-by-40 bar at x 30 and y 50.
- 5
Draw the attendance label below the bar; no console lines are expected.
Edge cases
CSS-only resizing
Changing only the displayed size can stretch or blur the drawing.
Set the canvas width and height attributes for its drawing coordinates. Handle high-density screens separately in a larger app.
Changing canvas dimensions later
Setting width or height clears the drawing and resets context settings.
Resize before drawing, or redraw the chart after changing those attributes.
What's going on
The Canvas API draws shapes and text inside a canvas element. A canvas is a rectangular drawing area on the page. This example turns an attendance count into a simple blue bar, so students can connect a number with a visual size.
The HTML sets the drawing area's width and height. getContext('2d') returns the tool used for flat drawing. The code paints a white background, chooses blue, and fills a rectangle. Its four arguments are the starting x position, starting y position, width, and height. Here 24 students use 240 pixels, or ten pixels each.
Canvas coordinates start at the top-left corner. Moving right increases x; moving down increases y. fillStyle affects later drawing, not shapes already painted. Canvas pixels do not become ordinary page text, so a real chart should also provide an accessible text summary outside the canvas. Drawing is visible on the page, not printed as console output.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is the third fillRect argument?
Pick an answer to check it.
Question 2
What happens to y when a shape moves down?
Pick an answer to check it.
Questions people ask
Why is y 50 near the top?
Canvas measures y downward from the top-left corner, unlike some school graph diagrams.
Why change fillStyle twice?
The bar and label need different colors. Each setting applies to the drawing commands that follow it.
Will console.log show the chart?
No. Canvas draws on the page. Open the saved HTML to see the picture.