Chapter 35: Canvas Coordinates

1. What are “Canvas Coordinates” actually?

Canvas coordinates are the address system (like a map grid) that tells JavaScript exactly where to draw something on the <canvas> element.

Think of the canvas like a big piece of graph paper:

  • Top-left corner = (0, 0)
  • X increases to the right
  • Y increases downwards (not upwards like school math graph paper)

This is very different from normal math coordinates (where Y goes up) — it’s the same as how computer screens, photos, and almost all web graphics work.

So remember forever:

  • (0, 0) = top-left corner of the canvas
  • Positive X = right
  • Positive Y = down
  • Canvas.width = rightmost X
  • Canvas.height = bottommost Y

2. Visual Map of Canvas Coordinates (Text Diagram)

Imagine a 400 × 300 canvas:

text
  • Top-left pixel = (0, 0)
  • Top-right pixel = (399, 0) ← width-1
  • Bottom-left pixel = (0, 299) ← height-1
  • Bottom-right pixel = (399, 299)
  • Exact center = (200, 150)

3. Your First Coordinate Example – Draw Points Everywhere (Copy-Paste & Run)

Create canvas-coordinates.html and paste this:

HTML

What you see:

  • Light grid showing coordinates every 50 pixels
  • Red dots + labels at corners and center
  • Arrow from top-left to center
  • Text explaining “Y increases DOWN” and “X increases RIGHT”

This is the most important visual every student should see once — it fixes the #1 confusion forever.

4. How Coordinates Work in Different Commands

A. fillRect(x, y, width, height)

JavaScript

B. arc(cx, cy, radius, startAngle, endAngle)

JavaScript

C. lineTo(x, y) & moveTo(x, y)

JavaScript

D. Text position

JavaScript

5. Very Important Coordinate Concepts (Exam & Project Favorites)

A. Canvas (0,0) is always top-left — never changes

No matter how big/small the canvas, (0,0) = top-left pixel.

B. Canvas size = pixel size

If you set <canvas width=”600″ height=”400″>, the drawing surface has exactly 600×400 pixels. CSS width/height only changes display size — drawing still happens in 600×400 pixels → can look blurry if CSS size ≠ attribute size.

Wrong way (blurry):

HTML

Right way (sharp):

HTML

C. Responsive canvas (very common question)

To make canvas responsive:

JavaScript

6. Teacher’s Quick Tips (Hyderabad Student Style)

  • Always remember: Y goes down — opposite of school graphs
  • Center of canvas = canvas.width / 2, canvas.height / 2
  • Use ctx.save() / ctx.restore() when translating/rotating — prevents messing up later drawings
  • Common mistake #1: Set CSS width/height instead of canvas.width/height → blurry output
  • Common mistake #2: Forget beginPath() before new shape → connects to previous line

Understood Canvas Coordinates fully now? This is the foundation of all canvas drawing — get this right and everything else becomes easy.

Tell me what you want next — we can build on this:

  • Full bouncing ball animation using coordinates?
  • Mouse coordinates demo (click shows x,y)?
  • Grid-based game board (chess/tictactoe style)?
  • Clock face with hour/minute hand using arc + rotate?
  • Or 15-question Canvas coordinates quiz?

Just say — we keep going step by step together! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *