CHAPTER 49 · HTML
Canvas drawing: use a script and keep a text equivalent
Create a small bitmap drawing and a button-driven comparison. Understand the drawing surface, coordinates and the limits of fallback content.
Download example files (ZIP)An empty drawing sheet on the page
Width and height define the bitmap
Ask for a drawing context
Read each rectangle command
Redrawing means issuing commands again
Do not rely only on canvas fallback text
A picture still needs a truthful explanation
Canvas and SVG solve different problems
Follow the idea
- 1Canvas: the bitmap surface
- 2Context: the drawing interface
- 3Commands: paint or clear pixels
- 4HTML: keep the important values readable
Canvas fallback is not a universal script-failure message.
Complete program: canvas-rectangles.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Draw on a Canvas</title>
<style>
canvas {
max-width: 100%;
height: auto;
border: 1px solid #777;
}
</style>
</head>
<body>
<h1>Paint two practice rectangles</h1>
<main>
<p id="drawing-description">
A blue rectangle sits to the left of a taller orange rectangle. The colours carry no
ranking.
</p>
<canvas
id="practice-canvas"
width="320"
height="160"
role="img"
aria-labelledby="drawing-description"
>Two rectangles: a blue one on the left and a taller orange one on the right.</canvas
>
<p>The drawing is created by JavaScript when this saved page runs.</p>
<script>
const canvas = document.getElementById('practice-canvas');
const context = canvas.getContext('2d');
if (context) {
context.fillStyle = '#2458a6';
context.fillRect(25, 45, 110, 60);
context.fillStyle = '#b85c15';
context.fillRect(190, 25, 80, 110);
}
</script>
</main>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. JavaScript does not run in this preview. Save the complete example and any named script file together to test its actions or canvas drawing in your browser.
Complete program: canvas-comparison.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A Canvas with a Text Equivalent</title>
<style>
canvas {
max-width: 100%;
height: auto;
border: 1px solid #777;
}
</style>
</head>
<body>
<h1>Draw a fictional item comparison</h1>
<main>
<p id="comparison-description">
Group A has 3 items and Group B has 5 items. One item is drawn as 30 horizontal canvas
units.
</p>
<ul>
<li>Group A: 3 items</li>
<li>Group B: 5 items</li>
</ul>
<button type="button" id="draw-comparison">Draw the comparison</button>
<p id="drawing-state" aria-live="polite">
Activate the button to draw the bars in the saved page.
</p>
<canvas
id="comparison-canvas"
width="320"
height="180"
role="img"
aria-labelledby="comparison-description"
>Group A has 3 items. Group B has 5 items.</canvas
>
<script>
const canvas = document.getElementById('comparison-canvas');
const context = canvas.getContext('2d');
const state = document.getElementById('drawing-state');
document.getElementById('draw-comparison').addEventListener('click', function () {
if (context) {
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = '#2458a6';
context.fillRect(25, 35, 90, 35);
context.fillStyle = '#b85c15';
context.fillRect(25, 110, 150, 35);
state.textContent = 'Drawing shown. Group A has 3 items; Group B has 5 items.';
} else {
state.textContent = 'Drawing is unavailable. Read the item counts above.';
}
});
</script>
</main>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. JavaScript does not run in this preview. Save the complete example and any named script file together to test its actions or canvas drawing in your browser.
Problem and solution
Common mistakes and useful tips
Do not expect canvas markup to draw by itself. Separate bitmap dimensions from CSS display size. Check getContext before using it. Changing dimensions clears the bitmap and drawing state. Keep essential values in ordinary HTML too. Do not describe static fictional bars as live measurements.
Practice questions
What does canvas provide before drawing commands run? What are the arguments of fillRect? Does changing fillStyle recolour existing rectangles? How wide is the Group B bar and why? Will inner canvas text necessarily appear when a script fails? Save the comparison program, activate it twice and inspect the text and bars. What stays the same?
Check your practice answers
A drawing surface, initially unpainted. X position, y position, width and height. No. It affects subsequent fills. 150 drawing units: 5 items times 30 units per item. No. Keep important content outside the canvas. The counts remain 3 and 5; the bars are cleared and redrawn with the same widths. No new data is fetched or counted.
Interview questions
How does canvas differ from SVG?
Why set width and height attributes?
What happens when canvas dimensions are reset?
Why keep chart values in HTML?
Quick revision
Create the surface, then draw. Read coordinates and dimensions carefully. Keep text equivalents outside canvas. Test saved scripts and avoid false live-data claims.