// devlog/001

There’s a string hanging off my website

How the pull cord in the corner works, and where the “no” goes in the dark.

web ~9 min read 0 checkboxes harmed

Every website with a dark mode has a button for it. Usually a little sun that turns into a little moon. You click it, the colours flip, everybody gets on with their day.

It's a solved problem. A boolean, a CSS class, maybe a localStorage line so it remembers. It is, objectively, the correct way to do it.

This post is about how I didn't do that.

The idea

I wanted a pull cord. The old kind you find hanging from a ceiling lamp or in a bathroom, with a weighted handle on the end of a bit of string. You tug it, it goes tck, the room changes.

And not a picture of a cord that's secretly a button. I wanted the behaviour. You grab it, you pull, it resists a little, it clicks, you let go, and it swings around for a while like it has opinions about being touched.

It's hanging in the top right corner of this page right now. You've probably already pulled it. That's fine. That's what it's for. I'm actually a little bit happy for you!

A rope is just 18 dots with trust issues

The cord is an SVG laid over the entire viewport. It has pointer-events: none everywhere except the handle, so it never blocks a click on the page underneath. The rope is a path, the handle is a group of shapes at the end of it, and every frame JavaScript decides where both of them are.

Deciding where they are is a verlet simulation, which sounds more impressive than it is. The rope is 18 points. Each point remembers where it is now and where it was one step ago. There's no velocity variable: the velocity is the difference between those two positions. Every step, each point keeps drifting the way it was already going, loses a bit of energy, and falls a bit.

const vx = (p.x - p.px) * DAMP, vy = (p.y - p.py) * DAMP;
p.px = p.x; p.py = p.y;
p.x += vx; p.y += vy + GRAVITY;  // DAMP = .985, GRAVITY = .55

Left alone, that's 18 dots falling off the bottom of the screen. What makes it a rope is the second half. After everything moves, the simulation walks down the chain and pushes each pair of neighbours back to the right distance apart. One pass isn't enough, because fixing one pair stretches the next one, so it does the whole chain 16 times per step. The first point is nailed to the ceiling and never moves.

The handle is the last point and gets special treatment. Hanging free, it only absorbs 30% of each correction, which makes it behave heavier than the string above it. While you're holding it, it absorbs none: it goes exactly where your pointer is, and the rest of the rope has to cope. Like all of us, frankly.

demo · the real cord, with its clothes offx-ray off

This doesn't draw a copy. It draws the 18 points and 17 constraints of the actual cord in the corner, live. Turn it on, then grab the handle or wave your cursor through the string.

orange dots = points · lines = distance constraints · the ring = the handle, at 30% weight

To draw it, the points become one SVG path made of quadratic curves through the midpoints between them, so 17 straight segments read as one smooth string. The path is drawn twice: once in orange and once as a darker dashed line on top, which passes for a braided cord from a normal viewing distance. When the rope stretches, the dash offset shifts, so the braid looks like it's untwisting. Nobody will ever notice this. I know it's there.

The handle is a bakelite-ish body with brass ends, built out of a few SVG gradients and rotated to follow the angle of the last bit of rope. Behind it sits an invisible ellipse that's a lot bigger than the handle, because the visible handle is about 22 pixels wide and fingers are not.

Gravity shouldn't depend on your monitor

The simulation runs on GSAP's ticker, which fires once per frame. The catch is that "once per frame" means 60 times a second on one screen, 120 on a nice phone and 144 on somebody's gaming monitor. Step the physics once per frame and the cord falls twice as fast for people with better hardware. Which is funny, but wrong.

// fixed 60Hz physics regardless of display refresh rate
let acc = 0;
gsap.ticker.add(() => {
  acc = Math.min(acc + gsap.ticker.deltaRatio(60), 4);
  while (acc >= 1) { step(); acc -= 1; }
  render();
});

deltaRatio(60) says how many 60 Hz frames' worth of time just passed. That goes into a bucket, and the simulation takes as many whole steps as the bucket holds. At 120 Hz it steps every other frame. At 30 Hz it steps twice a frame. Rendering still happens every frame, so it stays smooth either way.

The cap of 4 is there so that coming back to a tab you left an hour ago doesn't make the cord try to catch up on an hour of physics at once.

Pulling, with resistance

When you grab the handle, it follows your pointer. But a real cord doesn't let you drag the handle across the room, because it runs out of string. So once your pointer is further from the ceiling than the rope is long, the extra distance goes through a rubber-band curve:

const over = d - len;
const rub = MAXPULL * (1 - Math.exp(-over / MAXPULL)); // rubber-band resistance
reach = len + rub;
pull = rub * Math.max(0, dy / d);

