mc0de
Advanced HTML · Lesson 05 of 13

HTML Canvas

Ready to draw with code? Discover how HTML Canvas and JavaScript work together to create shapes, colorful artwork, text, and interactive graphics right inside your browser.

Canvas APIJavaScript 2D GraphicsHands-On Practice
● ● ●   canvas.html
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#4ee4a1";
ctx.beginPath();
ctx.arc(80, 55, 30, 0, Math.PI * 2);
ctx.fill();
01 / Getting Started

What If You Could Draw With Code?

Imagine creating a game, drawing colorful shapes, or designing an animated scene — all inside your browser. That’s what HTML Canvas makes possible!

The <canvas> element creates a drawing surface. JavaScript tells the browser what to draw on that surface.

Remember: Canvas is a bitmap drawing surface. Unlike ordinary HTML elements, individual shapes drawn on a canvas don’t automatically become separate interactive or accessible elements.
02 / Your First Canvas

Meet the <canvas> Element

First, create a canvas in HTML. Then select it with JavaScript and request its 2D drawing context.

Your first canvas
<canvas id="myCanvas" width="400" height="200">
  Your browser does not support canvas.
</canvas>

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

  ctx.fillStyle = "#39d5ed";
  ctx.fillRect(30, 30, 150, 80);
</script>

getContext("2d") gives you access to the 2D drawing tools. The fillRect() method draws a filled rectangle.

Important: Set the canvas’s drawing dimensions with its HTML width and height attributes. CSS can resize the display, but it doesn’t change the internal drawing coordinate system.
03 / Drawing Shapes

Rectangles, Circles & Lines

Canvas uses coordinates. The top-left corner is (0, 0), and values increase as you move right or down.

Three common shapes
// Filled rectangle
ctx.fillStyle = "#39d5ed";
ctx.fillRect(20, 20, 120, 70);

// Circle
ctx.beginPath();
ctx.arc(220, 60, 35, 0, Math.PI * 2);
ctx.fillStyle = "#4ee4a1";
ctx.fill();

// Line
ctx.beginPath();
ctx.moveTo(20, 140);
ctx.lineTo(300, 140);
ctx.strokeStyle = "#f59e0b";
ctx.lineWidth = 5;
ctx.stroke();

arc() draws a circular arc using a center point, radius, and starting and ending angles. A full circle runs from 0 to Math.PI * 2.

04 / Colors & Styles

Bring Your Graphics to Life

Canvas supports colors, outlines, transparency, and gradients. You can make a drawing as simple or detailed as you like.

Fill, stroke, and gradient
ctx.fillStyle = "#4ee4a1";
ctx.fillRect(20, 20, 100, 60);

ctx.strokeStyle = "#39d5ed";
ctx.lineWidth = 4;
ctx.strokeRect(150, 20, 100, 60);

const gradient = ctx.createLinearGradient(0, 100, 300, 100);
gradient.addColorStop(0, "#39d5ed");
gradient.addColorStop(1, "#4ee4a1");

ctx.fillStyle = gradient;
ctx.fillRect(20, 100, 230, 50);

Use fillStyle for filled shapes and strokeStyle for outlines. Gradients help you create smooth transitions between colors.

05 / Adding Text

Write on Your Canvas

Canvas isn’t limited to shapes. You can draw text too!

Canvas text
ctx.font = "bold 28px Arial";
ctx.fillStyle = "#176b76";
ctx.fillText("Hello, mC0de!", 30, 60);

ctx.font = "16px Arial";
ctx.fillStyle = "#475569";
ctx.fillText("I made this with JavaScript!", 30, 95);

The font property controls the text appearance, and fillText() draws text at the specified coordinates.

Accessibility tip: If your canvas contains important information, provide equivalent text or another accessible representation outside the drawing.
06 / Clearing & Redrawing

Change Your Drawing Anytime

Unlike HTML elements, canvas shapes aren’t automatically remembered as individual objects. To update a scene, you usually clear part or all of the canvas and draw the new scene.

Clear and redraw
ctx.clearRect(0, 0, canvas.width, canvas.height);

ctx.fillStyle = "#0f172a";
ctx.fillRect(0, 0, canvas.width, canvas.height);

ctx.fillStyle = "#4ee4a1";
ctx.beginPath();
ctx.arc(120, 80, 35, 0, Math.PI * 2);
ctx.fill();

For animation, developers often redraw the scene using requestAnimationFrame(). We’ll explore more JavaScript techniques in future lessons.

07 / Best Practices

Draw Smarter, Not Harder

  • Use descriptive canvas IDs and organized JavaScript.
  • Understand the coordinate system before drawing.
  • Use beginPath() when starting a new path.
  • Set appropriate drawing dimensions to avoid distortion.
  • Keep important information accessible outside the canvas.
  • Use SVG instead when you need individually accessible, scalable vector elements.

Canvas is especially powerful for games, visualizations, animations, and freeform drawing.

🚀 mC0de Challenge

Draw Your Own Coding Landscape

It’s time to create a scene using nothing but HTML Canvas and JavaScript. Think of it as painting with code!

Your Mission

  1. Create a canvas that’s 500 pixels wide and 300 pixels tall.
  2. Draw a sky using a filled rectangle.
  3. Add a sun or moon using arc().
  4. Draw at least two other shapes.
  5. Add a title using fillText().
  6. Experiment with your own colors and coordinates.
Extra challenge: Add mountains, stars, or a gradient sky. Can you create a sunset scene entirely with code?
💻 Interactive HTML Sandbox

Paint With Code!

Change the colors, coordinates, and shapes in the editor. Click Run Code to see your drawing appear. Your artwork stays right here in the browser.

Live Result
Lesson 05 Recap

What You’ve Learned

You’re Officially Drawing With Code!

You’ve learned how to turn a blank canvas into your own graphics using JavaScript.

  • How to create and access an HTML canvas.
  • How to draw rectangles, circles, and lines.
  • How to use colors, outlines, and gradients.
  • How to add text to a drawing.
  • How to clear and redraw a canvas.

Next up: SVG — discover another powerful way to create sharp, scalable graphics on the web!

Your Learning Journey

0 / 13 Completed

Keep going! Every completed lesson brings you closer to mastering Advanced HTML.

🎉

Nice Work!

You've learned how to draw shapes, work with coordinates, add color and text, and build a creative scene using HTML Canvas and JavaScript. Your coding skills are getting more visual!