Chapter 49: Canvas Images

Canvas Images (how to load, draw, position, scale, clip, rotate, and animate images in <canvas>) in the clearest, most patient, step-by-step way possible.

Imagine I’m sitting right next to you with my screen open. We’re going to build every concept together — slowly, with tiny complete runnable examples you can copy-paste immediately. No skipping steps, no magic, just plain English explanations, visual thinking, common beginner traps, and repeated patterns until drawing and controlling images on Canvas feels completely natural and easy.

Canvas Images – From Zero to Confident

1. The single most important sentence about images in Canvas

Images in Canvas are not automatically displayed like <img> tags. You have to:

  1. Load the image (create an Image object and wait for it to finish loading)
  2. Draw it manually with ctx.drawImage() whenever you want it to appear
  3. Redraw it every frame if you want to move, scale, rotate, or animate it

Once drawn, the image becomes just pixels on the canvas bitmap — there is no “image object” you can later say “move this photo 50px right”. You have to erase the old pixels (usually with clearRect) and draw the image again at the new position/scale/rotation.

2. The only command you need to draw images: drawImage()

There are five main overloads of drawImage() — but you will use these three most often:

Overload (most common first) What it does When to use it
drawImage(img, dx, dy) Draw full image at position (dx, dy) with its natural size Quick “paste image here”
drawImage(img, dx, dy, dWidth, dHeight) Draw image at (dx, dy) and force it to this size (scales) Resize / stretch image
drawImage(img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) Crop part of source image (sx,sy → sWidth,sHeight) and draw it at (dx,dy) with size dWidth×dHeight Sprite sheets, cropping, zooming in

Important rule: drawImage() does NOT wait for the image to load. If you call it before the image is ready → nothing appears (silent failure).

You must wait for img.onload before drawing.

3. Minimal complete image example (copy → run)

HTML

What you should see & remember forever:

  • Image appears only afteronload fires
  • drawImage(img, dx, dy) → natural size at position
  • drawImage(img, dx, dy, dW, dH) → forced size (can stretch/distort)
  • 9-argument version → crop source + resize destination (most powerful)

4. Common real-world patterns you’ll reuse

Pattern 1 – Center image perfectly

JavaScript

Pattern 2 – Scale to fit canvas while keeping aspect ratio

JavaScript

Pattern 3 – Sprite sheet frame (crop one frame)

JavaScript

5. Your three tiny practice tasks (10–15 min each)

Task 1 – Centered photo Load any online image Draw it perfectly centered (natural size or scaled to fit)

Task 2 – Photo frame Draw a thick black rounded rectangle Draw the same image inside it (scaled + centered)

Task 3 – Crop zoom Draw the same image three times:

  • full image scaled small
  • cropped center 50% of image scaled larger
  • cropped top-left quarter scaled even larger

Paste any of them here when you finish — I’ll review it like we’re pair-programming together.

Which part still feels a little slippery?

  • Why we must wait for img.onload?
  • Difference between 3-arg vs 5-arg vs 9-arg drawImage?
  • How to center an image perfectly?
  • Keeping aspect ratio when scaling?
  • Cropping from sprite sheets?
  • Something else?

Tell me — we’ll stay on images until you feel super confident loading, positioning, scaling, and cropping any picture you want.

You’re doing really well — images are one of the most powerful things you can do in Canvas! 🚀

You may also like...

Leave a Reply

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