Skylar Dong / The Lab / Creative Coding / Week 09

Everything's Filtered

A webcam mirror you edit with your hands. Pinch to frame a filtered version of yourself, push it away, or smash it into pixels.

The camera stays in your browser. Nothing is recorded or uploaded.

Works best on a laptop webcam, with both hands in view.

Draw
Pinch with both hands and pull them apart to frame a filtered you
Push away
Hold an open palm over a frame and move it toward the camera
Smash
Make a fist over a frame
C
Clear every frame

The filter

The background is the camera as it is. Inside every frame you draw, each pixel is recolored: the column it sits in picks a color from one gradient, and its brightness decides how much of that color it gets.

Bright parts of you turn into full color, dark parts sink to almost black, so the frame shows a glowing silhouette rather than a face.

Each column picks a color, left to right across 640 px
x 0 · purple x 256 · pink x 640 · yellow
Brightness decides how much of it (shown for the pink column)
in
out
0 90 130 255

brightness ≤ 90 → 15% of the color≥ 130 → full color

Reading hands

ml5’s handPose finds up to two hands and 21 points on each. The sketch turns those points into five rules.

Pinch
Thumb tip and index tip closer than 0.45 palm lengths. It only lets go past 0.65, so a shaky hand doesn’t flicker.
Draw a frame
Both hands pinching for more than 8 frames. Let go for 4 frames in a row to place it; anything under 40 px doesn’t count.
Push away
An open hand (finger curl above 1.6) over a frame. Every 1% the palm grows pushes the frame back, × 4.
Smash
A fist (curl below 1.2) held for 3 frames over a frame: 80 pixel squares, a shake and a white flash.
Palm length
Wrist to the base of the middle finger. Every distance is measured in palm lengths, so the same numbers work near or far from the camera.

Code

Every frame, glamUp() reads all 640 × 480 pixels of the mirrored camera and writes a recolored copy. Each column has its own gradient color, worked out once in setup(); a pixel’s brightness decides how much of that color it gets.

for (let y = 0; y < MIRROR_H; y++) {
  for (let x = 0; x < MIRROR_W; x++) {
    const i = (x + y * MIRROR_W) * 4;

    // Brightness = average of R, G, B (0 black ~ 255 white)
    const brightness = (src[i] + src[i + 1] + src[i + 2]) / 3;
    // ≤ 90 → 0.15 (almost black), ≥ 130 → 1 (full color), smooth in between
    const k = constrain((brightness - 90) / 40, 0, 1) * 0.85 + 0.15;

    const c = sunset[x];
    out[i] = c[0] * k;
    out[i + 1] = c[1] * k;
    out[i + 2] = c[2] * k;
    out[i + 3] = 255;
  }
}

A pinch starts below one threshold and only ends above a higher one, so a shaky hand doesn’t flicker between pinching and not.

if (st.pinching) {
  if (pinchDist > PINCH_NO || reach < FINGERS_OUT - 0.15) st.pinching = false;
} else if (pinchDist < PINCH_YES && reach > FINGERS_OUT) {
  st.pinching = true;
}