Circle stacking, pendulum motion, honest grip logic and counters tied to real events.
Remastered from PL-147 (original text not on file)
Circle stacking, pendulum motion, honest grip logic and counters tied to real events.
No runs posted yet. Be the first: post yours and tag @wikiwayne.
CLAW CRANE CARNIVAL: a Playable Claw Machine with a Swinging Claw and an Honest Grip
HOOK OBJECT: window.__claw
Build "CLAW CRANE CARNIVAL": a neon arcade claw-crane cabinet drawn in Canvas 2D (no libraries and no physics library). 44 round plush prizes lie in a pile behind the glass. A claw hangs on a cable from a sliding trolley and swings like a pendulum whenever the trolley stops. You steer, press DROP, and the claw grabs only what is really between its prongs, carries it to the chute, and a win bursts into confetti. It is fun to keep watching because no two tries go the same way and the claw really swings.
LOOK
- Style: neon arcade cabinet with glossy, outlined plush prizes. Palette: night room #0B0720, cabinet body #2A1B5E with trim #FF3DCB and #3DF0FF, glass interior #140B33, floor #1E1450, steel (rail, divider) #B8C2D6, claw metal #E6ECF7, cable #8A93A8, prize colors #FF4FA3, #3DE0FF, #FFD23F, #7CFF6B, #B57CFF and #FF8A3D (each with a 3 design-unit outline #1B1030, a white highlight and a two-dot face), panel #1A1040, text #FFF3C4.
- Design units: the cabinet is 600 x 680 design units. The view scale is s = min(window width / 640, window height / 700) and the cabinet is centered, so at 1280x720 s = 1.0286 and the cabinet is 699 px tall (97% of the height), and at 390x844 (portrait) s = 0.609 and the cabinet is 365 px wide (94% of the width). Everything below is in cabinet design units, origin at the cabinet's top-left, y pointing down; prizes(), clawPos() and trolleyX() use these units too.
- Cabinet layout: marquee y 0 to 70 with the title "CLAW CARNIVAL" and a row of 14 bulbs that chase (bulb k is bright when floor(t / 0.1) mod 14 = k); glass interior x 40 to 560, y 84 to 524; control panel y 524 to 680. Interior: left wall x = 40, right wall x = 560, ceiling y = 84, floor y = 524 for x from 144 to 560. Chute opening in the floor from x = 48 to x = 136 (88 wide) with a dark chute bin below it (y 524 to 600). Divider wall x 136 to 144, y 304 to 524 (220 tall, steel). Gantry rail along y = 98 from x = 64 to x = 544; the trolley rides it.
- Prizes: 44 circles with radius 20, 22 or 24 (index mod 3), mass proportional to r squared, starting in a staggered grid of 4 rows of 9 and one row of 8 from the floor up (rows 40 apart, x from 164, seeded jitter of 3 units), then simulated for 3.0 s of simulation time (180 frames) before the first frame, so the pile is already at rest on the floor in x 144 to 560.
- Claw: a cable from the trolley (x_t, 98) down to the head; a head box 28 x 20 centered at (hx, hy); 3 prongs: a left and a right arm, 56 long, pivoting at (hx - 12, hy + 10) and (hx + 12, hy + 10), plus a narrower middle prong between them. Open angle 38 degrees from vertical (tips at hx -/+ 46.5, hy + 54.1), closed angle 8 degrees (tips at hx -/+ 19.8, hy + 65.5). Home: trolley x 300 with a cable length L = 140, so the head center is (300, 238).
- Control panel: three round-cornered buttons, each 96 x 96 units: LEFT (x 56 to 152), RIGHT (x 168 to 264) and DROP (x 480 to 576), all at y 552 to 648; each is at least 56 CSS px wide and tall at both sizes above (96 x 0.609 = 58 px). Between them (x 280 to 470) a counter readout of five lines of 20 unit text, 27 units apart starting at y 540: "PLAYS n", "GRABS n", "MISSES n", "SLIPS n", "WON n" (each under 12 characters, so it fits the 190 unit box). A small arrow and the word "WIN" mark the chute.
- FIRST FRAME (t = 0, no input): the cabinet fills at least 90% of the window height (landscape; in portrait it is 49% of the height and 94% of the width, an accepted exception) with 44 prizes at rest in x 144 to 560, the highest prize top between y 300 and y 420, the claw at home (300, 238) hanging still with its prongs open, the bulbs chasing, all counters 0, no text over the pile.
MOTION / PLAY
- Controls: hold LEFT or RIGHT (the buttons, or ArrowLeft / ArrowRight on window keydown and keyup) to move the trolley; DROP (the button, or Space, ArrowDown or Enter on window keydown) starts one try. Controls only act while no try is running. The buttons are canvas-drawn and work with touch (pointer events).
- Trolley: while a direction is held its target speed is 140 units per second in that direction, otherwise 0; its speed v changes linearly at 700 units per second squared; x is clamped to 64 to 544 (speed 0 at a clamp).
- Pendulum: the claw angle theta (radians, positive = claw swings to the right of vertical) obeys theta'' = -(980 / L) sin(theta) - 1.535 theta' - (a / L) cos(theta), where a is the trolley's acceleration (dv/dt) and L the cable length (140 at home). The head is at (x_t + L sin(theta), 98 + L cos(theta)). Integrate at 240 substeps per second, so the swing decays to about a tenth in 3 s. swing() returns theta in degrees.
- A try (started by DROP, or by grab(x)) runs these phases in order:
1. Brake and straighten (0.5 s): the trolley brakes to a stop at 700 units per second squared, and theta and theta' are multiplied by (1 - smoothstep(0, 0.5, time)) until the claw hangs straight under the trolley.
2. Descend: the head goes straight down at 300 units per second with the prongs open, until its head circle (center (hx, hy + 8), radius 14) touches a prize circle or the floor, or until hy reaches 466, whichever comes first. The open prong tips pass through prizes without pushing them; only the head circle pushes prizes aside.
3. Close (0.6 s): the prongs go from 38 to 8 degrees linearly. At the end the grip test runs: a prize is between the prongs if |px - hx| <= 16 and |py - (hy + 44)| <= 20; if several qualify, the one nearest (hx, hy + 44) is chosen. If one is chosen: grabs +1, a clank, and the prize becomes held (it moves rigidly with the head at the offset it had at that moment; dxg = |px - hx| is remembered). If none: misses +1.
4. Hold 0.2 s, then lift at 300 units per second straight up until the head is back at the home height hy = 238. Slip rule: a held prize with dxg > 8 slips when the head has risen 50 units above its grip height: it becomes a free falling circle that keeps the head's velocity, and slips +1. A held prize with dxg <= 8 never slips.
5. Carry: the trolley moves to x = 92 (the middle of the chute opening) at 140 units per second, the claw swinging and carrying the held prize (if any).
6. Release: once the trolley has stopped at x = 92, wait until |theta| <= 8 degrees or 1.0 s has passed, then open the prongs from 8 to 38 degrees over 0.4 s; a held prize is released at the start of the opening as a free circle with the head's velocity. Releasing at the chute never counts as a slip.
7. After a further 1.2 s the try ends: the claw stays at x = 92 and the controls are live again. busy() is true from the start of a try to its end.
- Free prizes: gravity 1400 units per second squared, 4 substeps per frame (dt = 1/240 s), circle-circle position-based collisions with 8 iterations per substep, restitution 0.15, velocity damping 0.98 on contact; walls, floor, divider (a rectangle) and the kinematic head circle and held prize push free prizes. A held prize is carried over the divider and never collides with it while held. Nothing may pass through a wall: the fastest fall is about 990 units per second, 4.1 units per substep.
- Win: when a free prize's center goes below y = 548 while x is between 48 and 136, the prize is removed, won +1, 60 confetti pieces (3 to 6 units, the six prize colors) burst from the chute for 2.0 s, and the win jingle is triggered (jingles +1). prizes() only lists prizes still in the machine, so prizes().length = 44 minus won.
- The hook also exposes grab(x) (counts as pressing DROP: the trolley jumps to x with the claw hanging still at home, then a try starts at phase 2), prizes() returning {id, x, y, r, vx, vy} for every prize, busy(), heldId() (id of the held prize or null), swing() (degrees), trolleyX(), clawPos() returning {x, y} (the head center), prongs() (current prong angle in degrees), confetti() returning {count} (pieces alive), attract(on) (false turns the demo off), and layout() returning { cabinet, buttons: { left, right, drop } } where each is {x, y, w, h} in CSS px. Hook calls are not user input and do not pause the demo. setCameraOrbit(azimuthDeg, elevationDeg, scale) applies the CSS 3D transform perspective(1400px) rotateY(azimuth) rotateX(-elevation) scale(scale) to the canvas, default (0, 0, 1); pointer input only has to work at the default.
- ATTRACT MODE: with no input, a hands-free demo player starts on load and loops every 28 s. It waits 1.5 s. Then it runs tries back to back: it picks the topmost prize (smallest y - r) whose center x is between 164 and 230, holds LEFT or RIGHT through the real control path and lets go when the remaining distance to that prize's x is at most v squared / 1400 (the braking distance), then presses DROP once the trolley has stopped; when the try ends it waits 0.5 s and picks the next prize. It starts no new try after 24.0 s, but a try that is running is allowed to continue. At 28.0 s everything cuts back to the t = 0 state (the pile is rebuilt exactly as at t = 0, the claw back at home) with the counters keeping their values (1.5 + 22.5 + 4.0 = 28 s). Any input takes over; the demo resumes after 8 s with no input.
SOUND
- Moving trolley, descending or lifting claw -> a low motor whir (sawtooth 80 Hz to 120 Hz through a 400 Hz low-pass, gain 0.05) that follows the speed.
- Prongs closing on a grip -> a clank (square 180 Hz, 60 ms, plus a short noise tick). Slip -> a boing (sine 300 Hz falling to 150 Hz in 0.2 s).
- Win -> a jingle: triangle notes C5 523 Hz, E5 659 Hz, G5 784 Hz and C6 1,047 Hz, each 0.12 s long, 0.1 s apart. Button press -> a short click (1 kHz, 20 ms).
- With no AudioContext (before the first tap, and in ?test=1) nothing is played, but the jingle and clank triggers are still counted.
COUNTERS
stats() returns { fps, plays, grabs, misses, slips, won, jingles }.
- plays: +1 when a try starts (DROP accepted while no try is running, the demo's DROP, or grab()). Never on load.
- grabs: +1 when the prongs finish closing with a prize between them.
- misses: +1 when the prongs finish closing with no prize between them.
- slips: +1 when a held prize leaves the claw by the slip rule (never by opening at the chute).
- won: +1 exactly when a prize goes through the chute opening below y = 548.
- jingles: +1 each time the win jingle is triggered, whether or not sound is on. It always equals won.
- On screen: plays, grabs, misses, slips and won in the panel readout, readable in a paused frame and never over the prizes.
CHECKS for ?test=1
With ?test=1, run an accelerated self-test that measures real positions and real events (not self-set flags) and logs "TEST n PASS" or "TEST n FAIL: reason" to the console and to window.__testResults:
Each check starts from a fresh load; advance() steps in 1/60 s increments.
1. At t = 0 (before any advance), after attract(false): all stats() counters are zero; prizes().length is 44; every prize speed is at most 6 units per second (or the prize is asleep); no two prizes overlap by more than 2 units (r1 + r2 - distance <= 2); every prize has x - r >= 142 and x + r <= 562 and y + r <= 526; the highest prize top (y - r) is between 300 and 420; clawPos() is (300, 238) (each within 0.5), swing() is 0 and prongs() is 38 (within 0.5); at 1280x720 layout().cabinet is at least 90% of the window height tall (at 390x844 at least 90% of the window width wide); the three buttons are each at least 56 px wide and tall; after advance(5.0) every prize is still at most 6 units per second (or asleep), the overlap rule still holds and plays is 0.
2. After attract(false), at 1280x720: a real keydown of ArrowRight on window, advance(1.0), then keyup gives trolleyX() 426 (within 3) at the keyup; recording |swing()| every frame for the next 4.5 s gives a peak P1 in the first 1.2 s of at least 8 degrees, a maximum over 3.0 to 4.5 s of at most 0.25 x P1, and at least 2 sign changes of swing() (ignoring values within 0.05 degrees of zero); at the frame of the largest |swing()| clawPos().x is within 1 of trolleyX() + 140 sin(swing()); after 8.0 s in total from the keyup |swing()| is at most 0.5 and trolleyX() is 440 (within 3; the trolley coasts 14 units while braking).
3. After attract(false): grab(92) (over the empty chute opening), then advance until busy() is false (at most 20 s) gives plays 1, misses 1, grabs 0, slips 0, won 0, heldId() null on every frame, and every prize within 0.5 units of where it started; then pick the topmost prize (smallest y - r) with x between 170 and 300 and grab(its x + 12), advance until busy() is false: plays 2, grabs 1, misses 1, slips 1, won 0, prizes().length 44; heldId() was that prize's id during the lift and became null on the same frame slips went to 1; after advance(3.0) more every prize is at most 6 units per second (or asleep) and the overlap rule (at most 2 units) holds again.
4. After attract(false), pick the topmost prize (smallest y - r) with x between 170 and 300 and grab(its x): on the frame the prongs finish closing, heldId() is that prize's id and its x is within 16 of clawPos().x; on every frame while it is held its distance from clawPos() is at most 60; advancing until busy() is false (at most 20 s) gives plays 1, grabs 1, misses 0, slips 0, won 1, jingles 1 and prizes().length 43; on the frame won becomes 1 the delivered prize's last recorded y was between 520 and 560 and its x between 48 and 136; at that frame confetti().count is 60, and 2.2 s after that frame it is 0; on every frame of the run every prize has x - r >= 38 and x + r <= 562, no prize center is inside the divider rectangle (x 136 to 144, y 304 to 524), and any prize whose center is below y = 530 has x between 48 and 136.
5. On a fresh load with the demo running: at t = 1.3 all stats() counters are zero; at t = 3.5 plays is 1 (the first DROP comes after 1.5 s and the second at least 5 s later); at t = 26.0 plays is at least 3, grabs is at least 3, won is at least 2, plays minus (grabs + misses) is 0 or 1 (1 only while busy() is true and the prongs have not yet closed), prizes().length equals 44 minus won, jingles equals won, and the run has zero console errors.
KEEP IT SHORT
- Keep the whole file under about 1,500 lines (soft target) so you can write it in one pass and finish well inside 15 minutes.
- Save lines with helpers, loops and instancing, not by dropping features. Everything above must really work: no placeholders, no TODOs.
BUILD RULES (all required):
- Deliver ONE self-contained HTML file: inline CSS and JS, no build step, no local assets. Libraries only from a CDN. If you use Three.js, use exactly this import map: {"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js","three/addons/":"https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"}}
- It must open straight from disk in desktop Chrome and on an iPhone (Safari), fill the full screen, handle resize and touch, and stay smooth (target 60 fps).
- It must look stunning in the very FIRST frame: no black screen, no loading wait, no tiny subject. The main subject fills at least 50% of the screen height.
- SOUND (required): all sound is made in code with Web Audio (no audio files). Show a small "🔊 TAP FOR SOUND" chip; on the first tap create ONE AudioContext, route everything through a master gain of 0.6 into a DynamicsCompressor, and hide the chip. Also expose enableAudio() on the hook object named at the top.
- Expose the hook object named at the top with: setCameraOrbit(azimuthDeg, elevationDeg, distance) (or an equivalent camera setter), advance(seconds) that steps the simulation exactly that much for still captures, enableAudio(), and stats() returning {fps, ...the counters named above}. Use a fixed random seed of 360 so every run is the same.
- Every on-screen counter must count a real event you can see happen. No fake numbers.
- No text overlapping the subject; any label must be readable in a paused frame.
- No brand logos, no real people, no copyrighted characters, brand names or logos.
FAILURE MODES TO AVOID
- A claw that moves like a rigid stick: no swing when the trolley stops, a swing that never dies down, or one that dies instantly.
- Prizes that overlap, sink into each other or the floor, jitter forever, or fly through the glass, the divider or the floor.
- A grip that is faked: a prize that rises when it is not between the prongs, a prize that follows the claw by magic, or a claw that always wins or always misses.
- A prize that falls through the chute without won going up, or won going up with a confetti burst that is missing; won counted at release instead of at the chute.
- Counters stuck at 0, ticking on a timer, or not matching what happened (plays, grabs, misses, slips, won).
- A pile of fewer than 40 prizes, prizes that are not round, or a cabinet too small to read on a phone; buttons smaller than 56 px.
- Controls that do nothing on touch, or keys that only work after clicking a box.
- Text over the prizes, or a panel you cannot read in a paused frame.
- A demo player that never wins, gets stuck, or crashes in the first 10 s.
- The same variable declared twice, or methods called on objects that don't exist yet.
SELF-TEST LOOP
- If you can run code: build the file, open it, read the console, run ?test=1, and take screenshots at 3 s, 10 s and 30 s at 1280x720 and 390x844. Look at the screenshots and fix what looks wrong. Fix and retest until all 5 checks pass with zero console errors. Mark anything you could not run as UNMEASURED; never invent results.
- If you are chat-only: go through this prompt line by line, check your code against each line, and fix anything that would fail. Then write one corrected, complete file.
- Final answer: ONE complete HTML file in ONE code block, then the 5 checks marked PASS, FAIL or UNMEASURED. No snippets, no "rest unchanged".
Built one? Post it on X and tag @wikiwaynePost on X