Cookie settings

Choose how much analytics you share. Your player and accessibility preferences work with any choice.

Cancel Save preferences

Menu

Home Playground Read the room
AccessibilityDesign Systems

About 5–9 min to explore

Read the room

Sometimes the IntersectionObserver spec can miss what feel like really obvious overlaps. This tiny lil' service reliably tells a floating element what it's passing over, so your CSS can do something about it.

How often have you found yourself excited to play with a brand-new discovery like lush, tinted box-shadows via Josh W Comeau's shadow palette generator , only to remember your site swaps between dark and light background sections on the same page? Or how about Apple’s new(ish) Liquid Glass design language? Say what you will about its accessibility (and you’d probably be right), but the way it pulls color from whatever’s behind it and can update its ink for contrast is pretty cool.

Anything that floats runs into it eventually. A sticky nav bar, a docked player, a card riding over a hero image. They all get tuned against one background… and then the page moves. The shadow you dialed in so carefully over a light section goes muddy the second it crosses into a saturated one, and there’s no single version of that color that’s right in both places.

Set left Reset Set right

Shadow tint tuned for this canvas.

Same card, wrong shadow tint.

So canvas-watch lets background elements tell a floating element what to do. As a watched element scrolls, it works out which background it’s mostly sitting over and picks up a class that background provides. From there it’s down to however you want to write up the CSS, no more JS required.

What about IntersectionObserver?

If you’ve done much scroll-driven work, IntersectionObserver is probably the first thing that came to mind. The problem is that IO only compares an element against one of its ancestors or the viewport, and the section sitting behind your sticky nav is usually a sibling (or a cousin twice removed) somewhere else in the DOM. So we treat the overlap itself as plain geometry: canvas-watch compares bounding rectangles and picks whichever background covers the majority of the watched element. IO still plays a helpful part in the service though, it’s used as a cheap gate that keeps the math logic from running for anything that’s off screen.

Try it

Scroll the panel. The floating card measures how much of itself overlaps each background zone and tags itself with the winning .over-* class, and each of those classes swaps in a shadow tint tuned for that zone. Switch the watcher off to see how the card’s original mint tint holds up against a not-mint background. The shadows are exaggerated on purpose so the change is easy to spot (on a real page you’d probably want something a lot more subtle).

Bg class: .demo-canvas-mint Trigger: .canvas-accent-primary-300-trigger
Bg class: .demo-canvas-midnight Trigger: .canvas-brand-800-trigger
Bg class: .demo-canvas-coral Trigger: .canvas-critical-emphasis-trigger
Bg class: .demo-canvas-gold Trigger: .canvas-positive-trigger

Our shadow illustration device.

On Off Enable canvas-watch

How it works

Give your floating element a .watch-bg-canvas class, then give each background zone a *-trigger class. Canvas Watch maps a trigger like .canvas-brand-emphasis-trigger to .over-canvas-brand-emphasis on the floating element, and your CSS decides how restrained or dramatic the updated styling should be.

<script>
	import { watchBgCanvas } from '@mzebley/canvas-watch/svelte';
</script>

<section class="canvas-brand-emphasis-trigger">…</section>

<div class="card watch-bg-canvas" use:watchBgCanvas>…</div>
.card {
  --demo-shadow-tint: 158 76% 25%;
  box-shadow:
    0 0.5rem 0.8rem hsl(var(--demo-shadow-tint) / 0.72),
    0 2rem 4rem hsl(var(--demo-shadow-tint) / 0.5);
}

.card.over-canvas-brand-emphasis {
  --demo-shadow-tint: 215 100% 67%;
}

Every watched element on the page shares a single requestAnimationFrame loop, and each one only gets measured while it’s actually on screen, so a page full of them stays pretty cheap.

Beyond shadows

Shadows are what inspired me to dig into it all, but the dynamically applied class ends up being a hook for anything that should care about what’s behind it. In the demo below we play with what might happen to your floating nav bar across an image that reads mostly dark versus one that is quite light. Click the fix the styling button to see the updated CSS class canvas-watch would apply to update the shadows, background-tint, and inks used.

Set left Reset Set right

Home

Music

More

Home

Music

More

Take it home with you

If you’ve got something floating that could stand to read the room a little better, canvas-watch is lightweight, open source, and has a framework-agnostic core with a Svelte action ready to go. Let me know where you put it!

Full documentation + live examples  ·  Source on GitHub  ·  Package on npm

All demos
Sharing options

About

Content

Connect

© 2026 Mark Zebley