Cyber Code Academy
Tutorials Academy Courses
Home / html-tutorials-md / HTML Canvas Graphics

HTML Canvas Graphics

> Understand the canvas element, drawing context, coordinates, shapes, text, and the role of JavaScript in creating graphics.

**Cyber Code Academy · HTML Tutorial · Lesson 37 · Intermediate**

## 1. What is HTML Canvas?
The canvas element creates a drawable bitmap area inside a web page. JavaScript uses a rendering context to draw pixels, shapes, text, and images on that area.

Canvas is useful for charts, games, diagrams, and custom visualizations. The drawing itself is not represented as separate semantic HTML elements, so provide accessible alternatives where needed.

## 2. Create a canvas element

“`html

Your browser does not support canvas.

“`

The width and height attributes set the canvas drawing surface dimensions. CSS can control how the canvas is displayed, but stretching it with CSS alone may distort the drawing.

## 3. Get the 2D rendering context

“`html

const canvas = document.querySelector(“#myCanvas”);
const ctx = canvas.getContext(“2d”);

“`

The 2D context provides methods and properties for drawing. If the browser cannot provide the requested context, getContext may return null, so robust applications should handle that possibility.

## 4. Draw a rectangle

“`javascript
ctx.fillStyle = “#2563eb”;
ctx.fillRect(40, 40, 180, 100);
“`

fillRect(x, y, width, height) paints a filled rectangle. The coordinate origin (0,0) begins at the top-left of the canvas.

## 5. Draw a line and outline

“`javascript
ctx.beginPath();
ctx.moveTo(40, 180);
ctx.lineTo(220, 180);
ctx.lineWidth = 4;
ctx.strokeStyle = “#16a34a”;
ctx.stroke();
“`

beginPath starts a new path. moveTo sets the starting point and lineTo adds a segment. stroke draws the path outline.

## 6. Draw a circle

“`javascript
ctx.beginPath();
ctx.arc(320, 100, 50, 0, Math.PI * 2);
ctx.fillStyle = “#f59e0b”;
ctx.fill();
“`

arc draws part or all of a circle. The parameters include the centre, radius, start angle, and end angle in radians.

## 7. Draw text

“`javascript
ctx.font = “24px sans-serif”;
ctx.fillStyle = “#111827”;
ctx.fillText(“Hello Canvas”, 40, 250);
“`

fillText draws text at the provided coordinates. Canvas text is bitmap drawing, not normal selectable HTML text.

## 8. Accessibility and fallback
Canvas content is not automatically available as semantic text. Provide useful fallback content and a visible equivalent description or data table when the graphic communicates important information.

“`html

A chart showing sales growth from January to June.

Sales growth from January through June.


Sales data represented in the chart

“`

## 9. Common mistakes
– Forgetting width and height.
– Trying to draw before the canvas element is available.
– Confusing CSS dimensions with drawing-surface dimensions.
– Not handling a missing 2D context.
– Making important chart information available only as pixels.
– Forgetting to scale drawing for high-density displays in advanced projects.

## 10. Practice exercises
1. Draw a blue rectangle.
2. Draw a green line.
3. Draw a circle using arc.
4. Add a text label.
5. Provide fallback content and an equivalent table for a simple chart.

## 11. Knowledge check
1. What does canvas create?
2. Which method gets the 2D context?
3. Where is the canvas coordinate origin?
4. What does fillRect do?
5. Why does canvas need an accessible alternative?

**Next lesson:** HTML SVG Graphics.

Want to learn this professionally?

Explore practical IT training at Cyber Code Academy.

View Academy Courses