Adhiraj Singh ← All notes

My Page Pet Covers the Whole Screen and Can't Block a Single Click

page-buddy is a teal 3D fuzzball that lives on top of a web page. You import one file and call mountBuddy(). After that it walks along the bottom of the window, climbs the walls, hangs from the ceiling, chases a ball and pounces on a cursor that sits still for too long. It also covers its eyes when you focus a password field. It's three.js with no build step.

Most of buddy.js is character work: springs for squash and stretch, a table of facial expressions, a small state machine. That's the fun part, but it isn't what shaped the code. One constraint did, and it has nothing to do with the creature. The creature is drawn on a canvas that covers the whole viewport, sits above everything else, and still has to leave the page underneath fully usable.

One canvas over everything

The creature can be anywhere. It walks the full edge of the window, gets thrown into the middle and floats up on balloons. So the renderer gets the entire viewport:

Object.assign(canvas.style, { position: 'fixed', inset: 0, width: '100%', height: '100%', pointerEvents: 'none', zIndex: o.zIndex, touchAction: 'none' });

The default zIndex is 2147483000, just under the 32-bit integer ceiling. That means a transparent layer sits over every button, link and input on the host page. The only thing that keeps the page usable is pointerEvents: 'none', which sends every click, text selection and hover straight through to the elements underneath.

That creates a problem, because the creature has to be grabbed, thrown and poked. The canvas needs to take pointer events exactly where the creature is and nowhere else. CSS can't describe "where the creature is", because the creature is just pixels in a WebGL buffer.

Flipping one flag

So the canvas stays at none, and the flag switches to auto only while the cursor is over the creature. To decide what counts as "over", I raycast against an invisible mesh that's built into the model:

const hit = new THREE.Mesh(sph(1.25 * R, 12, 8), new THREE.MeshBasicMaterial({ transparent: true, opacity: 0, depthWrite: false }));
hit.position.y = -0.45 * R; inner.add(hit);

It's a low-poly sphere at zero opacity that doesn't write depth, and it's excluded from shadow casting afterwards. The raycast checks only this mesh, not the dozens of meshes that make up the fur, eyes and toes:

const hitTest = (cx, cy) => { ndc.set(cx / W * 2 - 1, -(cy / H) * 2 + 1); ray.setFromCamera(ndc, camera); return ray.intersectObject(M.hit).length > 0; };

Because the hit sphere lives inside inner, it inherits the creature's position, rotation, flip and squash. When the creature hangs upside down from the ceiling, the sphere goes with it, and the hit test needs no special case for that.

The toggle itself:

function setHover() { // re-checked every frame: the monster moves under a still cursor
  const hit = ptr.y > 0 && hitTest(ptr.x, ptr.y);
  if (hit !== m.hover) { m.hover = hit; canvas.style.pointerEvents = hit ? 'auto' : 'none'; canvas.style.cursor = hit ? 'grab' : ''; if (hit && !m.act && m.state === 'idle') startAct('wiggle', 1); }
  return hit;
}

The failure a mousemove handler can't catch

The obvious place to run this check is pointermove, and it does run there. The comment on that function is the reason it also runs somewhere else: the monster moves under a still cursor.

The creature acts on its own clock. It decides to walk somewhere, gets dropped off a wall by a hard scroll, or wanders after a ball. If the hover check only ran when the mouse moved, two things would go wrong.

The first is the bad one. Say the cursor rests on the creature, so the flag is auto. Then the creature walks away. The canvas still covers the whole viewport, and now it takes every pointer event. Every click on the page lands on an invisible layer until the user happens to move the mouse. From the user's side, the site has stopped responding and nothing on screen explains why.

The second is milder. The creature walks under a resting cursor while the flag is none. The cursor never turns into a grab hand, and a click there goes to whatever page element is underneath.

So the check runs on the creature's clock as well. At the end of every update:

updateToys(dt); pose(dt);
if (m.state !== 'grab') setHover();

The grab exception is deliberate. Once a drag has started, the pointerdown handler has already forced the canvas into auto and captured the pointer. A per-frame check that turned the flag off mid-drag would be wrong:

on(window, 'pointerdown', e => {
  if (e.button !== 0 || !hitTest(e.clientX, e.clientY)) return;
  e.preventDefault(); interacted(); cancelAct();
  ptr.world.set(e.clientX - W / 2, H / 2 - e.clientY);
  m.grabOff.set(m.pos.x - ptr.world.x, m.pos.y - ptr.world.y); m.downT = performance.now(); m.downAt.set(e.clientX, e.clientY);
  if (m.state === 'float') m.holding = null;
  setState('grab'); m.up.set(0, 1); canvas.style.pointerEvents = 'auto'; canvas.style.cursor = 'grabbing';
  try { canvas.setPointerCapture(e.pointerId); } catch {}
});

This listener is on window, not on the canvas, and it runs its own hit test at the event's coordinates. Whether a grab starts doesn't depend on the hover flag being up to date. It's decided from the geometry at the moment of the press.

Pixels and world units are the same number

All of this stays simple because of one line in resize:

camera.aspect = W / H; camera.position.z = H / 2 / Math.tan(THREE.MathUtils.degToRad(17.5)); camera.updateProjectionMatrix();

The camera has a 35° vertical field of view. Placing it at that distance makes the visible slice at z = 0 exactly H units tall, so one world unit is one CSS pixel. A pointer event then converts to world space with a subtraction, ptr.world.set(e.clientX - W / 2, H / 2 - e.clientY), and no unprojection is needed for movement. Gravity is written as const G = -2600; // px/s². The walk around the window edge is a single number s, measured in pixels clockwise from the bottom-left corner.

There's one small comment in the same function that this depends on:

W = canvas.clientWidth || innerWidth; H = canvas.clientHeight || innerHeight; // clientWidth excludes the scrollbar

innerWidth includes the scrollbar and the fixed canvas doesn't. If I sized from innerWidth, the right wall the creature climbs would sit off by the width of the scrollbar.

What this approach doesn't do

The hit shape is a sphere, not the creature's outline. The sphere has a radius of 1.25 head-radii and its centre sits below the head. The top of the head, the fur tufts and the ears stick out above it, so they can't be clicked. Meanwhile the empty space beside the tummy counts as part of the creature. I picked forgiving and cheap over exact.

The check costs one raycast per frame against a 12×8 sphere, on every page the creature runs on. I haven't measured it.

When the flag is auto, it applies to the whole canvas and not just the creature. That's fine, because the cursor is over the creature at that moment anyway. But it does mean the page's responsiveness depends on the per-frame check being right every time.

The other defensive choices follow the same rule of never getting in the host page's way. If the user prefers reduced motion, or if creating a WebGL context throws, mountBuddy returns a no-op object with the same methods, so calls like buddy.ball() in the host page don't break. Every listener is registered through a single AbortController, so destroy() removes all of them at once.

The lesson I took from this: if a layer sits over someone else's page, its click-through state has to be recomputed whenever the thing it represents moves, not just when the user acts. The mouse isn't the only thing that changes what's under the cursor.