How to fill the whole canvas with a specific color in JavaScript?

Estimated read time 1 min read

To fill the whole canvas with a specific color in JavaScript, you’ll need to use the Canvas API. Here’s an example:

<canvas id="myCanvas"></canvas>

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

ctx.fillStyle = "red";
ctx.fillRect(0, 0, canvas.width, canvas.height);
</script>

In this example, the canvas element is first selected and its 2D rendering context is obtained. The fillStyle property is set to the desired color and the fillRect method is used to fill the entire canvas with that color.

This is just a basic example and you can customize it further by changing the size and color as per your needs.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply