Canvas: fillRect()

The fillRect(x, y, width, height) method draws a filled rectangle on a 2D canvas.

What you will learn

Basic example

const context = canvas.getContext('2d'); context.fillStyle = '#2463eb'; context.fillRect(20, 10, 120, 60);

The first two values are the top-left position. The last two are width and height. The rectangle is filled using the current fillStyle.

Common mistakes