Dev.to WebDev 🛠 Dev 👁 2 📖 2 min read

Click anywhere to throw a snowball at the website (without breaking any links)

Here's a December toy for websites: click anywhere and a snowball arcs in from the bottom of the screen, splats where you clicked, then slowly slides down and melts. Headings and cards wobble when hit, a snowman pops up

Click anywhere to throw a snowball at the website (without breaking any links)

Snowball Fight by Fluri

Here's a December toy for websites: click anywhere and a snowball arcs in from the bottom of the screen, splats where you clicked, then slowly slides down and melts. Headings and cards wobble when hit, a snowman pops up now and then to have his hat knocked off, and five hits can unlock a discount code.

It's free (MIT), about 19 KB minified, no dependencies.

Install

<script src="https://cdn.jsdelivr.net/npm/@fluritheme/snowball-fight@1/dist/snowball-fight.global.js" data-auto></script>
import { createSnowballFight } from "@fluritheme/snowball-fight";

createSnowballFight({ hitsForReward: 5, message: "Great aim! Here's 10% off.", code: "SNOWBALL10" });

Rule one: don't break the website

A "click anywhere" effect is a great way to break a site. The click handler bails out for anything interactive, which turned into a longer selector than I expected:

const INTERACTIVE = "a,button,input,textarea,select,label,option,summary,details,video,audio,iframe," +
  "[contenteditable],[role=button],[role=link],[role=tab],[role=menuitem],[role=checkbox]," +
  "[role=switch],[role=slider],[tabindex]:not([tabindex='-1'])";

if (target.closest(INTERACTIVE)) return;
const sel = getSelection();
if (sel && !sel.isCollapsed && String(sel).trim()) return; // they're selecting text

The text selection check matters more than you'd think: without it, every attempt to copy a paragraph throws a snowball at it.

The throw

A snowball starts below the screen, roughly under the target, and flies on a simple arc: linear interpolation with an ease, minus a sine bump for height. It shrinks as it goes, which sells the depth:

const e = ease(k);
const x = b.sx + (b.tx - b.sx) * e;
const y = b.sy + (b.ty - b.sy) * e - b.arc * Math.sin(Math.PI * k);
const r = b.r0 + (b.r1 - b.r0) * k;

Flight time grows with distance (0.34 + d / 2600 seconds), so near and far throws both feel natural.

Splats that stick to the page

Each splat is pre-rendered once to a small offscreen canvas (a squashed core, streaks thrown outwards ending in droplets, and a cool shadow so it shows on white pages), then drawn with drawImage.

Splats are stored in page coordinates, not screen coordinates, so they stay on the paragraph you hit as you scroll. After a moment they start to slide down, leaving a fading smear drawn as a gradient-filled quadratic shape, and fade out after melt seconds.

Hits

When a ball lands, it checks what's under the impact point with elementFromPoint. If it's a heading, image, button or card, that element gets a short wobble and the hit counter goes up. Anything marked data-fluri-ignore is left alone. A hit event, fluri-snowball-hit, lets you count them in analytics.

With prefers-reduced-motion the balls skip the flight and just appear.

It's one of the free, MIT-licensed packages behind Fluri, my collection of interactive website effects. If you run WordPress, Shopify or Wix there's a no-code version with live settings.

What should the snowman do when you finally hit him? I'm open to suggestions for v2.

npm: https://www.npmjs.com/package/@fluritheme/snowball-fight

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.