Canvas coordinate system

Canvas drawing uses a coordinate system whose origin (0, 0) is at the top-left corner.

What you will learn

The basic rule

// x = 20, y = 10, width = 120, height = 60
context.fillRect(20, 10, 120, 60);

The rectangle starts 20 pixels from the left and 10 pixels from the top. Its right edge is at x 140, and its bottom edge is at y 70.

Canvas size matters

The width and height attributes define the drawing buffer. CSS can change how that buffer is displayed, but stretching it without considering the ratio can make the drawing blurry or distorted.

Common mistakes