#!/usr/bin/env python3 """t1183: rocket flame jitter — sample the flame per frame and save a frame strip. Usage: python3 test/t1183-flame-playwright.py URL TAG Starts a logged-out solo game, freezes the game core (rendering keeps running), places one gray rocket block with a flame and records the flame's frame / flip / rotation / size and its top-centre anchor every animation frame, plus a strip of close-up screenshots. """ import json, sys, statistics from pathlib import Path from playwright.sync_api import sync_playwright from PIL import Image, ImageDraw URL = sys.argv[1] if len(sys.argv) > 1 else "https://projects.timfalken.com/meteors/" TAG = sys.argv[2] if len(sys.argv) > 2 else "live" SHOTS = Path("/workspace/meteors-shots") fails = [] def check(c, m): print(("OK " if c else "FAIL ") + m) if not c: fails.append(m) with sync_playwright() as pw: br = pw.chromium.launch(args=["--use-gl=angle", "--use-angle=swiftshader"]) ctx = br.new_context(viewport={"width": 1280, "height": 800}) page = ctx.new_page() errors = [] page.on("pageerror", lambda e: errors.append(str(e))) page.goto(URL + ("" if URL.endswith("/") else "/") + "index.html?t1183=" + TAG, wait_until="networkidle") has = page.evaluate("() => typeof FlameFx !== 'undefined'") check(has, "FlameFx present in the served game.js") page.locator("#btn-play").click() page.wait_for_selector("#planet-select:not([hidden])", timeout=8000) page.wait_for_timeout(200) page.locator("#btn-planet-go").click() page.wait_for_function("() => window.app && app.mode === 'playing' && app.core", timeout=10000) page.wait_for_timeout(400) info = page.evaluate("""() => { app.core.tick = function () {}; for (const b of app.core.blocks.slice()) { b.y = -50; } const b = app.core.createBlock(4, 7, GameCore.COLOR_GRAY, 'still'); b.flame = true; b.vy = 0; window.__fb = b; const R = window.__flame = { rows: [] }; const loop = () => { const f = app.flames[b.id]; if (f) { const p = { x: f.transform.position.x, y: f.transform.position.y, w: f.transform.size.x, h: f.transform.size.y, rotation: f.transform.rotation, mirrorX: f.renderer.mirrorX }; const top = FlameFx.topCentreOf(p); R.rows.push([performance.now(), f.renderer.subImage, !!f.renderer.mirrorX, +f.transform.rotation.toFixed(3), +f.transform.size.x.toFixed(2), +f.transform.size.y.toFixed(2), +top.x.toFixed(3), +top.y.toFixed(3)]); } requestAnimationFrame(loop); }; requestAnimationFrame(loop); const cv = app.scene.canvas; const c = cv.getBoundingClientRect(); return { tile: app.tile, col: b.col, fieldH: app.fieldH, canvas: [c.left, c.top, c.width, c.height], cw: cv.width, reduced: FlameFx.reducedMotion(), fps: FlameFx.FPS, vars: [FlameFx.SCALE_VAR, FlameFx.WIDTH_VAR, FlameFx.HEIGHT_VAR, FlameFx.ROT_VAR_DEG] }; }""") page.wait_for_timeout(500) # Close-up strip around the rocket. t = info["tile"]; cl, ct, cwid, ch = info["canvas"]; k = cwid / info["cw"] cx = cl + (info["col"] * t + t / 2) * k topy = ct + (info["fieldH"] - 7 * t - t * 0.5) * k clip = {"x": cx - 2.0 * t * k, "y": topy - 0.4 * t * k, "width": 4.0 * t * k, "height": 3.6 * t * k} frames = [] for i in range(12): p = SHOTS / f"t1183-{TAG}-frame-{i:02d}.png" page.screenshot(path=str(p), clip=clip) frames.append(p) page.wait_for_timeout(45) page.wait_for_timeout(1500) rows = page.evaluate("() => window.__flame.rows") page.screenshot(path=str(SHOTS / f"t1183-{TAG}-full.png")) ctx.close() # Reduced-motion context: old rate, no flip / scale / rotation. ctx2 = br.new_context(viewport={"width": 1280, "height": 800}, reduced_motion="reduce") p2 = ctx2.new_page() p2.goto(URL + ("" if URL.endswith("/") else "/") + "index.html", wait_until="networkidle") red = p2.evaluate("""() => { const s = FlameFx.create(4, Math.random, FlameFx.reducedMotion()); const r = []; for (let i = 0; i < 40; i++) { FlameFx.advance(s, 0.05, Math.random, FlameFx.reducedMotion()); r.push([s.flip, s.rot, s.scale * s.w * s.h]); } return { reduced: FlameFx.reducedMotion(), anyJitter: r.some(x => x[0] || x[1] !== 0 || x[2] !== 1) }; }""") ctx2.close() br.close() # Strip image. ims = [Image.open(p) for p in frames] w, h = ims[0].size strip = Image.new("RGB", (w * len(ims), h + 18), (12, 12, 24)) d = ImageDraw.Draw(strip) for i, im in enumerate(ims): strip.paste(im, (i * w, 18)) d.text((i * w + 4, 3), f"#{i + 1}", fill=(220, 220, 220)) strip_path = SHOTS / f"t1183-{TAG}-strip.png" strip.save(strip_path) # Analysis over the sampled frames (only frames where the pose changed = ticks). ticks = [b for a, b in zip(rows, rows[1:]) if a[1:6] != b[1:6]] dur = (rows[-1][0] - rows[0][0]) / 1000 if len(rows) > 1 else 1 tick_rate = len(ticks) / dur frame_changes = sum(1 for a, b in zip(rows, rows[1:]) if a[1:6] != b[1:6] and a[1] != b[1]) same_frame_ticks = sum(1 for a, b in zip(rows, rows[1:]) if a[1:6] != b[1:6] and a[1] == b[1]) anchor_x = [r[6] for r in rows]; anchor_y = [r[7] for r in rows] rots = [r[3] for r in rows]; flips = [r[2] for r in rows] ws = [r[4] for r in rows]; hs = [r[5] for r in rows] summary = { "url": URL, "fps_constant": info["fps"], "variation": dict(zip(["scale", "width", "height", "rotation_deg"], info["vars"])), "reduced_motion_in_main_ctx": info["reduced"], "samples": len(rows), "seconds": round(dur, 2), "measured_tick_rate_hz": round(tick_rate, 1), "frames_used": sorted(set(r[1] for r in rows)), "ticks_with_same_frame": same_frame_ticks, "flip_share": round(sum(flips) / len(flips), 2), "rotation_range": [min(rots), max(rots)], "width_range": [min(ws), max(ws)], "height_range": [min(hs), max(hs)], "anchor_spread_px": [round(max(anchor_x) - min(anchor_x), 3), round(max(anchor_y) - min(anchor_y), 3)], "reduced_motion_ctx": red, "errors": errors, "strip": str(strip_path), } (SHOTS / f"t1183-{TAG}-metrics.json").write_text(json.dumps({"summary": summary, "rows": rows}, indent=1)) print(json.dumps(summary, indent=1)) check(len(rows) > 60, f"sampled {len(rows)} frames") check(15 <= tick_rate <= 30, f"tick rate ~25 Hz (measured {tick_rate:.1f}; capped by the headless frame rate)") check(set(summary["frames_used"]) == {0, 1, 2, 3}, "all 4 sprite frames used") check(same_frame_ticks == 0, "every tick switches to a different frame") check(0.3 <= summary["flip_share"] <= 0.7, f"flips about half the time ({summary['flip_share']})") check(max(abs(min(rots)), abs(max(rots))) <= 2.5 + 1e-6 and max(rots) - min(rots) > 2, "rotation within ±2.5° and varies") check(summary["anchor_spread_px"][0] < 0.01 and summary["anchor_spread_px"][1] < 0.01, f"top-centre anchor fixed {summary['anchor_spread_px']}") check(red["reduced"] and not red["anyJitter"], "reduced motion: no flip/scale/rotation") check(not errors, f"no page errors {errors[:2]}") print("ALL OK" if not fails else f"{len(fails)} FAIL") sys.exit(1 if fails else 0)