Cars that follow the rules (no red runs, no overlaps) and a tap that retimes the light without cutting a running green.
Cars that follow the rules (no red runs, no overlaps) and a tap that retimes the light without cutting a running green.
No runs posted yet. Be the first: post yours and tag @wikiwayne.
FOUR-WAY SIGNALS: Cars, Queues and a Tap-to-Retime Light
HOOK OBJECT: window.__signals
Build "FOUR-WAY SIGNALS": a top-down Canvas 2D (no libraries) road intersection where cars drive in from all four sides, obey a real traffic-light cycle, line up in queues at red and pull away together at green. Tap anywhere to change how long each green lasts and watch the queues shrink or explode. It is fun to keep watching because the queues breathe with the lights.
LOOK
- Style: clean top-down toy-map, flat shapes with soft 3 px drop shadows. Palette: grass #7FB069 with darker square blocks #6AA157, asphalt #3A3F47, lane dashes #F3E7B0, stop lines and crosswalk stripes #FFFFFF, signal lamps red #E63946, yellow #FFC857, green #4ADE80, car colors #E4572E #2E86AB #F6C945 #6DB65B #9B5DE5 #F1F1F1 #FF8FA3 #2A9D8F.
- World units (u): all numbers below are for a 1280x720 canvas, where 1 u = 1 px. On any other size, scale everything by min(width, height) / 720 and keep the intersection at the screen center.
- Roads: a vertical road x 580-700 and a horizontal road y 300-420, both running off every screen edge, so the intersection and its four roads fill the whole screen. Drive on the right: southbound lane center x = 610, northbound x = 670, eastbound y = 390, westbound y = 330. Stop lines (the car's front must stay behind them on red): eastbound x = 570, westbound x = 710, southbound y = 290, northbound y = 430.
- Cars: rounded rectangles 40 u long and 22 u wide, with a dark windshield #1B2A34, and brake lights that glow red while the car slows or waits. No two cars share a color with the car directly ahead of them.
- Signals: four signal heads, one beside each stop line and 4 u off the asphalt, each a dark box 16 x 44 u with three lamps. The lit lamp is full color; the other two are at 25% opacity.
- HUD: top-left grass area (inside x 24-420, y 24-100), 14 px bold text on a rounded translucent card: "PASSED n · STOPS n · RED RUNS n" on the first line and "GREEN 6 s · TAP TO RETIME" on the second. Never over the roads.
- FIRST FRAME (t = 0, no input): the north-south signal is green and the east-west signal is red. There are exactly 10 cars. Waiting at red, speed 0, in two queues: 3 eastbound cars with fronts at x = 566, 512 and 458, and 3 westbound cars with fronts at x = 714, 768 and 822 (each 4 u behind its stop line, 14 u between cars). Moving at 90 u/s toward the green: 2 southbound cars with fronts at y = 170 and 90, and 2 northbound cars with fronts at y = 550 and 630. The roads fill 100% of the screen height.
MOTION / PLAY
- Controls: tap, click or press Space anywhere on the map (the sound chip excluded) to cycle the green length 6 s, 9 s, 4 s, 6 s and so on; a small ring ripples out from the tap point and the HUD line updates. A new length applies from the next green; the green already running keeps its length.
- Signal cycle with the default 6 s green: NS green 6 s, NS yellow 2 s, all red 2 s, EW green 6 s, EW yellow 2 s, all red 2 s, then repeat (20 s per cycle; with a green of G seconds a cycle is 2G + 8 s). The cycle starts at t = 0 with NS green age 0. "Red" for an approach means its own signal is red, which includes both all-red periods.
- Cars go straight only. Each has a seeded desired speed between 80 and 100 u/s, accelerates at 60 u/s², and never brakes harder than 120 u/s². Every car keeps at least 14 u of bumper-to-bumper gap when stopped and a time gap of about 0.4 s when moving, so consecutive cars in a lane never get closer than 8 u.
- Stop rule, every frame: a car whose front is behind its stop line stops with its front 4 u behind the line (or behind the car ahead) if its signal is red, or yellow and (distance from its front to the line - 4) is at least v² / 240 (v in u/s). Otherwise it keeps going. A stopped lead car starts moving 0.3 s after its signal turns green; each follower starts 0.4 s after the car ahead has moved more than 5 u.
- Spawning: each approach adds one car every 3.0 s, with its front at the screen edge (so it enters from off-screen): eastbound at t = 0.75, 3.75, 6.75 ..., northbound at 1.5, 4.5, 7.5 ..., westbound at 2.25, 5.25, 8.25 ..., southbound at 3.0, 6.0, 9.0 .... A spawn waits until the lane has 14 u free behind the last car. A car is removed when its rear passes the far screen edge. Exits are always free, so the intersection never jams.
- At 90 u/s a car moves 1.5 u per 1/60 s frame, so a stop-line crossing must be detected by comparing the car's front before and after each step, not by sampling a thin band.
- Camera: setCamera(zoom, cx, cy) in world units, default (1, 640, 360); dragging is not used.
- ATTRACT MODE: with no input, a hands-free demo starts on load and loops every 29 s: it just runs the lights for 2.0 s, taps to change the green to 9 s at t = 2.0 s, runs the long cycle, taps again at t = 22.0 s to change it to 4 s (that green starts at 23.0 s), and runs until t = 29.0 s, when everything cuts back to the t = 0 state (2 + 20 + 7 = 29 s). Any input takes over; the demo resumes after 8 s with no input. Counters keep their values across the cut, except that the cut also resets the green length to 6 s.
- The hook also exposes tap() (same code path as a real tap), attract(on) (false turns the demo off), lightState() returning { phase, age } where phase is one of NS_GREEN, NS_YELLOW, ALL_RED, EW_GREEN, EW_YELLOW, cars() returning an array of { id, dir, x, y, front, speed } (x, y = center in u; front = the nose coordinate on the travel axis; speed in u/s, never negative), and layout() returning { scale, cx, cy } (cx, cy = screen center of the intersection in CSS px). Hook calls are not user input and do not pause the demo.
SOUND
- Every signal change → a short tick: 880 Hz for green, 660 Hz for yellow, 440 Hz for red (60 ms each).
- A car crossing its stop line on green or yellow → a soft filtered-noise whoosh (150 ms).
- A car stopping at a red → a faint low brake hiss (80 ms).
- A tap → a wooden "tock" (180 Hz, 90 ms).
- A red run → a short honk (400 Hz square, 200 ms).
COUNTERS
stats() returns { fps, passed, stops, redRuns, retimes, cycles, green }.
- passed: +1 exactly when a car's rear passes the far screen edge and the car disappears. Never on a timer, never on load, and not for cars removed by the attract cut.
- stops: +1 each time a car that has been moving above 10 u/s comes to a complete stop (speed 0) at a red or behind a queue. The 6 cars waiting at t = 0 do not count until they move and stop again.
- redRuns: +1 each time a car's front crosses its stop line while its own signal has been red for at least 0.1 s. A correct build keeps this at 0.
- retimes: +1 for every tap (real, demo or hook) that changes the green length.
- cycles: +1 each time a green phase begins (NS_GREEN or EW_GREEN), not counting the NS green already running at load.
- green: the green length currently set, in seconds (6, 9 or 4).
- On screen: passed, stops, redRuns and green in the HUD card, readable in a paused frame.
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: the canvas fills the window and layout().scale equals min(width, height) / 720; lightState() is NS_GREEN with age 0; cars() has exactly 10 cars; the 6 east-west cars have speed 0 and fronts within 1 u of 566, 512, 458 (east) and 714, 768, 822 (west); the 4 north-south cars have speed 90 (within 1) and fronts within 1 u of 170, 90 (south) and 550, 630 (north); stats() is all zeros except green = 6.
2. After attract(false) and advance(59.5) at the default timing: redRuns is 0; cycles is exactly 5 (green phases begin at 10, 20, 30, 40 and 50 s); and at every frame no two car rectangles overlap (including inside the intersection) and every pair of consecutive cars in one lane has at least 8 u of gap.
3. After attract(false), advance(9.5): the leading east-west car on each side is still stopped (speed 0) with its front within 1 u of 566 and 714, every stopped east-west car has a gap of 12 to 20 u to the car ahead, and stops is at least 3. Then advance(1.5) more (t = 11.0): both lead east-west cars have speed above 10 u/s and fronts past their stop lines (past x = 570 eastbound, below x = 710 westbound), and redRuns is 0.
4. After attract(false), tap() once at t = 0 (green becomes 9, retimes 1): lightState().phase is EW_GREEN after advance(12), EW_YELLOW after 8 s more (t = 20), ALL_RED after 2 s more (t = 22) and NS_GREEN after 2 s more (t = 24), with cycles equal to 2 at t = 24; then two more tap() calls give green 4 and then green 6, and retimes is 3.
5. On a fresh load with the demo running, and watching cars() every frame: at t = 1.9 retimes is 0; at t = 2.4 retimes is 1 and green is 9; at t = 21.9 cycles is 1 and retimes is 1; at t = 22.4 retimes is 2 and green is 4; at t = 24.0 lightState().phase is NS_GREEN and cycles is 2; at t = 28.5 passed is at least 6 and equals the number of cars that left cars() with their rear beyond a screen edge, 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
- Cars that drive through a red light, or a lamp color that does not match the rule the cars actually follow.
- Cars overlapping each other, merging into one blob in a queue, or passing through cross traffic inside the intersection.
- Cars that stop in the middle of the box, or start while the car ahead is still stopped (a queue that moves as one rigid block).
- All cars moving in lockstep at the same speed, or a queue that releases all at once instead of one by one.
- A tap that cuts the running green short, resets the cars, or changes nothing you can see in the queues.
- Stop-line crossings missed because a car moves more than the sampling band in one frame, so redRuns or passed drift away from what is on screen.
- Cars popping into existence on screen instead of entering from an edge, or vanishing before their rear leaves the screen.
- Counters that tick on a timer, or a HUD that sits over the roads or is too small to read.
- 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