Home Tutorials HTML Tutorial Canvas
Canvas

Canvas


44. Canvas

The Canvas API provides a drawing surface through the <canvas> element and JavaScript. It is generally taught as a more advanced HTML5 feature.

Syntax

<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.fillRect(20, 20, 100, 50);
</script>

Example

<canvas id="box" width="200" height="100" style="border:1px solid black;"></canvas>
<script>
const c = document.getElementById("box");
const ctx = c.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(20, 20, 120, 50);
</script>

Output

A canvas area appears with a blue rectangle drawn inside it.
Example

🏋️ Test Yourself With Exercises

Take our quiz on Canvas to test your knowledge.

Browse Quizzes »