Source Code: (back to article)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rectangle Example</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="rectangleCanvas" width="200" height="200"></canvas>
<script>
const canvas = document.getElementById('rectangleCanvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#FF0000'; // Set the fill color to red
ctx.fillRect(20, 20, 150, 100); // Draw the rectangle
</script>
</body>
</html>