Junior JavaScript

What is the purpose of the <canvas> element?

The <canvas> element is used to draw graphics, animations, charts, or games using

JavaScript.

Example:

<canvas id="myCanvas" width="200" height="100"></canvas>

<script>

const canvas = document.getElementById("myCanvas");

const ctx = canvas.getContext("2d");

ctx.fillStyle = "blue";

ctx.fillRect(10, 10, 180, 80);

</script>

Follow me on LinkedIn:

Key Takeaway:

<canvas> gives you a pixel-based drawing area inside HTML — perfect for dynamic

graphics.

More from JavaScript Tutorial

All questions for this course