Small overshoots stretch almost one-to-one. Bigger ones get rapidly diminishing returns, and however far you drag, the cord never stretches more than 85 pixels. It feels like pulling against a spring, which is roughly what a pull switch is.

Then only the downward part of that stretch counts. Yanking the cord sideways should swing it, not flip the lights. Once the downward pull passes 34 pixels, the theme switches. Once per pull, so you can't strobe the page by wiggling at the threshold. You can still strobe it by pulling repeatedly. I'm not your manager.

live · reading the real cord
85px · it won't stretch past this 34px · tck (pulling straight down) 0 how far you drag past the end of the rope → stretch ↑

Grab the cord and pull. The dot is your hand, plotted on the actual curve. Or let the button do it for you.

lights flipped this visit: 0

stretch = 85 × (1 − e−over/85). Pulling straight down, the click lands about 43px past the end of the rope.

Click, or drag?

Some people will drag the cord. Some will just click it, because twenty years of buttons have trained them to. Both should work. People should be free to decide what they do with a string. Isn't that what we've been fighting for all these centuries?

On pointer down, the code writes down where and when. On release, it counts as a click if the pointer moved less than 6 pixels, the whole thing took under 300 milliseconds, and the lights didn't already switch during the drag. A click then plays a quick yank: GSAP tweens the handle straight down past the threshold in 0.13 seconds, through exactly the same aimAt() as a real pull. So the click isn't a shortcut that flips a class. It's a very fast, very confident pull.

Enter and Space on the focused handle trigger the same yank, and the handle is marked up as role="switch" with aria-checked, so a screen reader announces it as a switch instead of "group, image, unlabeled thing".

Letting go is harder than grabbing

Starting a drag is one event. Ending one is a list. The release function gets called on all of these:

  • pointerup anywhere in the window, not only on the handle
  • pointercancel, when the browser takes the pointer away
  • lostpointercapture on the handle
  • the window losing focus
  • a mouse wheel scroll
  • the tab being hidden
  • a pointer move with no button pressed at all, which means the button came up somewhere the page didn't hear about

Miss one and you get a cord glued to a cursor that isn't holding it. Which is a very specific kind of haunted.

Phones need two more things. The handle has touch-action: none, and a touchstart listener registered with passive: false that calls preventDefault(). Without those, the phone sees a finger swiping downwards and concludes, reasonably, that you'd like to scroll. The CSS covers most browsers; the listener is there for iOS Safari.

The bits nobody asked for

  • You can knock it. On every pointer move, any rope point within 16 pixels of the cursor gets nudged by the cursor's movement. You can bat it around like a cat. You probably already have.
  • It swings when released. The stretch eases back to normal and the rope keeps swinging until damping wins.
  • It makes an entrance. On load, the rope starts flung out to the left and swings into place, as if someone just walked past it.
  • It glows in the dark. The brass tip has a radial gradient behind it that fades in when the lights go off and slowly breathes, and the hint underneath changes from "pull me ↓" to "psst. the glowy thing." Finding a light switch in the dark is hard. That's realism.
  • It clicks. The sound isn't a file. It's 35 milliseconds of random noise with a steep fade, pushed through a band-pass filter at 2.4 kHz. Generated fresh every single time, like artisanal bread.
audio · 35ms of handcrafted noiseno .mp3 files were involved
for (let i = 0; i < len; i++)
  d[i] = (Math.random() * 2 - 1) * Math.pow(1 - i / len, 5);
f.type = "bandpass"; f.frequency.value = 2400;
random samples × a fade that falls off a cliff, then band-passed until it sounds like plastic

The name

And then there's the part that justified all of this, at least to me.

The site is called noiggy. In the dark theme, the "no" switches to the exact colour of the background. It's still there, technically, taking up space. You just can't see it. So the name reads iggy, which also happens to be what people call my dog’s breed.

noiggy.

Same developer. Worse lighting.

// lights on. hi, I'm noiggy.// lights off. the "no" is still there. it's just shy.

On the homepage the "n" and "o" drop out one at a time when the lights go off, and bounce back in when they come on, accompanied by a short flicker, like a fluorescent tube in an office that's been meaning to replace it. The rest of the name does a little hop. I like to think it's relieved.

The theme itself is just a dark class on <html>, saved in localStorage, so the lights stay how you left them when you come back. Or when you open this post, if you were wondering why it's dark in here.

In total

The pull cord is roughly 130 lines of JavaScript, about 25 lines of SVG, three gradients, a sound effect made out of maths, and a list of seven ways a person can stop holding something.

A checkbox is <input type="checkbox">. It would have taken a minute. It works with a keyboard out of the box, it works on every device ever made, and it has never once needed a section called "Letting go is harder than grabbing".

Mine has gravity, though.