Code

Canvas

Compositing Modes

IntermediateglobalCompositeOperation — all 16 blend and compositing modes. Blue square = destination; orange circle = source. MDN Reference

const ops: GlobalCompositeOperation[] = [
  'source-over', 'source-in',
  'source-out',  'source-atop',
  'destination-over', 'destination-in',
  'destination-out',  'destination-atop',
  'lighter', 'copy', 'xor',
  'multiply', 'screen', 'overlay',
  'darken',  'lighten',
];

ops.forEach((op, i) => {
  ctx.save();

  // Clip each cell so operations don't bleed
  ctx.beginPath();
  ctx.rect(ox, oy, cellW, cellH);
  ctx.clip();

  // Draw destination (blue square)
  ctx.fillStyle = '#3b82f6';
  ctx.fillRect(ox + 5, oy + 5, 60, 60);

  // Apply composite op BEFORE drawing source
  ctx.globalCompositeOperation = op;

  // Draw source (orange circle)
  ctx.fillStyle = '#f97316';
  ctx.beginPath();
  ctx.arc(ox + 55, oy + 45, 32, 0, Math.PI * 2);
  ctx.fill();

  ctx.restore(); // also resets compositeOperation
});

// Common use-cases:
// 'multiply'  — darkens overlaps (like Photoshop Multiply)
// 'screen'    — brightens overlaps
// 'xor'       — cuts out intersection
// 'destination-out' — erase with a shape
Key Concepts
  • Set ctx.globalCompositeOperationbefore drawing the source shape
  • ctx.save() / ctx.restore() resets the composite operation per cell
  • source-over is the default — new drawing paints on top
  • destination-out — use a shape as an eraser mask
  • multiply / screen / overlay — CSS-equivalent layer blend modes
  • lighter — additive blend (useful for glowing particles)