</> Code Corner

HTML · HTML Graphics · Lesson 45

HTML Canvas

Draw graphics with JavaScript.

The <canvas> element is a container for graphics drawn with JavaScript. You can draw paths, boxes, circles, text and images.

Example

<canvas id="myCanvas" width="250" height="120" style="border:1px solid #000;"></canvas>
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.fillStyle = "#04AA6D";
ctx.fillRect(10, 10, 100, 60);
ctx.beginPath();
ctx.arc(180, 60, 40, 0, 2 * Math.PI);
ctx.stroke();
ctx.font = "16px Arial";
ctx.fillStyle = "black";
ctx.fillText("Canvas!", 20, 100);
</script>
Tip: A canvas has no border and no content by default — you draw everything with JavaScript.

Keep reading “HTML Canvas”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