HTML Tutorial menu

HTML Canvas

The <canvas> element is a blank drawing area. You draw on it with JavaScript.

The canvas Element

Example

html
<canvas id="board" width="300" height="150"></canvas>

On its own, a canvas is empty and invisible. Always set width and height in the HTML. If you only resize it with CSS, the drawing gets stretched and blurry.

Canvas is used for games, charts, photo editors, signature pads and animations.

Your First Drawing

Drawing always starts the same way: find the canvas, then get its 2D "context". The context is the object that has all the drawing tools.

Example

html
<!DOCTYPE html>
<html>
<body>

<canvas id="board" width="300" height="150" style="border:1px solid #999;"></canvas>

<script>
const canvas = document.getElementById("board");
const ctx = canvas.getContext("2d");

ctx.fillStyle = "#e34c26";
ctx.fillRect(20, 20, 120, 80);   // x, y, width, height

ctx.strokeStyle = "navy";
ctx.lineWidth = 4;
ctx.strokeRect(160, 20, 120, 80);
</script>

</body>
</html>
Try it Yourself »

The left box is filled. The right box is only an outline.

Canvas Coordinates

The top-left corner is (0, 0). x grows to the right and y grows downwards, which is different from maths class.

PointPosition
(0, 0)Top-left corner
(300, 0)Top-right corner of a 300-wide canvas
(0, 150)Bottom-left corner of a 150-high canvas

Main Drawing Tools

CodeDraws
fillRect(x, y, w, h)A filled rectangle
strokeRect(x, y, w, h)A rectangle outline
clearRect(x, y, w, h)Erases an area
beginPath(), moveTo(), lineTo(), stroke()Lines
arc(x, y, r, start, end)Circles and parts of circles
fillText(text, x, y)Text
fillStyle, strokeStyleThe fill and line colours
lineWidth, fontLine thickness and text font

Lines and Circles

A path is a shape you build step by step, and then draw.

Example

html
<!DOCTYPE html>
<html>
<body>

<canvas id="c" width="300" height="160" style="border:1px solid #999;"></canvas>

<script>
const ctx = document.getElementById("c").getContext("2d");

// a line
ctx.beginPath();
ctx.moveTo(20, 140);
ctx.lineTo(140, 20);
ctx.strokeStyle = "green";
ctx.lineWidth = 3;
ctx.stroke();

// a circle: centre (220, 80), radius 50
ctx.beginPath();
ctx.arc(220, 80, 50, 0, 2 * Math.PI);
ctx.fillStyle = "gold";
ctx.fill();
ctx.stroke();
</script>

</body>
</html>
Try it Yourself »

Angles in arc() are in radians, not degrees. A full circle is 2 * Math.PI, half a circle is Math.PI.

Drawing Text

Example

html
<!DOCTYPE html>
<html>
<body>

<canvas id="c" width="320" height="100" style="border:1px solid #999;"></canvas>

<script>
const ctx = document.getElementById("c").getContext("2d");
ctx.font = "bold 28px Arial";
ctx.fillStyle = "#e34c26";
ctx.fillText("Workownix", 20, 45);

ctx.font = "18px Arial";
ctx.fillStyle = "#333";
ctx.fillText("Certificate of Completion", 20, 80);
</script>

</body>
</html>
Try it Yourself »

A Simple Bar Chart

Here is a small chart of a shop's sales for four days. The bars are drawn in a loop.

Example

html
<!DOCTYPE html>
<html>
<body>

<h3>Laptops sold this week</h3>
<canvas id="chart" width="320" height="200" style="border:1px solid #ccc;"></canvas>

<script>
const ctx = document.getElementById("chart").getContext("2d");
const days = ["Mon", "Tue", "Wed", "Thu"];
const sales = [5, 9, 3, 7];

ctx.font = "14px Arial";
for (let i = 0; i < sales.length; i++) {
  const x = 30 + i * 70;
  const height = sales[i] * 15;
  const y = 170 - height;
  ctx.fillStyle = "#2a7ae2";
  ctx.fillRect(x, y, 40, height);
  ctx.fillStyle = "#000";
  ctx.fillText(days[i], x + 5, 190);
  ctx.fillText(sales[i], x + 14, y - 5);
}
</script>

</body>
</html>
Try it Yourself »

Canvas and Accessibility

A canvas is just pixels. Screen readers cannot read what you drew. Put a short text description between the canvas tags, and show important numbers as text or a table as well.

Example

html
<canvas id="chart" width="320" height="200">
  Sales chart: Monday 5, Tuesday 9, Wednesday 3, Thursday 7 laptops.
</canvas>

For charts on real websites, ready-made libraries like Chart.js draw on a canvas for you. Learning the basics here helps you understand them.

Exercise

Exercise

What must you call before you can draw on a canvas?

Learn HTML with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp