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
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.
So
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).
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.
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!