Chapter 52: Canvas Compositing

Canvas Compositing (also called globalCompositeOperation) in the clearest, most patient, step-by-step way possible.

Imagine I’m sitting right next to you with my laptop 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 compositing feels completely natural and easy to use.

Canvas Compositing – From Zero to Confident

1. The single most important sentence about compositing

Compositing controls what happens when you draw something new on top of pixels that are already on the canvas.

Normally, when you draw a shape or image, the new pixels replace the old ones (or blend with alpha transparency). But with globalCompositeOperation you can change that rule — you can tell Canvas:

  • “Only draw where there is already something”
  • “Only draw where there is nothing”
  • “Subtract colors”
  • “Keep the brightest parts”
  • “Use the new shape as a mask”
  • etc.

This is one of the most powerful (and most underused) features in Canvas — it lets you do things that normally require very complex math or external image editors.

2. The property that controls everything: globalCompositeOperation

You set it on the context:

JavaScript

It stays set until you change it again or restore() after save().

There are ~26 possible values in the Canvas spec, but you will use these 8 most often (the “Porter-Duff” operations + a few extras):

Value What happens when new shape is drawn on top of existing pixels Visual intuition Most common real use case
source-over Normal: new pixels on top (default) New paint covers old paint Everyday drawing (most default use)
destination-over Old pixels on top — new shape is drawn behind existing New paint goes under old paint Draw background after foreground
source-in Only keep new shape where it overlaps existing pixels New shape becomes a mask for old content Fill inside existing shape
source-out Only keep new shape where it does NOT overlap existing Cut holes in existing content Erase / punch holes
destination-in Only keep old pixels where new shape overlaps them New shape acts as a mask for old content Clip / mask existing drawing
destination-out Keep old pixels only where new shape does NOT overlap New shape punches transparent holes Erase parts of existing drawing
lighter Add colors together (like screen blend) Bright areas get brighter Glows, light effects, particle systems
xor Keep pixels that are in one shape but not both Creates “cut-out” / XOR effect Interesting patterns, debug visualizations

Quick rule of thumb (memorize this table):

  • Want normal painting? → ‘source-over’ (you almost never need to set it)
  • Want to mask / clip with a shape? → ‘destination-in’
  • Want to erase / punch holes? → ‘destination-out’ or ‘source-out’
  • Want glowing / additive light? → ‘lighter’

3. Minimal complete compositing example (copy → run)

HTML

What you should see & remember forever:

  • source-over → yellow rectangle covers red circle & blue square (normal painting)
  • destination-in → only the part of red/blue where yellow overlaps remains → yellow acts as a mask
  • destination-out → yellow punches transparent holes in red/blue → yellow acts as an eraser
  • lighter → yellow adds to whatever is below → creates glowing/brightening effect

4. Very common real-world patterns

Pattern 1 – Circular mask / avatar

JavaScript

Pattern 2 – Erase / punch hole

JavaScript

Pattern 3 – Glow / additive light

JavaScript

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

Task 1 – Circular avatar Draw a rectangle filled with any photo/image Clip it to a perfect circle Add a thick white stroke around the circle

Task 2 – Punch hole Fill canvas with green Punch a large transparent circle in the center using destination-out

Task 3 – Glowing orb Draw a purple circle Add a large white radial gradient circle using lighter compositing → create soft glow

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 clip() is different from globalCompositeOperation?
  • Difference between destination-in vs source-in?
  • How destination-out actually erases?
  • When to use lighter vs normal blending?
  • Why we mustsave() / restore() with compositing?
  • Something else?

Tell me — we’ll stay on compositing until you feel super confident using it to mask, erase, glow, or blend anything you draw.

You’re doing really well — compositing is one of the most powerful (and most “wow” inducing) features in Canvas! 🚀

You may also like...

Leave a Reply

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