Image Segmentation
PyTorch
English
painting-vision-robotics-kit
semantic-segmentation
robotics
edge-ai
construction-ai
autonomous-painting
wall-painting-robot
paint-coverage-estimation
building-facade
drywall
skirting-detection
window-detection
lidar
depth-validation
segformer
deeplabv3
Eval Results (legacy)
Update code: real-machine G-code adapter, commissioning gate, machine_job runner, pump watchdog firmware, twin completion gate, realism and masking modules (card and weights unchanged)
4eecd03 verified Download console/demo.js from constructelligence/painting-vision-robotics-kit: direct link, hf CLI and curl.
- Browser
- Download file 45.7 kB
-
https://huggingface.co/constructelligence/painting-vision-robotics-kit/resolve/main/console/demo.js
- Command line
-
hf download hf://constructelligence/painting-vision-robotics-kit/console/demo.js
-
curl -L -o demo.js https://huggingface.co/constructelligence/painting-vision-robotics-kit/resolve/main/console/demo.js
45.7 kB
| ; | |
| // The phone demo. It replays plan.motion (path_motion.timeline: every stroke, step-over, lift and hop with the | |
| // acceleration the machine would have) over the photo, so the tool is always exactly where the plan says it is. | |
| // Paint takes the wall's own lighting (shadows, texture) instead of lying on it as a flat colour. | |
| // It is a picture of the plan, not a machine moving: nothing here is measured. | |
| const $ = (id) => document.getElementById(id); | |
| const SCAN_S = 1.6, TAPE_S = 5.0, FILM_S = 1.2, PAINT_S = 10.0, DONE_S = 1.5; | |
| const TOTAL_S = SCAN_S + TAPE_S + FILM_S + PAINT_S + DONE_S; | |
| const COLOURS = ["#e9e4d8", "#cfd8dc", "#9fb8a5", "#7da0c4", "#d9a79a", "#2f3e4e"]; | |
| const app = document.querySelector(".app"); | |
| const canvas = $("canvas"), ctx = canvas.getContext("2d"); | |
| const mk = () => { const c = document.createElement("canvas"); return [c, c.getContext("2d", { willReadFrequently: true })]; }; | |
| const [strokeLayer, sctx] = mk(), [coat, cctx] = mk(), [wetLayer, wctx] = mk(), [regionScratch, rctx] = mk(), [smallLayer, smctx] = mk(); | |
| let plan = null, photo = null, paintLayer = null, regionsImg = null, tinted = null, pathLayer = null; | |
| let shown = {}, colour = COLOURS[0], pinnedOn = false; | |
| let tNow = 0, playing = false, speed = 1, lastTs = 0, raf = 0, recorder = null, chunks = []; | |
| let track = [], machineTotal = 0, maskSmall = null, maskCount = 1, cover = 0, coverAt = -1; | |
| let toolKind = "roller", zMax = 150, showPath = false, compare = false, cmpX = 0.5, lastDir = { dx: 1, dy: 0 }; | |
| const ease = (x) => x < 0 ? 0 : x > 1 ? 1 : x * x * (3 - 2 * x); | |
| const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| const fmtClock = (s) => `${String(Math.floor(s / 60)).padStart(2, "0")}:${String(Math.floor(s % 60)).padStart(2, "0")}`; | |
| function show(err) { $("err").hidden = !err; $("err").textContent = err || ""; } | |
| function phase(name, text) { app.dataset.phase = name; $("phase").textContent = text; } | |
| function img(src) { | |
| return new Promise((res, rej) => { const i = new Image(); i.onload = () => res(i); i.onerror = () => rej(new Error("could not load " + src)); i.src = src; }); | |
| } | |
| async function api(path, options) { | |
| const r = await fetch(path, options); | |
| const body = await r.json().catch(() => ({})); | |
| if (!r.ok) throw new Error(body.error || r.statusText); | |
| return body; | |
| } | |
| // ---------------------------------------------------------------------------------------------------- loading | |
| async function start(file) { | |
| if (!file) return; | |
| show(""); | |
| $("start").hidden = true; $("stage-wrap").hidden = false; $("result").hidden = true; | |
| phase("scan", "Scanning"); | |
| $("scan").hidden = false; $("caption").textContent = "Finding the wall, windows and fittings…"; | |
| try { | |
| const bytes = await file.arrayBuffer(); | |
| const job = await api("/api/jobs?filename=" + encodeURIComponent(file.name || "photo.jpg"), { method: "POST", body: bytes }); | |
| plan = await api(`/api/jobs/${job.id}/demo`); | |
| if (!plan.ok) throw new Error(plan.reason || "no wall found"); | |
| photo = await img(`/api/jobs/${job.id}/photo.jpg`); | |
| paintLayer = await img(`/api/jobs/${job.id}/demo_paint.png`); | |
| regionsImg = await img(`/api/jobs/${job.id}/demo_regions.png`); | |
| shown = Object.fromEntries(plan.regions.map((r) => [r.class, true])); | |
| legend(); | |
| } catch (e) { | |
| show(e.message); $("start").hidden = false; $("stage-wrap").hidden = true; phase("start", ""); return; | |
| } | |
| const W = Math.min(1080, photo.naturalWidth), H = Math.round(photo.naturalHeight * W / photo.naturalWidth); | |
| for (const c of [canvas, strokeLayer, coat, wetLayer]) { c.width = W; c.height = H; } | |
| if (!plan.motion_spray) toolKind = "roller"; | |
| $("t-spray").disabled = !plan.motion_spray; $("t-roller").setAttribute("aria-pressed", String(toolKind === "roller")); $("t-spray").setAttribute("aria-pressed", String(toolKind === "spray")); | |
| buildTrack(); buildMaskSmall(); buildTint(); buildPathLayer(); | |
| $("scan").hidden = true; $("compare").hidden = true; | |
| compare = false; $("compare").setAttribute("aria-pressed", "false"); | |
| play(true); | |
| } | |
| /** Phases laid end to end on one machine clock; each move keeps its own trapezoid (ramp) for exact position. */ | |
| function buildTrack() { | |
| track = []; let off = 0; | |
| const motion = toolKind === "spray" && plan.motion_spray ? plan.motion_spray : plan.motion; | |
| for (const p of (motion && motion.phases) || []) { | |
| const mmOf = (m) => Math.hypot((m.b[0] - m.a[0]) * plan.shape[1], (m.b[1] - m.a[1]) * plan.shape[0]) * plan.scale.mm_per_px; | |
| let odo = 0; const cum = p.moves.map((m) => { const o = odo; if (m.k === "paint" || m.k === "step") odo += mmOf(m); return o; }); | |
| let since = 0; const sinceAt = p.moves.map((m, i) => { const v = since; if (m.k === "reload") since = 0; else if (m.k === "paint" || m.k === "step") since += mmOf(m); return v; }); | |
| const f = p.force_n || [0, 0]; | |
| track.push({ tool: p.tool, width: p.width, off, end: off + p.seconds, moves: p.moves, cum, mmOf, sinceAt, reloadMm: p.reload_mm || 0, force: (f[0] + f[1]) / 2, | |
| standoff: p.standoff_mm || 0, paintMoves: p.moves.filter((m) => m.k === "paint").length }); | |
| off += p.seconds; | |
| } | |
| machineTotal = off; | |
| zMax = Math.max(140, ...track.flatMap((ph) => ph.moves.map((m) => Math.max(m.z0, m.z1))) ) * 1.18; | |
| } | |
| const k_ = () => machineTotal / PAINT_S; // machine seconds per demo second at 1x | |
| function frac(m, tm) { // distance fraction at machine time tm inside move m | |
| const u = clamp((tm - m.t0) / Math.max(1e-9, m.t1 - m.t0), 0, 1), r = m.ramp; | |
| if (r <= 0 || r >= 0.5) return u < 0.5 ? 2 * u * u : 1 - 2 * (1 - u) * (1 - u); | |
| const v = 1 / (1 - r); | |
| return u < r ? v * u * u / (2 * r) : u <= 1 - r ? v * (r / 2 + (u - r)) : 1 - v * (1 - u) * (1 - u) / (2 * r); | |
| } | |
| const lerpPt = (a, b, f) => [a[0] + (b[0] - a[0]) * f, a[1] + (b[1] - a[1]) * f]; | |
| // ---------------------------------------------------------------------------------------------------- paint look | |
| /** The photo's own light on the chosen colour: low-pass luminance (shadows, gradients) + fine detail + a little nap. */ | |
| function buildTint() { | |
| const W = canvas.width, H = canvas.height; | |
| const [pc, px] = mk(); pc.width = W; pc.height = H; px.drawImage(photo, 0, 0, W, H); | |
| const src = px.getImageData(0, 0, W, H).data; | |
| const [lc, lx] = mk(); lc.width = Math.max(8, Math.round(W / 14)); lc.height = Math.max(8, Math.round(H / 14)); | |
| lx.imageSmoothingQuality = "high"; lx.drawImage(photo, 0, 0, lc.width, lc.height); | |
| const [uc, ux] = mk(); uc.width = W; uc.height = H; ux.imageSmoothingQuality = "high"; ux.drawImage(lc, 0, 0, W, H); | |
| const low = ux.getImageData(0, 0, W, H).data; | |
| const [mc, mx] = mk(); mc.width = W; mc.height = H; mx.drawImage(paintLayer, 0, 0, W, H); | |
| const mask = mx.getImageData(0, 0, W, H).data; | |
| let sum = 0, n = 0; | |
| for (let i = 0; i < mask.length; i += 4) if (mask[i + 3] > 128) { sum += low[i] * 0.3 + low[i + 1] * 0.59 + low[i + 2] * 0.11; n++; } | |
| const ref = Math.max(40, n ? sum / n : 160); | |
| const out = new ImageData(W, H), o = out.data; | |
| tinted = { ref, shade: new Float32Array(W * H), mask: new Uint8Array(W * H) }; | |
| for (let i = 0, p = 0; i < mask.length; i += 4, p++) { | |
| const l = low[i] * 0.3 + low[i + 1] * 0.59 + low[i + 2] * 0.11, f = src[i] * 0.3 + src[i + 1] * 0.59 + src[i + 2] * 0.11; | |
| const h = ((p * 2654435761) >>> 0) / 4294967296; // fixed nap, same on every replay | |
| tinted.shade[p] = clamp(1 + (l / ref - 1) * 0.65 + (f - l) / ref * 0.3, 0.4, 1.4) * (0.985 + 0.03 * h); | |
| tinted.mask[p] = mask[i + 3]; | |
| } | |
| tint(out); tinted.data = out; | |
| } | |
| function tint(out) { | |
| const o = out.data, W = canvas.width, H = canvas.height; | |
| const r = parseInt(colour.slice(1, 3), 16), g = parseInt(colour.slice(3, 5), 16), b = parseInt(colour.slice(5, 7), 16); | |
| for (let p = 0, i = 0; p < W * H; p++, i += 4) { | |
| const s = tinted.shade[p]; | |
| o[i] = Math.min(255, r * s); o[i + 1] = Math.min(255, g * s); o[i + 2] = Math.min(255, b * s); o[i + 3] = tinted.mask[p]; | |
| } | |
| const [c, x] = mk(); c.width = W; c.height = H; x.putImageData(out, 0, 0); tinted.canvas = c; | |
| } | |
| function buildMaskSmall() { | |
| const sw = 240, sh = Math.max(1, Math.round(240 * canvas.height / canvas.width)); | |
| smallLayer.width = sw; smallLayer.height = sh; | |
| smctx.clearRect(0, 0, sw, sh); smctx.drawImage(paintLayer, 0, 0, sw, sh); | |
| const d = smctx.getImageData(0, 0, sw, sh).data; | |
| maskSmall = new Uint8Array(sw * sh); maskCount = 0; | |
| for (let i = 0; i < maskSmall.length; i++) { maskSmall[i] = d[i * 4 + 3] > 128 ? 1 : 0; maskCount += maskSmall[i]; } | |
| maskCount = Math.max(1, maskCount); coverAt = -1; | |
| } | |
| /** What fraction of the paintable wall carries paint right now: measured from the drawn strokes, not from the clock. */ | |
| function coverage() { | |
| if (coverAt === Math.round(tNow * 20)) return cover; | |
| coverAt = Math.round(tNow * 20); | |
| const sw = smallLayer.width, sh = smallLayer.height; | |
| smctx.clearRect(0, 0, sw, sh); smctx.drawImage(strokeLayer, 0, 0, sw, sh); | |
| const d = smctx.getImageData(0, 0, sw, sh).data; let n = 0; | |
| for (let i = 0; i < maskSmall.length; i++) if (maskSmall[i] && d[i * 4 + 3] > 100) n++; | |
| cover = n / maskCount; | |
| return cover; | |
| } | |
| function buildPathLayer() { | |
| const W = canvas.width, H = canvas.height; pathLayer = document.createElement("canvas"); pathLayer.width = W; pathLayer.height = H; | |
| const c = pathLayer.getContext("2d"); | |
| for (const ph of track) { | |
| c.lineWidth = Math.max(1.5, W / 420); c.lineCap = "round"; | |
| for (const m of ph.moves) { | |
| if (m.k !== "paint" && m.k !== "step" && m.k !== "travel") continue; | |
| c.setLineDash(m.k === "paint" ? [] : [W / 90, W / 120]); | |
| c.strokeStyle = m.k === "paint" ? (ph.tool === "brush" ? "rgba(255,214,10,.85)" : "rgba(10,132,255,.8)") : "rgba(255,255,255,.6)"; | |
| c.beginPath(); c.moveTo(m.a[0] * W, m.a[1] * H); c.lineTo(m.b[0] * W, m.b[1] * H); c.stroke(); | |
| if (m.k === "paint" && Math.hypot(m.b[0] - m.a[0], m.b[1] - m.a[1]) * W > 90) { // a chevron at the middle shows direction | |
| const mx = (m.a[0] + m.b[0]) / 2 * W, my = (m.a[1] + m.b[1]) / 2 * H, a = Math.atan2(m.b[1] - m.a[1], m.b[0] - m.a[0]), s = W / 90; | |
| c.setLineDash([]); c.beginPath(); | |
| c.moveTo(mx - Math.cos(a) * s - Math.sin(a) * s, my - Math.sin(a) * s + Math.cos(a) * s); c.lineTo(mx, my); | |
| c.lineTo(mx - Math.cos(a) * s + Math.sin(a) * s, my - Math.sin(a) * s - Math.cos(a) * s); c.stroke(); | |
| } | |
| } | |
| } | |
| } | |
| // ---------------------------------------------------------------------------------------------------- playback | |
| function play(fromStart) { | |
| if (fromStart) tNow = 0; | |
| if (tNow >= TOTAL_S - 1e-6) tNow = 0; | |
| playing = true; $("result").hidden = true; lastTs = 0; $("play").textContent = "Pause"; | |
| cancelAnimationFrame(raf); raf = requestAnimationFrame(tick); | |
| } | |
| function pause() { playing = false; $("play").textContent = "Play"; } | |
| function tick(now) { | |
| if (playing) { | |
| if (lastTs) tNow = Math.min(TOTAL_S, tNow + (now - lastTs) / 1000 * speed); | |
| lastTs = now; | |
| if (tNow >= TOTAL_S) { playing = false; $("play").textContent = "Replay"; draw(TOTAL_S); finish(); return; } | |
| } | |
| draw(tNow); | |
| if (playing) raf = requestAnimationFrame(tick); | |
| } | |
| const redraw = () => { if (plan && !playing) draw(tNow); }; | |
| // ---------------------------------------------------------------------------------------------------- drawing | |
| function strokePartialTo(c, a, b, f, W, H) { c.beginPath(); c.moveTo(a[0] * W, a[1] * H); const e = lerpPt(a, b, f); c.lineTo(e[0] * W, e[1] * H); c.stroke(); } | |
| function polylineLen(pts) { let s = 0; for (let i = 1; i < pts.length; i++) s += Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]); return s; } | |
| function strokePartial(c, pts, f, W, H) { | |
| const total = polylineLen(pts) * f; let acc = 0; c.beginPath(); c.moveTo(pts[0][0] * W, pts[0][1] * H); | |
| for (let i = 1; i < pts.length; i++) { | |
| const seg = Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]); | |
| if (acc + seg <= total) { c.lineTo(pts[i][0] * W, pts[i][1] * H); acc += seg; continue; } | |
| const q = seg ? (total - acc) / seg : 0, e = lerpPt(pts[i - 1], pts[i], q); c.lineTo(e[0] * W, e[1] * H); break; | |
| } | |
| c.stroke(); | |
| } | |
| function tipOf(pts, f) { | |
| const total = polylineLen(pts) * clamp(f, 0, 1); let acc = 0; | |
| for (let i = 1; i < pts.length; i++) { | |
| const seg = Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]); | |
| if (acc + seg >= total || i === pts.length - 1) { | |
| const q = seg ? clamp((total - acc) / seg, 0, 1) : 0, e = lerpPt(pts[i - 1], pts[i], q); | |
| return { x: e[0], y: e[1], dx: pts[i][0] - pts[i - 1][0], dy: pts[i][1] - pts[i - 1][1] }; | |
| } | |
| acc += seg; | |
| } | |
| return { x: pts[0][0], y: pts[0][1], dx: 1, dy: 0 }; | |
| } | |
| const [doneLayer, dctx] = mk(); | |
| let lay = { sig: "", tau: -1, pi: 0, mi: 0 }; | |
| const smooth = (x) => x * x * (3 - 2 * x); | |
| const vnoise = (seed, x) => { const i = Math.floor(x), f = x - i; return hash(seed, i) * (1 - smooth(f)) + hash(seed, i + 1) * smooth(f); }; | |
| /** A hand-like brush stroke: loads on, presses in over the first 14 %, lifts off over the last 20 % (the bristles | |
| * splay when pressed and narrow as they lift), the line drifts a little, and the paint thins as the brush empties. */ | |
| function brushStroke(c, a, b, f, W, H, seed, wpx, load) { | |
| const dx = (b[0] - a[0]) * W, dy = (b[1] - a[1]) * H, len = Math.hypot(dx, dy); | |
| if (len < 1) return; | |
| const nx = -dy / len, ny = dx / len, n = Math.max(2, Math.ceil(len / 4 * f)); | |
| let prev = null; | |
| c.lineCap = "round"; c.lineJoin = "round"; | |
| for (let j = 0; j <= n; j++) { | |
| const u = (j / n) * f, press = smooth(Math.min(1, u / 0.14, (1 - u) / 0.2, 1)); | |
| const wob = (vnoise(seed, u * 5) - 0.5) * wpx * 0.2 * (0.4 + 0.6 * press); | |
| const pt = [a[0] * W + dx * u + nx * wob, a[1] * H + dy * u + ny * wob]; | |
| if (prev) { | |
| c.lineWidth = Math.max(1.5, wpx * (0.78 + 0.22 * press) * (0.94 + 0.12 * vnoise(seed + 7, u * 9))); | |
| c.globalAlpha = (0.62 + 0.36 * load) * (0.9 + 0.1 * press); | |
| c.beginPath(); c.moveTo(prev[0], prev[1]); c.lineTo(pt[0], pt[1]); c.stroke(); | |
| } | |
| prev = pt; | |
| } | |
| c.globalAlpha = 1; | |
| } | |
| /** One finished (f = 1) or running move into canvas c, in the tool's own manner. */ | |
| function strokeMove(c, ph, pi, mi, m, f, W, H) { | |
| if (m.k !== "paint" && m.k !== "step") return; | |
| const wpx = Math.max(ph.tool === "brush" ? 3 : 4, ph.width * W), load = ph.reloadMm ? clamp(1 - ph.sinceAt[mi] / ph.reloadMm, 0, 1) : 1; | |
| c.strokeStyle = "#fff"; | |
| if (ph.tool === "spray") { // a fan builds up softly: faint wide halo + denser core, edges feather | |
| c.lineCap = "butt"; | |
| for (const [wf, al] of [[1.22, 0.3], [1.0, 0.5], [0.72, 0.55]]) { c.lineWidth = wpx * wf; c.globalAlpha = al; strokePartialTo(c, m.a, m.b, f, W, H); } | |
| c.globalAlpha = 1; | |
| } else if (ph.tool === "brush") { | |
| brushStroke(c, m.a, m.b, f, W, H, pi * 100003 + mi, wpx, load); | |
| } else { // a roller lays down less as it dries out between dips | |
| c.lineCap = "butt"; c.lineWidth = wpx; c.globalAlpha = 0.74 + 0.22 * load; strokePartialTo(c, m.a, m.b, f, W, H); c.globalAlpha = 1; | |
| } | |
| } | |
| /** Lay every stroke the machine has finished by machine time tau into strokeLayer (alpha = where paint is) and the | |
| * last moments into wetLayer (where it still shines). Finished strokes are cached, so playback only draws new ones. | |
| * Returns the move running now. */ | |
| function layPaint(tau, W, H) { | |
| const sig = `${toolKind}|${W}x${H}|${machineTotal}`; | |
| if (lay.sig !== sig || tau < lay.tau - 1e-9) { dctx.clearRect(0, 0, W, H); lay = { sig, tau, pi: 0, mi: 0 }; } | |
| while (lay.pi < track.length) { | |
| const ph = track[lay.pi]; | |
| if (tau <= ph.off) break; | |
| while (lay.mi < ph.moves.length && ph.off + ph.moves[lay.mi].t1 <= tau) { strokeMove(dctx, ph, lay.pi, lay.mi, ph.moves[lay.mi], 1, W, H); lay.mi++; } | |
| if (lay.mi >= ph.moves.length) { lay.pi++; lay.mi = 0; } else break; | |
| } | |
| lay.tau = tau; | |
| sctx.clearRect(0, 0, W, H); wctx.clearRect(0, 0, W, H); | |
| sctx.drawImage(doneLayer, 0, 0); | |
| let now = null; | |
| if (lay.pi < track.length && tau > track[lay.pi].off && lay.mi < track[lay.pi].moves.length) { | |
| const ph = track[lay.pi], m = ph.moves[lay.mi], a0 = ph.off + m.t0; | |
| if (tau > a0) { now = { ph, m }; strokeMove(sctx, ph, lay.pi, lay.mi, m, frac({ t0: a0, t1: ph.off + m.t1, ramp: m.ramp }, tau), W, H); } | |
| } | |
| // wet sheen: the strokes of the last ~1.4 demo seconds shine, then dry matt | |
| const wet = k_() * 1.4; | |
| let pi = lay.pi, mi = lay.mi - 1; | |
| for (let guard = 0; guard < 400; guard++) { | |
| if (mi < 0) { pi--; if (pi < 0) break; mi = track[pi].moves.length - 1; } | |
| const ph = track[pi], m = ph.moves[mi], age = tau - (ph.off + m.t1); | |
| if (age > wet) break; | |
| if (m.k === "paint") { | |
| wctx.lineCap = "butt"; wctx.lineWidth = Math.max(3, ph.width * W) * (ph.tool === "spray" ? 0.9 : 1); | |
| wctx.strokeStyle = `rgba(255,255,255,${(ph.tool === "spray" ? 0.2 : 0.34) * (1 - Math.max(0, age) / wet)})`; | |
| strokePartialTo(wctx, m.a, m.b, 1, W, H); | |
| } | |
| mi--; | |
| } | |
| return now; | |
| } | |
| const PAR = [-0.6, -0.8], BEAM_Z = 420; // screen direction of "toward the camera", and how far the beam rides off the wall | |
| const hash = (a, b) => { let h = (a * 374761393 + b * 668265263) | 0; h = (h ^ (h >>> 13)) * 1274126177; return ((h ^ (h >>> 16)) >>> 0) / 4294967296; }; | |
| /** The machine in depth: the beam and carriage ride BEAM_Z off the wall, a telescoping axis pushes the tool to z mm | |
| * (contact tools touch at 0, spray stands off), and a shadow stays on the wall at the true position so lift and | |
| * standoff are readable. Parallax only; nothing here is measured. */ | |
| function drawRig(c, W, H, st) { | |
| const u = Math.max(W, H) / 560, kz = W * 0.00016, x = st.x * W, y = st.y * H, z = st.z || 0; | |
| const at = (zz) => [x + PAR[0] * kz * zz, y + PAR[1] * kz * zz]; | |
| const T = at(z), C = at(BEAM_Z), ang = Math.atan2(st.dy, st.dx), nx = -Math.sin(ang), ny = Math.cos(ang); | |
| c.save(); | |
| // shadow on the wall: crisp at contact, soft and faint when away | |
| const k = clamp(z / 320, 0, 1), sr = (4 + 20 * k) * u; | |
| c.save(); c.translate(x + 3 * u, y + 5 * u); c.rotate(ang + Math.PI / 2); | |
| const sg = c.createRadialGradient(0, 0, 0, 0, 0, st.len / 2 + sr); | |
| sg.addColorStop(0, `rgba(0,0,0,${0.34 - 0.2 * k})`); sg.addColorStop(1, "rgba(0,0,0,0)"); | |
| c.fillStyle = sg; c.scale(1, (sr + 5 * u) / (st.len / 2 + sr)); c.beginPath(); c.arc(0, 0, st.len / 2 + sr, 0, 7); c.fill(); c.restore(); | |
| // carriage shadow on the wall (it is far off the wall, so it lands far from the carriage) | |
| c.fillStyle = "rgba(0,0,0,.12)"; roundRect(c, x - 27 * u + 4 * u, y - 8 * u, 54 * u, 30 * u, 8 * u); c.fill(); | |
| // rails and beam at carriage height | |
| const rail = c.createLinearGradient(0, 0, 14 * u, 0); rail.addColorStop(0, "rgba(255,255,255,.5)"); rail.addColorStop(1, "rgba(120,125,135,.5)"); | |
| c.fillStyle = rail; c.fillRect(0, 0, 9 * u, H); c.fillRect(W - 9 * u, 0, 9 * u, H); | |
| const beamH = 12 * u, bg = c.createLinearGradient(0, C[1] - beamH / 2, 0, C[1] + beamH / 2); | |
| bg.addColorStop(0, "#f2f2f4"); bg.addColorStop(.5, "#c7c9cf"); bg.addColorStop(1, "#8e9098"); | |
| c.shadowColor = "rgba(0,0,0,.4)"; c.shadowBlur = 12 * u; c.shadowOffsetY = 5 * u; c.fillStyle = bg; c.fillRect(0, C[1] - beamH / 2, W, beamH); c.shadowColor = "transparent"; | |
| // telescoping axis: outer sleeve from the carriage, inner rod to the tool | |
| const mid = [(C[0] + T[0]) / 2, (C[1] + T[1]) / 2]; | |
| c.lineCap = "round"; c.strokeStyle = "#2a2a2e"; c.lineWidth = 9 * u; c.beginPath(); c.moveTo(C[0], C[1]); c.lineTo(mid[0] * 0.5 + C[0] * 0.5, mid[1] * 0.5 + C[1] * 0.5); c.stroke(); | |
| c.strokeStyle = "#b9bcc4"; c.lineWidth = 5 * u; c.beginPath(); c.moveTo(mid[0] * 0.5 + C[0] * 0.5, mid[1] * 0.5 + C[1] * 0.5); c.lineTo(T[0], T[1]); c.stroke(); | |
| // carriage | |
| const cw = 54 * u, ch = 38 * u; | |
| c.shadowColor = "rgba(0,0,0,.5)"; c.shadowBlur = 14 * u; c.shadowOffsetY = 6 * u; c.fillStyle = "#1d1d1f"; roundRect(c, C[0] - cw / 2, C[1] - ch / 2, cw, ch, 9 * u); c.fill(); c.shadowColor = "transparent"; | |
| c.fillStyle = st.kind === "tape" ? "#0a84ff" : colour; roundRect(c, C[0] - cw / 2 + 5 * u, C[1] - ch / 2 + 5 * u, cw - 10 * u, 6 * u, 3 * u); c.fill(); | |
| c.fillStyle = st.painting ? "#30d158" : st.moving ? "#0a84ff" : "#ffd60a"; c.beginPath(); c.arc(C[0] + cw / 2 - 11 * u, C[1] + ch / 2 - 10 * u, 3.2 * u, 0, 7); c.fill(); | |
| // spray: the cone from nozzle to the footprint, mist where it lands, droplets in flight | |
| if (st.kind === "spray" && st.painting) { | |
| const hw = st.len / 2, a = [x + nx * hw, y + ny * hw], b = [x - nx * hw, y - ny * hw], rgb = colour; | |
| const cg = c.createLinearGradient(T[0], T[1], x, y); cg.addColorStop(0, hexA(rgb, 0.55)); cg.addColorStop(1, hexA(rgb, 0.16)); | |
| c.fillStyle = cg; c.beginPath(); c.moveTo(T[0], T[1]); c.lineTo(a[0], a[1]); c.lineTo(b[0], b[1]); c.closePath(); c.fill(); | |
| const mg = c.createRadialGradient(x, y, 0, x, y, hw * 1.15); mg.addColorStop(0, hexA(rgb, 0.4)); mg.addColorStop(1, hexA(rgb, 0)); | |
| c.save(); c.translate(x, y); c.rotate(ang + Math.PI / 2); c.scale(0.5, 1); c.translate(-x, -y); c.fillStyle = mg; c.beginPath(); c.arc(x, y, hw * 1.15, 0, 7); c.fill(); c.restore(); | |
| for (let i = 0; i < 70; i++) { | |
| const r = hash(i, st.frame), l = hash(i + 91, st.frame) * 2 - 1, q = hash(i + 7, st.frame); | |
| const px = T[0] + ((x + nx * hw * l) - T[0]) * r, py = T[1] + ((y + ny * hw * l) - T[1]) * r; | |
| c.fillStyle = hexA(rgb, 0.7 - 0.35 * r); c.beginPath(); c.arc(px, py, (0.6 + q * 1.4) * u, 0, 7); c.fill(); | |
| } | |
| } | |
| if (st.reloading) { // the tray on the side rail: paint surface, the tool sinks into it | |
| const tw = 74 * u, th = 20 * u; | |
| c.fillStyle = "#3a3a3e"; roundRect(c, x - tw / 2, y - th / 2 + 4 * u, tw, th, 6 * u); c.fill(); | |
| c.fillStyle = colour; roundRect(c, x - tw / 2 + 4 * u, y - th / 2 + 6 * u, tw - 8 * u, th * 0.55, 4 * u); c.fill(); | |
| c.fillStyle = "rgba(255,255,255,.35)"; c.fillRect(x - tw / 2 + 8 * u, y - th / 2 + 8 * u, tw * 0.4, 2 * u); | |
| } | |
| // the tool, axis across the direction of travel, length = the stripe it lays | |
| c.translate(T[0], T[1]); c.rotate(ang + Math.PI / 2); | |
| if (st.kind === "tape") { | |
| c.fillStyle = "#0a84ff"; roundRect(c, -11 * u, -7 * u, 22 * u, 14 * u, 4 * u); c.fill(); | |
| } else if (st.kind === "spray") { | |
| c.fillStyle = "#2a2a2e"; roundRect(c, -9 * u, -7 * u, 18 * u, 14 * u, 4 * u); c.fill(); | |
| c.fillStyle = st.painting ? colour : "#6e6e73"; c.beginPath(); c.arc(0, 0, 3.4 * u, 0, 7); c.fill(); | |
| } else { | |
| const thick = (st.kind === "brush" ? 7 : 13) * u, len = st.len, squash = st.force ? 1 - 0.1 * st.force / 40 : 1; | |
| const g = c.createLinearGradient(0, -thick / 2, 0, thick / 2); // the cylinder, lit from above-left | |
| g.addColorStop(0, "rgba(255,255,255,.95)"); g.addColorStop(.3, colour); g.addColorStop(1, "rgba(0,0,0,.45)"); | |
| c.fillStyle = g; c.strokeStyle = "rgba(0,0,0,.4)"; c.lineWidth = u; | |
| roundRect(c, -len / 2, -thick * squash / 2, len, thick * squash, thick / 2); c.fill(); c.stroke(); | |
| if (st.kind === "roller") { // nap lines roll across the face as it turns | |
| c.strokeStyle = "rgba(0,0,0,.2)"; c.lineWidth = 0.9 * u; | |
| for (let i = 0; i < 4; i++) { const yy = (((st.odo / 157 + i / 4) % 1) - 0.5) * thick * 0.8; c.beginPath(); c.moveTo(-len / 2 + 3 * u, yy); c.lineTo(len / 2 - 3 * u, yy); c.stroke(); } | |
| } | |
| c.fillStyle = "#6e6e73"; c.fillRect(-1.5 * u, -thick / 2 - 10 * u, 3 * u, 10 * u); | |
| } | |
| c.restore(); | |
| if (z < 8 && st.footprint) { // exactly what the tool covers at this instant | |
| c.save(); c.translate(x, y); c.rotate(ang + Math.PI / 2); | |
| c.strokeStyle = "rgba(255,255,255,.85)"; c.lineWidth = Math.max(1, u); c.setLineDash([4 * u, 3 * u]); | |
| c.strokeRect(-st.len / 2, -1.5 * u, st.len, 3 * u); | |
| if (st.force) { c.setLineDash([]); c.strokeStyle = `rgba(48,209,88,${0.35 + 0.6 * st.force / 40})`; c.lineWidth = 2 * u; c.beginPath(); c.arc(0, 0, (5 + st.force / 4) * u, 0, 7); c.stroke(); } | |
| c.restore(); | |
| } else if (st.kind === "spray") { // standoff gauge: tool to wall | |
| c.strokeStyle = "rgba(255,255,255,.4)"; c.setLineDash([3 * u, 3 * u]); c.lineWidth = u; c.beginPath(); c.moveTo(T[0], T[1]); c.lineTo(x, y); c.stroke(); | |
| } | |
| c.restore(); | |
| } | |
| const hexA = (hex, a) => `rgba(${parseInt(hex.slice(1, 3), 16)},${parseInt(hex.slice(3, 5), 16)},${parseInt(hex.slice(5, 7), 16)},${a})`; | |
| /** Side view of the last and next few strokes: tool depth against time, wall along the bottom. */ | |
| const zc = $("zpanel"), zx = zc.getContext("2d"); | |
| function drawZ(tau, now) { | |
| const cw = zc.clientWidth || 480, ch = zc.clientHeight || 92, dpr = Math.min(2, window.devicePixelRatio || 1); | |
| if (zc.width !== Math.round(cw * dpr)) { zc.width = Math.round(cw * dpr); zc.height = Math.round(ch * dpr); } | |
| zx.setTransform(dpr, 0, 0, dpr, 0, 0); zx.clearRect(0, 0, cw, ch); | |
| const base = ch - 18, top = 10, win = k_() * 2.4, y = (z) => base - clamp(z / zMax, 0, 1) * (base - top); | |
| zx.fillStyle = "rgba(255,255,255,.07)"; zx.fillRect(0, base, cw, ch - base); // the wall | |
| zx.strokeStyle = "rgba(255,255,255,.35)"; zx.lineWidth = 1; zx.beginPath(); zx.moveTo(0, base + .5); zx.lineTo(cw, base + .5); zx.stroke(); | |
| zx.fillStyle = "rgba(152,152,157,.9)"; zx.font = "11px -apple-system, system-ui, sans-serif"; zx.textBaseline = "middle"; | |
| zx.fillText("wall", 6, base + 9); | |
| const zAtTau = (t) => { | |
| for (const ph of track) { | |
| if (t < ph.off || t > ph.end) continue; | |
| const tl = t - ph.off; let lo = 0, hi = ph.moves.length - 1; | |
| while (lo < hi) { const mid = (lo + hi) >> 1; if (ph.moves[mid].t1 < tl) lo = mid + 1; else hi = mid; } | |
| const m = ph.moves[lo]; return { z: zOf(m, tl), k: m.k }; | |
| } | |
| return null; | |
| }; | |
| let prev = null; | |
| for (let i = 0; i <= 120; i++) { | |
| const t = tau - win + (2 * win * i) / 120, s = zAtTau(t); if (!s) { prev = null; continue; } | |
| const px = (i / 120) * cw, py = y(s.z); | |
| if (prev) { | |
| zx.strokeStyle = s.k === "travel" || s.k === "lift" || s.k === "lower" ? "rgba(255,255,255,.7)" : (track[0] && track[0].tool === "spray" ? "#ffd60a" : "#0a84ff"); | |
| zx.lineWidth = 2.2; zx.beginPath(); zx.moveTo(prev[0], prev[1]); zx.lineTo(px, py); zx.stroke(); | |
| zx.fillStyle = "rgba(10,132,255,.1)"; zx.fillRect(Math.min(prev[0], px), Math.min(prev[1], py), Math.max(1, Math.abs(px - prev[0])), base - Math.min(prev[1], py)); | |
| } | |
| prev = [px, py]; | |
| } | |
| const cur = zAtTau(tau); | |
| zx.strokeStyle = "rgba(255,255,255,.35)"; zx.setLineDash([3, 3]); zx.beginPath(); zx.moveTo(cw / 2, 4); zx.lineTo(cw / 2, base); zx.stroke(); zx.setLineDash([]); | |
| if (cur) { zx.fillStyle = "#fff"; zx.beginPath(); zx.arc(cw / 2, y(cur.z), 4.5, 0, 7); zx.fill(); } | |
| zx.textAlign = "right"; zx.fillStyle = "#f5f5f7"; zx.font = "600 12px -apple-system, system-ui, sans-serif"; | |
| const spray = track[0] && track[0].tool === "spray"; | |
| zx.fillText(cur ? (spray ? `standoff ${Math.round(cur.z)} mm` : `${Math.round(cur.z)} mm off wall`) : "", cw - 8, 14); zx.textAlign = "left"; | |
| zx.fillStyle = "rgba(152,152,157,.9)"; zx.font = "11px -apple-system, system-ui, sans-serif"; zx.fillText("depth · side view", 8, 14); | |
| } | |
| const zOf = (m, tl) => { const u = clamp((tl - m.t0) / Math.max(1e-9, m.t1 - m.t0), 0, 1), e = m.k === "lift" || m.k === "lower" ? u * u * (3 - 2 * u) : u; return m.z0 + (m.z1 - m.z0) * e; }; | |
| /** Contact force: ramps in over the first tenth of a stroke and out over the last, as the controller would. */ | |
| const forceOf = (ph, m, tl) => { | |
| if (ph.tool === "spray" || !ph.force) return 0; | |
| if (m.k === "step") return ph.force; | |
| if (m.k !== "paint") return 0; | |
| const u = clamp((tl - m.t0) / Math.max(1e-9, m.t1 - m.t0), 0, 1); | |
| return ph.force * Math.min(1, u / 0.1, (1 - u) / 0.1); | |
| }; | |
| function roundRect(c, x, y, w, h, r) { | |
| r = Math.min(r, w / 2, h / 2); | |
| c.beginPath(); c.moveTo(x + r, y); c.arcTo(x + w, y, x + w, y + h, r); c.arcTo(x + w, y + h, x, y + h, r); | |
| c.arcTo(x, y + h, x, y, r); c.arcTo(x, y, x + w, y, r); c.closePath(); | |
| } | |
| function draw(t) { | |
| const W = canvas.width, H = canvas.height; | |
| ctx.clearRect(0, 0, W, H); ctx.drawImage(photo, 0, 0, W, H); | |
| const tTape = (t - SCAN_S) / TAPE_S, tFilm = (t - SCAN_S - TAPE_S) / FILM_S, tPaintRaw = (t - SCAN_S - TAPE_S - FILM_S) / PAINT_S; | |
| const tPaint = clamp(tPaintRaw, 0, 1), tau = tPaint * machineTotal; | |
| let robot = null, caption = null; | |
| // 1. regions found: every class tinted and labelled; fades once the work starts (chips keep any on) | |
| if (regionsImg) drawRegions(W, H, ease(t / SCAN_S) * (1 - ease(tTape * 4))); | |
| if (t < SCAN_S) caption = ["Regions found", `${plan.bill.wall_m2} m² of wall · ${plan.items.length} things to protect`]; | |
| // 2. tape lays itself, run after run | |
| if (tTape > 0) { | |
| const widthPx = Math.max(3, plan.tape_width_mm / plan.scale.mm_per_px * (W / plan.shape[1])); | |
| const n = plan.tape.length, f = Math.min(1, ease(tTape)); | |
| let metres = 0; | |
| if (tTape < 1 && n) { | |
| const idx = Math.min(n - 1, Math.floor(f * n)), loc = f * n - idx; | |
| robot = { ...tipOf(plan.tape[idx].points, loc), kind: "tape", len: 22, z: 0, frame: Math.floor(tNow * 30), odo: 0, moving: true, painting: true, label: `Taping · run ${idx + 1} of ${n}` }; | |
| } | |
| ctx.lineCap = "butt"; ctx.lineJoin = "miter"; | |
| plan.tape.forEach((r, i) => { | |
| const a = i / n, b = (i + 1) / n, local = clamp((f - a) / (b - a), 0, 1); | |
| if (local <= 0) return; | |
| ctx.save(); ctx.shadowColor = "rgba(0,0,0,.35)"; ctx.shadowBlur = 3; ctx.shadowOffsetY = 1.5; | |
| ctx.strokeStyle = "rgba(60,150,255,0.94)"; ctx.lineWidth = widthPx; strokePartial(ctx, r.points, local, W, H); ctx.restore(); | |
| ctx.strokeStyle = "rgba(255,255,255,0.38)"; ctx.lineWidth = Math.max(1, widthPx * 0.18); strokePartial(ctx, r.points, local, W, H); | |
| metres += r.length_mm / 1000 * local; | |
| }); | |
| $("s-tape").textContent = (tTape >= 1 ? plan.bill.tape_m : metres).toFixed(1); | |
| if (tTape < 1) caption = ["Taping the cut lines", `${plan.bill.tape_width_mm || plan.tape_width_mm} mm tape on the protected side of every edge`]; | |
| } | |
| // 3. film over openings, covers on the small things | |
| if (tFilm > 0) { | |
| const a = ease(tFilm); | |
| plan.items.forEach((it) => { | |
| const [x0, y0, x1, y1] = it.bbox, w = (x1 - x0) * W, h = (y1 - y0) * H; | |
| if (it.film_m2) { | |
| const g = ctx.createLinearGradient(x0 * W, y0 * H, x1 * W, y1 * H); | |
| g.addColorStop(0, `rgba(160,215,240,${0.42 * a})`); g.addColorStop(.45, `rgba(255,255,255,${0.2 * a})`); g.addColorStop(1, `rgba(130,195,225,${0.36 * a})`); | |
| ctx.fillStyle = g; ctx.fillRect(x0 * W, y0 * H, w, h); | |
| } else if (it.treatment === "cover") { ctx.strokeStyle = `rgba(255,170,0,${a})`; ctx.lineWidth = 3; ctx.strokeRect(x0 * W - 4, y0 * H - 4, w + 8, h + 8); } | |
| }); | |
| $("s-film").textContent = (plan.bill.film_m2 * a).toFixed(1); | |
| if (tPaintRaw < 0) caption = ["Masked", `${plan.bill.film_m2} m² of film · ${plan.bill.covers} cover(s) · ${plan.bill.rolls} roll of tape`]; | |
| } | |
| // 4. cut-in, then roller passes, from the timeline | |
| let readout = null; | |
| if (tPaintRaw > 0 && track.length) { | |
| const now = layPaint(tau, W, H); | |
| // paint = the wall's own lit colour, revealed through the strokes that are down | |
| cctx.clearRect(0, 0, W, H); cctx.drawImage(tinted.canvas, 0, 0, W, H); | |
| cctx.globalCompositeOperation = "destination-in"; cctx.drawImage(strokeLayer, 0, 0, W, H); cctx.globalCompositeOperation = "source-over"; | |
| ctx.drawImage(coat, 0, 0, W, H); | |
| wctx.globalCompositeOperation = "destination-in"; wctx.drawImage(paintLayer, 0, 0, W, H); wctx.globalCompositeOperation = "source-over"; | |
| ctx.drawImage(wetLayer, 0, 0, W, H); | |
| if (showPath) ctx.drawImage(pathLayer, 0, 0, W, H); | |
| const cov = coverage(); | |
| $("s-paint").textContent = Math.round(100 * cov) + "%"; | |
| if (tPaintRaw < 1 && now) { | |
| const { ph, m } = now, tm = tau - ph.off, f = frac(m, tm); | |
| const p = lerpPt(m.a, m.b, f), dx = m.b[0] - m.a[0], dy = m.b[1] - m.a[1]; | |
| if (Math.hypot(dx, dy) > 1e-6) lastDir = { dx, dy }; | |
| const z = zOf(m, tm), painting = m.k === "paint" || m.k === "step"; | |
| const i = ph.moves.slice(0, ph.moves.indexOf(m) + 1).filter((q) => q.k === "paint").length; | |
| const eps = 0.04, f2 = frac(m, tm + eps), v = Math.hypot((f2 - f) * dx * plan.shape[1], (f2 - f) * dy * plan.shape[0]) * plan.scale.mm_per_px / eps; | |
| const idx = ph.moves.indexOf(m), odo = ph.cum[idx] + (painting ? f * ph.mmOf(m) : 0); | |
| const reloading = m.k === "reload", sinceNow = ph.reloadMm ? (reloading ? 0 : ph.sinceAt[idx] + (painting ? f * ph.mmOf(m) : 0)) : 0; | |
| const load = ph.reloadMm ? (reloading ? ease((tm - m.t0) / Math.max(1e-9, m.t1 - m.t0)) : clamp(1 - sinceNow / ph.reloadMm, 0, 1)) : 1; | |
| robot = { reloading, load, x: p[0], y: p[1], dx: lastDir.dx * W, dy: lastDir.dy * H, kind: ph.tool, len: Math.max(ph.tool === "brush" ? 8 : 16, ph.width * W), z, moving: v > 5, painting, | |
| footprint: painting && ph.tool !== "spray", force: forceOf(ph, m, tm), odo, frame: Math.floor(tNow * 30), label: "" }; | |
| readout = { tool: ph.tool, i: Math.max(1, i), n: ph.paintMoves, kind: m.k, v, tau, z, force: robot.force, load, reloading }; | |
| const what = ph.tool === "brush" ? "Cutting in" : ph.tool === "spray" ? "Spraying" : "Rolling"; | |
| caption = reloading ? ["Reloading at the tray", `the ${ph.tool} is dipped on the side rail, then returns to where it stopped`] : m.k === "lift" || m.k === "travel" || m.k === "lower" | |
| ? ph.tool === "spray" ? ["Valve closed, repositioning", `holding ${Math.round(ph.standoff)} mm standoff, no paint in the air`] : ["Moving to the next section", `retract to ${Math.round(Math.max(m.z0, m.z1))} mm, fast hop, plunge back to the wall`] | |
| : [`${what} the wall`, `${ph.tool === "spray" ? `${Math.round(ph.standoff)} mm standoff · fan ${Math.round(ph.width * plan.shape[1] * plan.scale.mm_per_px)} mm · 50% overlap, trigger off on every hop` : ph.tool === "brush" ? "brush first around windows and edges, so the roller blends into wet paint" : `${plan.bill.coats} coats · ${plan.motion.orientation} passes, top-down, ordered to cut travel`}`]; | |
| robot.label = `${what} · ${ph.tool === "brush" ? "stroke" : "pass"} ${Math.max(1, i)} of ${ph.paintMoves}`; | |
| } | |
| if (tPaintRaw >= 1) caption = ["Job complete", `${Math.round(100 * cov)}% of the paintable wall reached · drag Before / after to compare`]; | |
| if (tPaintRaw >= 0.98 && tPaintRaw < 1) caption = ["Tape off, touch-dry", "removed at 45° before the coat cures so the edge stays sharp"]; | |
| } | |
| if (robot) drawRig(ctx, W, H, robot); | |
| else if (tPaintRaw >= 1 && t < TOTAL_S - 0.001) drawRig(ctx, W, H, { x: 0.5, y: 0.04, dx: 1, dy: 0, kind: "roller", len: Math.max(16, (plan.roller_width || 0.05) * W), z: 120, moving: false, painting: false, odo: 0, frame: 0 }); | |
| if (tTape > 0 || tPaintRaw > 0) drawLabel(ctx, W, robot ? robot.label : tPaintRaw >= 1 ? "Parked · job complete" : "", readout); | |
| if (compare && t >= TOTAL_S - 1e-6) drawCompare(W, H); | |
| if (caption) cap(caption[0], caption[1]); | |
| hud(t, tPaintRaw, tau, readout); | |
| drawZ(tau, readout); | |
| phaseFor(t); | |
| } | |
| function drawLabel(c, W, text, readout) { | |
| if (!text) return; | |
| c.save(); | |
| const fs = Math.max(11, Math.round(W / 62)); c.font = `600 ${fs}px -apple-system, "SF Pro Text", system-ui, sans-serif`; | |
| const tw = c.measureText(text).width, pw = tw + 30, ph = fs + 14; | |
| c.fillStyle = "rgba(20,20,24,.74)"; roundRect(c, 14, 14, pw, ph, ph / 2); c.fill(); | |
| c.fillStyle = readout && readout.kind === "paint" ? "#30d158" : "#ffd60a"; c.beginPath(); c.arc(27, 14 + ph / 2, 4, 0, 7); c.fill(); | |
| c.fillStyle = "#fff"; c.textBaseline = "middle"; c.fillText(text, 38, 14 + ph / 2 + 1); | |
| c.restore(); | |
| } | |
| function drawCompare(W, H) { | |
| const x = cmpX * W; | |
| ctx.save(); ctx.beginPath(); ctx.rect(0, 0, x, H); ctx.clip(); ctx.drawImage(photo, 0, 0, W, H); ctx.restore(); | |
| ctx.fillStyle = "#fff"; ctx.fillRect(x - 1, 0, 2, H); | |
| ctx.fillStyle = "rgba(20,20,24,.8)"; ctx.beginPath(); ctx.arc(x, H / 2, 17, 0, 7); ctx.fill(); | |
| ctx.strokeStyle = "#fff"; ctx.lineWidth = 2.2; ctx.beginPath(); ctx.moveTo(x - 4, H / 2 - 6); ctx.lineTo(x - 9, H / 2); ctx.lineTo(x - 4, H / 2 + 6); | |
| ctx.moveTo(x + 4, H / 2 - 6); ctx.lineTo(x + 9, H / 2); ctx.lineTo(x + 4, H / 2 + 6); ctx.stroke(); | |
| ctx.font = `600 ${Math.max(11, Math.round(W / 70))}px system-ui, sans-serif`; ctx.fillStyle = "rgba(0,0,0,.6)"; | |
| ctx.fillText("Before", 12, H - 14); ctx.textAlign = "right"; ctx.fillText("After", W - 12, H - 14); ctx.textAlign = "left"; | |
| } | |
| // Regions are drawn as per-class layers so a chip can switch one off; tinting is done on a scratch canvas. | |
| function drawRegions(W, H, a) { | |
| const pinned = Object.values(shown).some(Boolean) && pinnedOn; | |
| const alpha = Math.max(a, pinned ? 0.55 : 0); | |
| if (alpha < 0.01) return; | |
| regionScratch.width = W; regionScratch.height = H; | |
| rctx.clearRect(0, 0, W, H); rctx.drawImage(regionsImg, 0, 0, W, H); | |
| const data = rctx.getImageData(0, 0, W, H), px = data.data; | |
| const off = plan.regions.filter((r) => !shown[r.class]).map((r) => r.colour); | |
| if (off.length) { | |
| const hex = off.map((c) => [parseInt(c.slice(1, 3), 16), parseInt(c.slice(3, 5), 16), parseInt(c.slice(5, 7), 16)]); | |
| for (let i = 0; i < px.length; i += 4) if (px[i + 3] && hex.some((h) => h[0] === px[i] && h[1] === px[i + 1] && h[2] === px[i + 2])) px[i + 3] = 0; | |
| rctx.putImageData(data, 0, 0); | |
| } | |
| ctx.globalAlpha = alpha; ctx.drawImage(regionScratch, 0, 0); ctx.globalAlpha = 1; | |
| ctx.font = `600 ${Math.max(11, Math.round(W / 70))}px system-ui, sans-serif`; ctx.textBaseline = "top"; | |
| plan.items.forEach((it) => { | |
| if (!shown[it.class]) return; | |
| const style = plan.regions.find((r) => r.class === it.class); if (!style) return; | |
| const x = it.bbox[0] * W, y = it.bbox[1] * H, text = style.label; | |
| const w = ctx.measureText(text).width + 10, h = Math.round(W / 70) + 8; | |
| ctx.fillStyle = style.colour; ctx.globalAlpha = Math.min(1, alpha * 1.6); | |
| ctx.fillRect(x, Math.max(0, y - h), w, h); | |
| ctx.fillStyle = "#06120f"; ctx.fillText(text, x + 5, Math.max(0, y - h) + 4); ctx.globalAlpha = 1; | |
| }); | |
| ctx.textBaseline = "alphabetic"; | |
| } | |
| function legend() { | |
| const box = $("legend"); box.innerHTML = ""; | |
| plan.regions.forEach((r) => { | |
| const b = document.createElement("button"); | |
| b.setAttribute("aria-pressed", String(shown[r.class])); | |
| b.innerHTML = `<i></i>${r.label}${r.count > 1 ? " ×" + r.count : ""}`; b.firstChild.style.background = r.colour; | |
| b.onclick = () => { shown[r.class] = !shown[r.class]; pinnedOn = true; legend(); redraw(); }; | |
| box.appendChild(b); | |
| }); | |
| } | |
| // ---------------------------------------------------------------------------------------------------- readouts | |
| let lastCap = ""; | |
| function cap(a, b) { const s = `${a}|${b}`; if (s === lastCap) return; lastCap = s; $("caption").innerHTML = `<b>${a}</b><br><small>${b}</small>`; } | |
| function hud(t, tPaintRaw, tau, readout) { | |
| $("scrub").value = String(Math.round(t / TOTAL_S * 1000)); | |
| $("clock").textContent = tPaintRaw > 0 ? `${fmtClock(Math.min(tau, machineTotal))} / ${fmtClock(machineTotal)} machine` : ""; | |
| const r = $("readout"); | |
| if (readout) { | |
| const tool = readout.tool === "brush" ? "Brush" : readout.tool === "spray" ? "Spray" : "Roller", mmps = Math.round(readout.v); | |
| const on = readout.kind === "paint" || readout.kind === "step", fz = readout.force ? `<span><b>${readout.force.toFixed(0)}</b> N</span>` : ""; | |
| r.innerHTML = `<span><b>${tool}</b> ${readout.reloading ? "dipping" : readout.tool === "spray" ? (on ? "trigger on" : "valve closed") : on ? "on wall" : "lifted"}</span><span><b>${mmps}</b> mm/s</span><span><b>${Math.round(readout.z)}</b> mm z</span>${fz}${readout.tool !== "spray" ? `<span>paint <b>${Math.round(100 * readout.load)}</b>%</span>` : ""}<span><b>${readout.i}</b>/${readout.n} ${readout.tool === "brush" ? "strokes" : "passes"}</span>`; | |
| } else r.innerHTML = ""; | |
| } | |
| function phaseFor(t) { | |
| const n = t < SCAN_S ? ["scan", "Scanning"] : t < SCAN_S + TAPE_S ? ["tape", "Masking"] : t < SCAN_S + TAPE_S + FILM_S ? ["film", "Masking"] : t < TOTAL_S - DONE_S ? ["paint", "Painting"] : ["done", "Planned"]; | |
| if (app.dataset.phase !== n[0]) phase(n[0], n[1]); | |
| } | |
| function finish() { | |
| phase("done", "Planned"); | |
| const b = plan.bill, mo = (toolKind === "spray" ? plan.motion_spray : plan.motion) || {}; | |
| const cov = Math.round(100 * coverage()); | |
| $("result").hidden = false; $("compare").hidden = false; | |
| $("result").innerHTML = `<h2>The job, planned</h2><dl> | |
| <dt>Wall to paint</dt><dd>${b.paint_m2} m²</dd> | |
| <dt>Reached by roller + brush</dt><dd>${cov}%</dd> | |
| <dt>Tape</dt><dd>${b.tape_m} m · ${b.rolls} roll</dd> | |
| <dt>Film / covers</dt><dd>${b.film_m2} m² / ${b.covers}</dd> | |
| <dt>Paint (${b.coats} coats)</dt><dd>${b.paint_litres} L</dd> | |
| <dt>Masking time</dt><dd>${b.tape_minutes} min</dd> | |
| <dt>Painting time (${toolKind})</dt><dd>${toolKind === "spray" && b.spray_minutes != null ? b.spray_minutes : b.paint_minutes} min</dd> | |
| <dt>Path</dt><dd>${mo.orientation || "horizontal"} · ${Math.round(100 * (mo.travel_share || 0))}% travel · ${mo.lifts || 0} lift${mo.lifts === 1 ? "" : "s"}</dd></dl> | |
| <p>Scale: ${plan.scale.source || "measured"} (${plan.scale.mm_per_px} mm per pixel). Times include every reversal, step-over and lifted hop at the tool's speed and acceleration. This is the plan and a simulation of it, not a machine moving.</p>`; | |
| if (recorder && recorder.state === "recording") recorder.stop(); | |
| } | |
| // ---------------------------------------------------------------------------------------------------- controls | |
| function swatches() { | |
| $("swatches").innerHTML = ""; | |
| COLOURS.forEach((c) => { | |
| const b = document.createElement("button"); | |
| b.style.background = c; b.setAttribute("aria-label", "Paint colour " + c); b.setAttribute("aria-pressed", String(c === colour)); | |
| b.onclick = () => { colour = c; swatches(); if (plan) { tint(tinted.data); if (playing) return; draw(tNow); } }; | |
| $("swatches").appendChild(b); | |
| }); | |
| } | |
| function record() { | |
| if (!plan || !window.MediaRecorder) { show("This browser cannot record the canvas; use your phone's screen recording instead."); return; } | |
| const type = ["video/mp4", "video/webm;codecs=vp9", "video/webm"].find((m) => MediaRecorder.isTypeSupported(m)); | |
| chunks = []; speed = 1; $("speed").value = "1"; | |
| recorder = new MediaRecorder(canvas.captureStream(30), type ? { mimeType: type, videoBitsPerSecond: 6e6 } : undefined); | |
| recorder.ondataavailable = (e) => e.data.size && chunks.push(e.data); | |
| recorder.onstop = () => { | |
| const blob = new Blob(chunks, { type: recorder.mimeType }); | |
| const a = document.createElement("a"); a.href = URL.createObjectURL(blob); | |
| a.download = "painting-vision-demo." + (recorder.mimeType.includes("mp4") ? "mp4" : "webm"); a.click(); | |
| $("record").disabled = false; $("record").textContent = "Record clip"; | |
| }; | |
| $("record").disabled = true; $("record").textContent = "Recording…"; | |
| recorder.start(); play(true); | |
| } | |
| function seek(v) { tNow = clamp(v, 0, TOTAL_S); lastTs = 0; if (tNow < TOTAL_S) $("result").hidden = true; if (!playing) draw(tNow); } | |
| const cmpMove = (e) => { const r = canvas.getBoundingClientRect(); cmpX = clamp((e.clientX - r.left) / r.width, 0.02, 0.98); redraw(); }; | |
| ["file", "file2", "file3"].forEach((id) => $(id).addEventListener("change", (e) => start(e.target.files[0]))); | |
| $("play").onclick = () => { if (!plan) return; if (playing) pause(); else play(tNow >= TOTAL_S - 1e-6); }; | |
| $("replay").onclick = () => plan && play(true); | |
| $("record").onclick = record; | |
| $("scrub").addEventListener("input", (e) => { if (!plan) return; pause(); $("play").textContent = "Play"; seek(Number(e.target.value) / 1000 * TOTAL_S); }); | |
| $("speed").addEventListener("change", (e) => { speed = Number(e.target.value); }); | |
| $("path").onclick = () => { showPath = !showPath; $("path").setAttribute("aria-pressed", String(showPath)); redraw(); }; | |
| $("compare").onclick = () => { | |
| compare = !compare; $("compare").setAttribute("aria-pressed", String(compare)); canvas.classList.toggle("cmp", compare); | |
| if (compare) { pause(); tNow = TOTAL_S; $("play").textContent = "Replay"; } | |
| draw(tNow); | |
| }; | |
| canvas.addEventListener("pointerdown", (e) => { if (!compare) return; canvas.setPointerCapture(e.pointerId); cmpMove(e); canvas.onpointermove = cmpMove; }); | |
| canvas.addEventListener("pointerup", () => { canvas.onpointermove = null; }); | |
| document.addEventListener("keydown", (e) => { | |
| if (!plan || /INPUT|SELECT|TEXTAREA/.test(document.activeElement.tagName) && document.activeElement.type !== "range") return; | |
| if (e.code === "Space") { e.preventDefault(); $("play").click(); } | |
| else if (e.code === "ArrowRight") { pause(); seek(tNow + 1); } | |
| else if (e.code === "ArrowLeft") { pause(); seek(tNow - 1); } | |
| }); | |
| function setTool(k) { | |
| if (k === "spray" && !(plan && plan.motion_spray)) return; | |
| toolKind = k; $("t-roller").setAttribute("aria-pressed", String(k === "roller")); $("t-spray").setAttribute("aria-pressed", String(k === "spray")); | |
| if (!plan) return; buildTrack(); buildPathLayer(); play(true); | |
| } | |
| $("t-roller").onclick = () => setTool("roller"); $("t-spray").onclick = () => setTool("spray"); | |
| $("tilt").onclick = () => { const on = $("stage").classList.toggle("tilt"); $("tilt").setAttribute("aria-pressed", String(on)); }; | |
| swatches(); | |