labs

clickable

2026-08-28

a click without a sample

every press on this site is four milliseconds of white noise through a narrow bandpass filter, which is what a mechanical click acoustically is. synthesizing it costs no network bytes and lets each kind pick its own timbre out of one primitive instead of shipping a sprite sheet of near identical samples.

a little variation

the detail that matters is the jitter: with it off, five presses are bit for bit the same and the ear catches the repetition by the third, because no real mechanism ever makes exactly the same sound twice. the ticks under the pad plot where the last few presses actually landed, so the jitter is legible with the sound off.

take it with you

three steps, and the stage above runs in your own project.

  1. install

    nothing to add. it runs on react alone, 18 or newer.

  2. copy

    3 files, kept together in one folder, clickable/. the styles travel with them.

    "use client";
    
    import { useState } from "react";
    import { haptic, playBurst } from "./feedback";
    import "./clickable.css";
    
    interface Props {
      // hz the noise is filtered around: higher reads as a tick, lower as a thock
      frequency?: number;
      // how narrow the filter is: higher rings more like a tone
      q?: number;
      gain?: number;
      // samples the noise takes to die away
      decay?: number;
      // random detune per press, as a share of frequency; 0 makes every press identical
      spread?: number;
    }
    
    const SAMPLES = 192;
    const HISTORY = 8;
    const AXIS = 0.25;
    const MID = 20;
    
    function envelope(gain: number, decay: number): string {
      const top: string[] = [];
      const bottom: string[] = [];
      for (let x = 0; x <= 100; x += 1) {
        const a = 18 * gain * Math.exp(-((x / 100) * SAMPLES) / decay);
        top.push(`${x},${(MID - a).toFixed(2)}`);
        bottom.push(`${x},${(MID + a).toFixed(2)}`);
      }
      return `M${top.join(" L")} L${bottom.reverse().join(" L")} Z`;
    }
    
    export default function ClickTuner({
      frequency = 2600,
      q = 7,
      gain = 0.35,
      decay = 30,
      spread = 0.3,
    }: Props) {
      const [hits, setHits] = useState<number[]>([]);
    
      const press = () => {
        const centre = frequency * (1 + (Math.random() - 0.5) * spread);
        playBurst({ frequency: centre, q, gain, decay }, 0);
        haptic("click");
        setHits((prev) => [...prev, centre].slice(-HISTORY));
      };
    
      return (
        <div className="click-lab">
          <figure className="click-lab-envelope">
            <svg viewBox="0 0 100 40" preserveAspectRatio="none" aria-hidden="true">
              <path d={envelope(gain, decay)} />
            </svg>
            <figcaption>4ms of noise, shaped</figcaption>
          </figure>
    
          <button
            type="button"
            className="click-lab-pad"
            onPointerDown={press}
            onKeyDown={(e) => {
              if (e.key === " " || e.key === "Enter") {
                e.preventDefault();
                press();
              }
            }}
          >
            press
          </button>
    
          <figure className="click-lab-scatter">
            <div className="click-lab-axis">
              {hits.map((hz, i) => (
                <span
                  key={`${i}-${hz}`}
                  style={{ left: `${(50 + ((hz / frequency - 1) / AXIS) * 50).toFixed(2)}%` }}
                  data-fresh={String(i === hits.length - 1)}
                />
              ))}
            </div>
            <figcaption>
              {hits.length === 0
                ? "where the last presses landed"
                : `${hits.length} press${hits.length === 1 ? "" : "es"}, ${Math.round(hits[hits.length - 1]!)}hz`}
            </figcaption>
          </figure>
        </div>
      );
    }
    
  3. use

    the stage above, exactly as it runs here. the comments mark what to change.

    example.tsx
    "use client";
    
    import ClickTuner from "./clickable/ClickTuner";
    
    // the tuner plays one burst per press so you can hear each setting;
    // in your own buttons, call feedback("tick" | "click" | "close" | "confirm") from ./clickable/feedback
    export default function ClickableExample() {
      return <ClickTuner frequency={2600} q={7} gain={0.35} decay={30} spread={0.3} />;
    }