Skip to main content
William Boyden
Theme: Night, matching your device

A light show that cannot hear the music

7 min read

100Lighthouse, all four categories, phone and desktop profiles

I built a fan page for a friend's bass music with a visualizer and a light show. The player is SoundCloud's, in a frame the page cannot listen to. How the visuals keep time anyway, how fourteen themes reach the canvas, how the 3D patterns work without WebGL, and what it took to keep a loud page at 100 in Lighthouse.

A friend of mine makes riddim and dubstep as BIOTECHNICK. He was having a rough week, so I built him a page: his tracks, a lot of glow, and a button that says something true and kind each time he presses it. It is live at willboyden.github.io/biotechnick, and the source is on GitHub.

It is one static page with no framework and no dependencies. Three things about it were less simple than they sound.

The music stays his

The page plays his tracks, but it does not hold them. Each track streams from SoundCloud's own player, so his play counts still count. A download link appears only where he offers a download himself, and it points to the place he chose. The repository contains no audio, and a test fails the build if any link on the page leaves a short list of allowed hosts.

SoundCloud's player is also the heaviest thing a visitor could load, so the page does not load it. The frame and its script are created on the first press of play. Until then a visit costs the page's own files: about 28 kB of HTML, CSS and JavaScript after compression, one 22 kB font, and the artwork. No tracker and no cookie comes with it.

Visuals without the audio

An EDM page should move with the music. The usual way is the Web Audio API: read the samples, take a spectrum, draw it. That is not possible here. The player is a frame from another site, and a page cannot read audio out of a frame it does not own. That rule protects every visitor, and I did not want to work around it by hosting copies of his files.

What the player does give is two facts: whether it is playing, and how far into the track it is. Each track in the page's configuration also carries a tempo. Position and tempo are enough for a clock:

const beats = (seconds * bpm) / 60 / 2; // half time: where riddim lands
const kick = Math.exp(-(beats - Math.floor(beats)) * 5);

kick jumps to 1 on each half-time beat and decays before the next one. Everything that moves reads that one value. The bars in the hero swell with it, a CSS variable carries it to the glow around the artwork, and the light show uses it to push its beams and rings outward. The player reports its position at intervals, so the clock advances by itself between reports and takes each report as a correction.

This is not beat detection, and the page does not claim it is. The tempos are my estimates, typed into a configuration file. The visuals hold the right pace, but nothing lines their pulse up with the real kick drum, and a breakdown with no drums pulses like a drop. For a page meant to make someone smile that was a fair trade. If he sends the real tempos, they are one number per track.

Auto mode, from the same clock

The light show has ten patterns, and it should be able to pick its own. With no audio to analyse, "follow the music" again means "follow the clock". On Auto the show counts bars. Every eight bars it moves to a new pattern, in an order worked out from the track's place in the list, so each track gets its own sequence. Within those eight bars its symmetry steps up from 4 to 6 to 8 to 12, which reads as a build. Over the last bar the outgoing and incoming patterns are both drawn, one fading down as the other fades up.

Dubstep is written in eight-bar and sixteen-bar blocks, so a change every eight bars tends to land near a real change in the track. It is still counting and not listening: if a track's sections do not start on a multiple of eight bars, the changes will be off from the music by the same amount every time.

Three dimensions without WebGL

Four of the patterns are 3D: a wire landscape rolling toward the viewer, cubes tumbling inside each other, a globe, and a star field. I did not use WebGL. A wireframe needs only one piece of 3D arithmetic, the perspective divide: rotate each point, then divide its x and y by its distance.

const d = 3.2 / (3.2 + z);
return [w / 2 + x * scale * d, h / 2 + y * scale * d];

The lines are then drawn on the same 2D canvas as everything else. That keeps the theme colours, the blending and the fades working without a second code path, adds no library, and cannot fail on a device with a blocked or missing graphics driver. The four 3D patterns together are about a hundred lines.

When nothing is playing the show idles slowly. It never flashes or strobes, it is capped at 30 frames a second, it stops drawing when it is off screen, and it holds a still frame when the device asks for reduced motion.

Fourteen themes, and a canvas that follows them

There are fourteen themes, ten dark and four light, with names like Bass Lab, Blacklight and Foam Party. Each is written as three colours: a background, a text colour and one accent. A build step derives the other colours from those three and adjusts each until its contrast passes, so adding a theme is one line.

CSS follows a theme for free, because every colour in the stylesheet is a variable. A canvas does not: it paints with whatever colour it was given when it drew. So the light show reads the theme's variables from the page, and reads them again when the theme changes.

Colour alone was not enough. On a dark theme I blend the beams additively, the way real light adds up: where two beams cross it gets brighter. On a light theme adding light to an already bright background only washes out toward white. There the canvas multiplies instead, so the shapes layer like ink on paper.

Twenty frames of the light show: its ten patterns in a dark theme (top two rows) and in a light theme (bottom two rows). In the dark theme the colours add up to bright cores; in the light theme the same patterns are drawn like ink on a cream background. The last four patterns in each pair of rows are the 3D ones: a wire landscape, nested cubes, a globe and a star field

Keeping a loud page at 100

A page with glow, a display font, two canvases and fourteen themes is not what usually scores 100 in Lighthouse. It does, in all four categories, on both the phone and the desktop profile. I measured the live site with a cold cache: ten of the themes after the first release, and a first visit plus two themes again after each later change.

What it took:

  • One font file. The first build scored 98 on the phone profile. It carried a variable font of about 50 kB where one weight, 22 kB, was all the design used. That, the next item and a smaller hero image made up the difference.
  • One stylesheet request for the page's own styles, and artwork in two sizes so a phone does not download desktop images.
  • No layout shift. Images and canvases have their size reserved before they load; Lighthouse reports a shift of 0.
  • A strict Content-Security-Policy, with no inline script or style anywhere. The theme is still applied before the first paint, from a small script file in the head.
  • Contrast measured, not eyeballed. A script loads the page in a headless browser, switches through every theme at two widths and measures each visible piece of text against what is behind it: 4,662 measurements, all passing WCAG AA.

All of this was measured in headless Chrome. I have not yet watched the light show on a physical phone.

What I would keep

The constraint made the design. Because the page could not hear the audio, it needed a clock, and one clock shared by everything gave the page a pulse that a spectrum per canvas would not have: the bars, the glow and the light show all land together. Auto mode came from the same clock for a few more lines. And because the themes were three colours each, the canvas could follow them with a few variable reads.