Aral Code

Graphics & Scripts Lesson 37 of 42

HTML Canvas — Drawing with JavaScript

Use the canvas element and a few lines of JavaScript to draw shapes, text, gradients and a simple bar chart of class grades.

The <canvas> element is a blank drawing board on your page. Unlike SVG, where you write each shape as an HTML element, canvas is painted with JavaScript. It's what many browser games, photo editors and charts use behind the scenes.

Creating a canvas

By itself, a canvas is just an empty, see-through rectangle. Give it an id (so JavaScript can find it), a width, and a height:

Example An empty canvas with a border
<canvas id="board" width="300" height="120" style="border: 2px dashed #999;">
  Your browser does not support canvas.
</canvas>

The text between the tags only shows in very old browsers that don't support canvas.

Drawing a rectangle

To draw, you first get the canvas's context — think of it as the paintbrush. Then you set colors and call drawing commands.

Example Filled and outlined rectangles
<canvas id="c" width="320" height="140" style="border: 1px solid #ccc;"></canvas>

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

  ctx.fillStyle = "#0038A8";          // paint color
  ctx.fillRect(20, 20, 120, 80);       // x, y, width, height

  ctx.strokeStyle = "#CE1126";        // outline color
  ctx.lineWidth = 6;
  ctx.strokeRect(170, 20, 120, 80);
</script>

Like SVG, the canvas grid starts at the top-left corner (0, 0), and y grows downward. fillRect paints a solid rectangle; strokeRect draws only its outline.

Lines and paths

For lines and other shapes, you "trace a path" and then paint it. It works like drawing with a pen: lift the pen and move it (moveTo), drag it to a point (lineTo), then ink it (stroke) or fill it (fill).

Example A house drawn with paths
<canvas id="c" width="260" height="210"></canvas>
<script>
  const ctx = document.getElementById("c").getContext("2d");

  // walls
  ctx.fillStyle = "#f2c46d";
  ctx.fillRect(50, 100, 160, 100);

  // roof (a triangle)
  ctx.beginPath();
  ctx.moveTo(35, 100);
  ctx.lineTo(130, 25);
  ctx.lineTo(225, 100);
  ctx.closePath();
  ctx.fillStyle = "#b4372b";
  ctx.fill();

  // door
  ctx.fillStyle = "#5b3a1a";
  ctx.fillRect(112, 140, 36, 60);
</script>

Circles

Circles are drawn with arc(x, y, radius, startAngle, endAngle). The angles are in radians. A full circle goes from 0 to 2 * Math.PI, which is the same as 360°.

Example Circles with arc
<canvas id="c" width="300" height="130"></canvas>
<script>
  const ctx = document.getElementById("c").getContext("2d");
  const colors = ["#0038A8", "#CE1126", "#FCD116"];

  for (let i = 0; i < 3; i++) {
    ctx.beginPath();
    ctx.arc(60 + i * 90, 65, 40, 0, 2 * Math.PI);
    ctx.fillStyle = colors[i];
    ctx.fill();
  }
</script>

That example uses a loop to draw three circles. Each time, i is 0, then 1, then 2, so the x position moves 90 pixels to the right. This is where canvas gets powerful: code can draw hundreds of shapes in a split second.

Text and gradients

Example Text with a gradient background
<canvas id="c" width="340" height="120"></canvas>
<script>
  const ctx = document.getElementById("c").getContext("2d");

  const grad = ctx.createLinearGradient(0, 0, 340, 0);
  grad.addColorStop(0, "#0f5c4d");
  grad.addColorStop(1, "#7fd1b9");
  ctx.fillStyle = grad;
  ctx.fillRect(0, 0, 340, 120);

  ctx.font = "bold 30px Arial";
  ctx.fillStyle = "white";
  ctx.fillText("Congrats, Grade 10!", 20, 70);
</script>

Project: a bar chart of class grades

Here's a real use. Suppose your section's average grades per subject are stored in a list. A loop draws one bar per subject, with the bar's height based on the grade:

Example A bar chart from data
<canvas id="chart" width="360" height="250" style="background: #fafafa;"></canvas>
<script>
  const ctx = document.getElementById("chart").getContext("2d");
  const subjects = ["Math", "Sci", "Eng", "Fil", "ICT"];
  const grades = [86, 82, 90, 88, 94];

  ctx.font = "13px Arial";
  for (let i = 0; i < grades.length; i++) {
    const barHeight = (grades[i] - 60) * 5;   // 60 at the bottom, 100 at the top
    const x = 30 + i * 65;
    const y = 220 - barHeight;

    ctx.fillStyle = grades[i] >= 90 ? "#1d7a43" : "#2f6fd6";
    ctx.fillRect(x, y, 40, barHeight);

    ctx.fillStyle = "#333";
    ctx.fillText(grades[i], x + 10, y - 6);
    ctx.fillText(subjects[i], x + 6, 240);
  }
</script>

Change a number in grades and the chart redraws. Grades of 90 and up are drawn green, the rest blue. That's done by the ? : part, which means "if the grade is at least 90, use green, otherwise use blue."

Canvas or SVG?

SVGCanvas
How you drawHTML-like elementsJavaScript commands
ZoomingAlways sharpCan get blurry (it's pixels)
Best forLogos, icons, diagramsGames, animations, photo effects, many moving objects
Changing one shape laterEasy, it's an elementRedraw the picture

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What do you need before you can draw on a canvas?
2. What does ctx.fillRect(10, 20, 100, 50) draw?
3. Which is better for an animated browser game with many moving objects?
4. How do you draw a full circle with arc?

By Sir Reyel · Last updated 2026-12-04