I made a 9 KB northern lights background for any website (no WebGL)

2026-11-30来源 dev.toEnglish
I made a 9 KB northern lights background for any website (no WebGL)

Aurora backgrounds are everywhere right now. Most of the ones I found are either a blurred CSS gradient that never really looks like an aurora, or a WebGL shader that pulls in a whole library.

I wanted something in between: real curtains of light that shimmer and drift, that you add with one script tag, and that don't make a laptop fan spin up. So I built Aurora, a free MIT package with zero dependencies, about 9 KB minified.

Aurora by Fluri: northern lights over a website header

Live demo: https://fluri.xyz/aurora/

<script src="https://cdn.jsdelivr.net/npm/@fluritheme/aurora@1/dist/aurora.global.js" data-auto></script> 

Or with npm:

import { createAurora } from "@fluritheme/aurora"; const aurora = createAurora({ palette: "classic", height: 0.6 }); aurora.pulse(); // a wave of light aurora.shootingStar(); 

Here are the three tricks that made it look right and stay cheap.

1. Draw it tiny, then let the browser blur it

An aurora is soft. Instead of blurring anything (canvas filter is slow and Safari doesn't support it), I draw the lights into an offscreen canvas at a quarter of the screen size, then draw that canvas onto the visible one, scaled up with imageSmoothingQuality = "high". The upscale is the blur, and it's free.

On top of that there's a second, even smaller copy (a twelfth of the size) drawn underneath at 70% opacity. That gives the soft halo around the bright edges that makes it feel like light rather than paint.

2. Curtains are just a lot of thin vertical strips

Each curtain is a wavy line across the screen, made from three sine waves with different speeds:

y = base + amp * ( 0.58 * Math.sin(x * f1 + t * s1) + 0.28 * Math.sin(x * f2 - t * s2) + 0.14 * Math.sin(x * f3 + t * s3) ); 

For every pixel column of the small canvas, I draw one thin strip upwards from that line. The strip is a pre-rendered 2×160 px gradient: bright and almost white at the bottom edge, the main colour in the middle, fading into violet or pink and then nothing at the top. Real auroras look exactly like that: a bright lower hem with rays rising out of it.

The strips are drawn with globalCompositeOperation = "lighter", so where curtains overlap they add up and glow, like real light does.

Two things stop it looking like a bar chart:

  • Brightness changes along the curtain with slow folds plus a faster, finer shimmer, so you see rays without hard steps between columns.
  • Colour blends smoothly between three gradients along x. My first version picked one gradient per column and you could see rectangles of colour. Cross-fading between neighbours fixed it.

3. Let the page decide how it blends

On a dark page the canvas uses mix-blend-mode: screen, so it only ever adds light: text stays readable and the lights glow over a starry sky. On a light page screen would be invisible, so it switches to multiply with deeper colours, which gives a soft watercolour tint across the header instead.

The theme checks the background colour behind the page, and checks again every few seconds, so a site's dark-mode switch is followed automatically.

The small stuff

  • The curtains lean towards your cursor and brighten near it. Clicking sends a ripple of light outwards along them.
  • Stars twinkle, and every 9 to 18 seconds a shooting star crosses the sky (dark pages only).
  • It fades out as you scroll, pauses in background tabs and slows right down for prefers-reduced-motion.
  • The canvas has pointer-events: none, so it never blocks a click or text selection.

You can limit it to one section too:

createAurora({ root: document.querySelector(".hero"), height: 1 }); 

Palettes: classic, arctic, sunset, candy, or brand with your own colour.

It's part of Fluri, my collection of interactive website effects. The npm packages are free; if you run WordPress, Shopify or Wix there's a no-code version with live settings.

I'd love feedback on how it runs on your machine, especially older phones. What would you put under northern lights?

npm: https://www.npmjs.com/package/@fluritheme/aurora

原文:https://www.a8q9.cn/en/article/i-made-a-9-kb-northern-lights-background-for-any-website-no-webgl-b32b06.html

相关文章