Rover
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, user-scalable=no">
<meta name="theme-color" content="#150a33">
<title>Rover</title>
<style>
:root {
--bg: #150a33; --ink: #f6eaff; --dim: #a58fd6;
--pink: #ff2e97; --cyan: #22e6ff; --yel: #ffe14d; --red: #ff3b4e; --vio: #8a5cff;
--arc: "Arial Rounded MT Bold", "Varela Round", "Nunito", "SF Pro Rounded", ui-rounded, "Segoe UI Black", "Arial Black", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 900 12px/1.2 var(--arc); overflow: hidden;
-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none;
overscroll-behavior: none; }
#sky { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 0; }
main { position: relative; z-index: 1; height: 100%; max-width: 560px; margin: 0 auto; display: flex; flex-direction: column; gap: 8px;
padding: calc(10px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left)); }
/* ---------- header ---------- */
header { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex: none; height: 36px; }
.logo { font-size: 28px; font-style: italic; letter-spacing: .1em; line-height: 1; padding-right: 6px;
background: linear-gradient(180deg, #fffbd0 8%, #ffe14d 42%, #ff8a3d 58%, #ff2e97 92%); -webkit-background-clip: text; background-clip: text; color: transparent;
filter: drop-shadow(0 2px 0 #7a1195) drop-shadow(0 0 9px rgba(255, 46, 151, .75)); }
.who { display: flex; flex-direction: column; min-width: 0; }
.who small { font-size: 9px; letter-spacing: .28em; color: var(--cyan); text-shadow: 0 0 8px rgba(34, 230, 255, .7); margin-top: 3px;
white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-transform: uppercase; }
.status { display: flex; align-items: center; gap: 9px; flex: none; }
.pill { font-size: 10px; letter-spacing: .16em; padding: 5px 9px 4px; border-radius: 99px; border: 1.5px solid var(--cyan); color: var(--cyan);
background: rgba(10, 4, 34, .6); box-shadow: 0 0 10px rgba(34, 230, 255, .45), inset 0 0 8px rgba(34, 230, 255, .18); white-space: nowrap; }
.pill.wait { border-color: var(--yel); color: var(--yel); box-shadow: 0 0 10px rgba(255, 225, 77, .4); }
.pill.lost { border-color: var(--red); color: #fff; background: rgba(255, 59, 78, .35); box-shadow: 0 0 14px rgba(255, 59, 78, .8); animation: blink .7s steps(2) infinite; }
.sig { display: flex; align-items: flex-end; gap: 3px; height: 22px; }
.sig i { width: 5px; border-radius: 2px; background: rgba(165, 143, 214, .28); }
.sig i:nth-child(1) { height: 6px } .sig i:nth-child(2) { height: 10px } .sig i:nth-child(3) { height: 14px } .sig i:nth-child(4) { height: 18px } .sig i:nth-child(5) { height: 22px }
.sig i.on { background: var(--cyan); box-shadow: 0 0 7px var(--cyan); }
.sig.low i.on { background: var(--yel); box-shadow: 0 0 7px var(--yel); }
.sigbox { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.sigbox small { font-size: 8px; letter-spacing: .08em; color: var(--dim); font-variant-numeric: tabular-nums; }
/* ---------- HUD ---------- */
.glass { background: linear-gradient(180deg, rgba(34, 12, 82, .74), rgba(14, 5, 44, .8)); border: 1.5px solid rgba(255, 46, 151, .6); border-radius: 14px;
box-shadow: 0 0 16px rgba(255, 46, 151, .22), inset 0 0 0 1px rgba(34, 230, 255, .1), inset 0 1px 0 rgba(255, 255, 255, .1); }
.lbl { font-size: 9px; letter-spacing: .24em; color: var(--cyan); text-shadow: 0 0 6px rgba(34, 230, 255, .6); }
.num { font-variant-numeric: tabular-nums; }
.batt { flex: none; display: flex; align-items: center; gap: 9px; padding: 0 11px; height: 32px; }
.cells { flex: 1; display: flex; gap: 3px; height: 14px; transform: skewX(-14deg); }
.cells i { flex: 1; border-radius: 2px; background: rgba(165, 143, 214, .2); }
.cells i.on { background: linear-gradient(180deg, #b6fbff, var(--cyan)); box-shadow: 0 0 8px rgba(34, 230, 255, .85); }
.batt.warn .cells i.on { background: linear-gradient(180deg, #fff6b8, var(--yel)); box-shadow: 0 0 8px rgba(255, 225, 77, .85); }
.batt.crit .cells i.on { background: linear-gradient(180deg, #ffb0b8, var(--red)); box-shadow: 0 0 9px rgba(255, 59, 78, .95); animation: blink .8s steps(2) infinite; }
.batt b { font-size: 14px; min-width: 40px; text-align: right; color: var(--ink); }
.batt small { font-size: 10px; color: var(--dim); min-width: 34px; text-align: right; }
.batt.warn b { color: var(--yel); } .batt.crit b { color: var(--red); }
.tiles { flex: none; display: grid; grid-template-columns: 1.12fr 1fr 1.12fr; gap: 8px; height: 112px; }
.tile { position: relative; padding: 8px 8px 6px; display: flex; flex-direction: column; align-items: center; min-width: 0; overflow: hidden; }
.tile .lbl { align-self: flex-start; }
.tile svg { display: block; }
.seg { flex: 1; display: flex; align-items: center; gap: 4px; transform: skewX(-8deg); }
.seg svg { height: 50px; width: auto; overflow: visible; }
.seg polygon { fill: rgba(255, 225, 77, .09); }
.seg polygon.on { fill: var(--yel); filter: drop-shadow(0 0 3px rgba(255, 190, 60, .95)); }
.unit { font-size: 9px; letter-spacing: .2em; color: var(--dim); }
.unit b { color: var(--ink); font-size: 11px; letter-spacing: .06em; }
#rov { flex: 1; width: 100%; min-height: 0; }
#radar { flex: 1; width: 100%; min-height: 0; overflow: visible; }
.sweep { transform-box: view-box; transform-origin: 50px 60px; animation: sweep 2.6s ease-in-out infinite alternate; }
.ping { transform-box: view-box; transform-origin: 50px 60px; opacity: 0; }
.near .ping { animation: ping .9s ease-out infinite; }
.near #blip { stroke: var(--red); filter: drop-shadow(0 0 4px var(--red)); }
.near #dist { color: var(--red); text-shadow: 0 0 8px rgba(255, 59, 78, .9); }
@keyframes sweep { from { transform: rotate(-62deg) } to { transform: rotate(62deg) } }
@keyframes ping { from { transform: scale(.1); opacity: .95 } to { transform: scale(1); opacity: 0 } }
@keyframes blink { 50% { opacity: .35 } }
@keyframes flash { 50% { opacity: .6 } }
.compass { flex: none; display: flex; align-items: center; gap: 8px; height: 34px; padding: 0 11px; }
.ribbon { position: relative; flex: 1; height: 100%; overflow: hidden;
-webkit-mask-image: linear-gradient(90deg, transparent, #000 18%, #000 82%, transparent); mask-image: linear-gradient(90deg, transparent, #000 18%, #000 82%, transparent); }
#strip { position: absolute; left: 50%; top: 0; height: 100%; will-change: transform; }
#strip i { position: absolute; bottom: 4px; width: 2px; margin-left: -1px; height: 6px; border-radius: 1px; background: rgba(165, 143, 214, .6); }
#strip i.m { height: 9px; background: var(--pink); box-shadow: 0 0 5px var(--pink); }
#strip b { position: absolute; top: 4px; width: 30px; margin-left: -15px; text-align: center; font-size: 10px; letter-spacing: .04em; color: var(--dim); }
#strip b.c { color: var(--ink); font-size: 12px; top: 3px; }
#strip b.n { color: var(--yel); text-shadow: 0 0 7px rgba(255, 225, 77, .8); }
.needle { position: absolute; left: 50%; top: 0; bottom: 0; width: 2px; margin-left: -1px; background: var(--yel); box-shadow: 0 0 8px var(--yel); opacity: .9; }
.needle::before { content: ""; position: absolute; left: -4px; top: 0; border: 5px solid transparent; border-top: 6px solid var(--yel); }
#hdg { min-width: 40px; text-align: right; font-size: 14px; }
/* ---------- control deck ---------- */
.deck { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 8px; }
#pad { position: relative; flex: 1; min-height: 0; touch-action: none; cursor: grab; --d: 280px; }
#trail { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.base { position: absolute; left: 50%; top: 50%; width: var(--d); height: var(--d); margin: calc(var(--d) / -2) 0 0 calc(var(--d) / -2); border-radius: 50%;
background: radial-gradient(circle, rgba(34, 230, 255, .1) 0, rgba(20, 6, 60, .5) 38%, rgba(12, 4, 40, .62) 100%);
border: 2.5px solid var(--pink); box-shadow: 0 0 22px rgba(255, 46, 151, .65), inset 0 0 0 6px rgba(10, 4, 34, .55), inset 0 0 0 7.5px rgba(34, 230, 255, .9), inset 0 0 30px 8px rgba(255, 46, 151, .3);
pointer-events: none; transition: border-color .25s, box-shadow .25s, opacity .25s; }
.base svg { position: absolute; inset: 0; width: 100%; height: 100%; }
#knob { position: absolute; left: 50%; top: 50%; width: calc(var(--d) * .34); height: calc(var(--d) * .34); margin: calc(var(--d) * -.17) 0 0 calc(var(--d) * -.17); border-radius: 50%; pointer-events: none;
background: radial-gradient(circle at 34% 28%, #fff 0, #ffc2e2 9%, #ff5cae 30%, var(--pink) 55%, #9c0f63 100%);
box-shadow: 0 0 0 3px rgba(255, 255, 255, .22), 0 0 26px rgba(255, 46, 151, .95), 0 0 60px rgba(255, 46, 151, .45), 0 10px 18px rgba(0, 0, 0, .55), inset 0 -7px 14px rgba(70, 0, 50, .6);
will-change: transform; transition: filter .25s, box-shadow .25s; }
#knob::after { content: ""; position: absolute; inset: 24%; border-radius: 50%; border: 2px solid rgba(255, 255, 255, .28); }
.held #knob { box-shadow: 0 0 0 3px rgba(255, 255, 255, .5), 0 0 34px rgba(255, 46, 151, 1), 0 0 80px rgba(255, 46, 151, .6), 0 6px 12px rgba(0, 0, 0, .55), inset 0 -7px 14px rgba(70, 0, 50, .6); }
.safe #knob { filter: saturate(.28) brightness(.72); box-shadow: 0 0 0 3px rgba(255, 255, 255, .16), 0 10px 18px rgba(0, 0, 0, .55), inset 0 -7px 14px rgba(40, 0, 50, .6); }
.safe .base { border-color: rgba(165, 143, 214, .7); box-shadow: 0 0 12px rgba(138, 92, 255, .35), inset 0 0 20px rgba(138, 92, 255, .2), inset 0 0 0 7px rgba(10, 4, 34, .5), inset 0 0 0 8.5px rgba(165, 143, 214, .35); }
.down .base, .down #knob { opacity: .35; }
.down #arm { filter: saturate(.35) brightness(.7); }
.down .batt, .down .tiles, .down .compass, .down .mix, .down .sigbox { opacity: .5; }
.tagline { position: absolute; left: 0; right: 0; top: 2px; text-align: center; font-size: 10px; letter-spacing: .22em; color: var(--yel);
text-shadow: 0 0 8px rgba(255, 225, 77, .8); pointer-events: none; opacity: 0; transition: opacity .3s; }
.tagline.show { opacity: 1; }
.tagline span { display: inline-block; padding: 4px 10px 3px; border-radius: 99px; border: 1.5px solid var(--yel); background: rgba(10, 4, 34, .7); }
#demoTag.show span { animation: blink 1.2s steps(2) infinite; }
#hint { top: auto; bottom: 4px; }
#over { position: absolute; inset: 0; display: none; flex-direction: column; align-items: center; justify-content: center; gap: 8px; pointer-events: none; text-align: center; }
.down #over { display: flex; }
#over b { font-size: 34px; font-style: italic; letter-spacing: .08em; color: #fff; -webkit-text-stroke: 1.5px var(--red);
text-shadow: 0 3px 0 #7a0a1c, 0 0 18px var(--red), 0 0 40px rgba(255, 59, 78, .7); animation: flash .9s steps(2) infinite; }
.wait #over b { -webkit-text-stroke-color: var(--yel); text-shadow: 0 3px 0 #7a5a00, 0 0 18px rgba(255, 225, 77, .8); font-size: 26px; }
#over small { font-size: 10px; letter-spacing: .24em; color: var(--ink); background: rgba(10, 4, 34, .7); padding: 4px 10px; border-radius: 99px; }
.row { flex: none; display: flex; align-items: stretch; gap: 10px; height: 62px; }
.btn { position: relative; flex: none; border: 0; padding: 0 0 6px; margin: 0; font: inherit; color: #2a0b00; cursor: pointer; touch-action: manipulation;
border-radius: 16px; font-size: 17px; font-style: italic; letter-spacing: .12em; outline: none;
transition: transform .06s, box-shadow .06s, background .2s; -webkit-appearance: none; appearance: none; }
.btn:active, .btn.press { transform: translateY(5px); }
.btn small { display: block; font-size: 8px; letter-spacing: .2em; font-style: normal; opacity: .75; margin-top: 2px; }
#arm { width: 112px; background: linear-gradient(180deg, #fff6b0, var(--yel) 45%, #f0a800);
box-shadow: 0 6px 0 #8a5a00, 0 9px 16px rgba(0, 0, 0, .5), 0 0 18px rgba(255, 225, 77, .55), inset 0 2px 0 rgba(255, 255, 255, .7); }
#arm:active, #arm.press { box-shadow: 0 1px 0 #8a5a00, 0 2px 6px rgba(0, 0, 0, .5), 0 0 18px rgba(255, 225, 77, .55), inset 0 2px 0 rgba(255, 255, 255, .7); }
#arm.on { color: #00303a; background: linear-gradient(180deg, #d5fdff, var(--cyan) 45%, #0aa6c9);
box-shadow: 0 6px 0 #05566c, 0 9px 16px rgba(0, 0, 0, .5), 0 0 22px rgba(34, 230, 255, .75), inset 0 2px 0 rgba(255, 255, 255, .7); }
#arm.on:active, #arm.on.press { box-shadow: 0 1px 0 #05566c, 0 2px 6px rgba(0, 0, 0, .5), 0 0 22px rgba(34, 230, 255, .75), inset 0 2px 0 rgba(255, 255, 255, .7); }
#arm.nudge { animation: nudge .45s ease-in-out 3; }
@keyframes nudge { 50% { transform: scale(1.08); filter: brightness(1.25) } }
#stop { width: 124px; color: #fff; font-size: 22px; text-shadow: 0 2px 0 rgba(90, 0, 14, .8); background: linear-gradient(180deg, #ff97a2, var(--red) 45%, #c40f26);
box-shadow: 0 6px 0 #6e0715, 0 9px 16px rgba(0, 0, 0, .5), 0 0 22px rgba(255, 59, 78, .7), inset 0 2px 0 rgba(255, 255, 255, .55); }
#stop:active, #stop.press { box-shadow: 0 1px 0 #6e0715, 0 2px 6px rgba(0, 0, 0, .5), 0 0 22px rgba(255, 59, 78, .7), inset 0 2px 0 rgba(255, 255, 255, .55); }
.mix { flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: 5px; padding: 0 9px; }
.mix div { display: flex; align-items: center; gap: 6px; }
.mix .lbl { width: 9px; letter-spacing: 0; }
.mix u { position: relative; flex: 1; height: 8px; border-radius: 4px; background: rgba(165, 143, 214, .2); overflow: hidden; }
.mix u::after { content: ""; position: absolute; left: 50%; top: 0; bottom: 0; width: 1px; background: rgba(246, 234, 255, .5); }
.mix s { position: absolute; top: 0; bottom: 0; left: 50%; width: 0; background: var(--cyan); box-shadow: 0 0 6px var(--cyan); }
.mix s.rev { background: var(--pink); box-shadow: 0 0 6px var(--pink); }
#lamp { font-size: 9px; letter-spacing: .22em; text-align: center; color: var(--dim); display: block; }
#lamp.on { color: var(--cyan); text-shadow: 0 0 8px rgba(34, 230, 255, .8); }
@media (max-height: 640px) {
.tiles { height: 96px; } .seg svg { height: 40px; } .row { height: 56px; } header { height: 32px; } .logo { font-size: 24px; }
}
@media (min-height: 800px) { .tiles { height: 124px; } .seg svg { height: 56px; } .row { height: 68px; } }
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { animation: none !important; transition: none !important; }
.near .ping { opacity: .6; }
}
</style>
</head>
<body>
<canvas id="sky" aria-hidden="true"></canvas>
<main id="app" class="wait down safe">
<header>
<div class="who"><span class="logo">ROVER</span><small id="name">TWO-TRACK DRIVE</small></div>
<div class="status">
<span id="link" class="pill wait">LINKING</span>
<div class="sigbox"><div class="sig" id="sig"><i></i><i></i><i></i><i></i><i></i></div><small id="rssi" class="num">-- dBm</small></div>
</div>
</header>
<section class="batt glass" id="batt">
<span class="lbl">BATT</span>
<div class="cells" id="cells"></div>
<b id="pct" class="num">--%</b><small id="volt" class="num">-.- V</small>
</section>
<section class="tiles">
<div class="tile glass">
<span class="lbl">SPEED</span>
<div class="seg" id="seg"></div>
<span class="unit">% POWER</span>
</div>
<div class="tile glass">
<span class="lbl">TRACKS</span>
<svg id="rov" viewBox="0 0 80 78" aria-hidden="true">
<defs>
<linearGradient id="beam" x1="0" y1="1" x2="0" y2="0"><stop offset="0" stop-color="#ffe14d" stop-opacity=".75"/><stop offset="1" stop-color="#ffe14d" stop-opacity="0"/></linearGradient>
<linearGradient id="hull" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#3a1a8a"/><stop offset="1" stop-color="#1b0a4d"/></linearGradient>
</defs>
<path id="beams" d="M31 16 L24 0 H38 L36 16 Z M49 16 L56 0 H42 L44 16 Z" fill="url(#beam)" opacity="0"/>
<rect x="11" y="12" width="15" height="60" rx="6" fill="#12052f" stroke="#ff2e97" stroke-width="1.6"/>
<rect x="54" y="12" width="15" height="60" rx="6" fill="#12052f" stroke="#ff2e97" stroke-width="1.6"/>
<line id="trL" x1="18.5" y1="15" x2="18.5" y2="69" stroke="#ff2e97" stroke-width="9" stroke-dasharray="2.5 4.5" opacity=".85"/>
<line id="trR" x1="61.5" y1="15" x2="61.5" y2="69" stroke="#ff2e97" stroke-width="9" stroke-dasharray="2.5 4.5" opacity=".85"/>
<path d="M27 24 Q27 16 35 16 H45 Q53 16 53 24 V62 Q53 68 47 68 H33 Q27 68 27 62 Z" fill="url(#hull)" stroke="#22e6ff" stroke-width="1.6"/>
<path d="M32 30 H48 L45 42 H35 Z" fill="#22e6ff" opacity=".28" stroke="#22e6ff" stroke-width="1"/>
<circle cx="33" cy="20.5" r="1.8" fill="#ffe14d"/><circle cx="47" cy="20.5" r="1.8" fill="#ffe14d"/>
<circle id="led" cx="40" cy="56" r="3" fill="#a58fd6"/>
</svg>
</div>
<div class="tile glass" id="radarTile">
<span class="lbl">RADAR</span>
<svg id="radar" viewBox="0 0 100 64" aria-hidden="true">
<g id="arcs" fill="none" stroke="#22e6ff" stroke-width="1" opacity=".5"></g>
<path class="sweep" d="M50 60 L50 10" stroke="#22e6ff" stroke-width="1.4" opacity=".55" stroke-linecap="round"/>
<circle class="ping" cx="50" cy="60" r="50" fill="none" stroke="#ff3b4e" stroke-width="2.5"/>
<path id="blip" fill="none" stroke="#ffe14d" stroke-width="3.6" stroke-linecap="round" opacity="0"/>
<path d="M50 55 L54 62 H46 Z" fill="#ff2e97"/>
</svg>
<span class="unit"><b id="dist" class="num">---</b> CM</span>
</div>
</section>
<section class="compass glass">
<span class="lbl">HDG</span>
<div class="ribbon"><div id="strip"></div><div class="needle"></div></div>
<b id="hdg" class="num">---°</b>
</section>
<section class="deck">
<div id="pad" role="application" aria-label="Joystick: drag to drive">
<canvas id="trail" aria-hidden="true"></canvas>
<div class="base">
<svg viewBox="0 0 200 200" aria-hidden="true">
<circle cx="100" cy="100" r="62" fill="none" stroke="#22e6ff" stroke-width="1" stroke-dasharray="2 7" opacity=".7"/>
<circle cx="100" cy="100" r="34" fill="none" stroke="#ff2e97" stroke-width="1" opacity=".45"/>
<path d="M100 14 L107 26 H93 Z M100 186 L107 174 H93 Z M14 100 L26 93 V107 Z M186 100 L174 93 V107 Z" fill="#ffe14d" opacity=".9"/>
<path d="M100 34 V56 M100 144 V166 M34 100 H56 M144 100 H166" stroke="#22e6ff" stroke-width="1.4" opacity=".55" stroke-linecap="round"/>
</svg>
</div>
<div id="knob"></div>
<div class="tagline" id="demoTag"><span>DEMO · TOUCH TO DRIVE</span></div>
<div class="tagline" id="hint"><span>PRESS ARM TO DRIVE</span></div>
<div id="over"><b id="overT">CONNECTING</b><small id="overS">LOOKING FOR THE ROVER</small></div>
</div>
<div class="row">
<button class="btn" id="arm" type="button" aria-pressed="false"><span id="armT">ARM</span><small id="armS">MOTORS OFF</small></button>
<div class="mix glass">
<span id="lamp">SAFE</span>
<div><span class="lbl">L</span><u><s id="barL"></s></u></div>
<div><span class="lbl">R</span><u><s id="barR"></s></u></div>
</div>
<button class="btn" id="stop" type="button">STOP</button>
</div>
</section>
</main>
<script>
(function () {
'use strict';
var $ = function (id) { return document.getElementById(id); };
var reduced = matchMedia('(prefers-reduced-motion: reduce)').matches;
// The app's bridge (docs/device-ui): there in the Connectivitizer app, missing in a browser opened straight on the rover.
var bridge = window.connectivitizer && typeof window.connectivitizer.webSocket === 'function' ? window.connectivitizer : null;
var clamp = function (v, a, b) { return v < a ? a : v > b ? b : v; };
var now = function () { return performance.now(); };
var app = $('app'), pad = $('pad'), knob = $('knob');
var link = 'wait'; // 'wait' | 'up' | 'lost'
var armed = false, armHold = 0;
var tele = null, teleAt = 0, startedAt = now();
var stick = { x: 0, y: 0 }; // what is commanded: x right, y forward, each -1..1
var held = false, pid = null, origin = { x: 0, y: 0 }, travel = 80;
var k = { x: 0, y: 0, vx: 0, vy: 0 }; // where the knob is drawn
var keys = {};
var show = { speed: 0, heading: 0, dist: null, l: 0, r: 0, offL: 0, offR: 0, hasHeading: false };
/* ---------- static parts of the HUD ---------- */
var cells = $('cells');
for (var i = 0; i < 12; i++) cells.appendChild(document.createElement('i'));
var SEGS = ['5,2 17,2 19,4 17,6 5,6 3,4', '20,5 22,7 22,16 20,18 18,16 18,7', '20,20 22,22 22,31 20,33 18,31 18,22', '5,32 17,32 19,34 17,36 5,36 3,34',
'2,20 4,22 4,31 2,33 0,31 0,22', '2,5 4,7 4,16 2,18 0,16 0,7', '5,17 17,17 19,19 17,21 5,21 3,19'];
var DIGITS = ['1111110', '0110000', '1101101', '1111001', '0110011', '1011011', '1011111', '1110000', '1111111', '1111011'];
var NS = 'http://www.w3.org/2000/svg';
var digits = [];
for (var d = 0; d < 3; d++) {
var svg = document.createElementNS(NS, 'svg');
svg.setAttribute('viewBox', '0 0 22 38');
var polys = SEGS.map(function (points) {
var p = document.createElementNS(NS, 'polygon');
p.setAttribute('points', points);
svg.appendChild(p);
return p;
});
$('seg').appendChild(svg);
digits.push(polys);
}
var shownNumber = -1;
function setNumber(n) {
if (n === shownNumber) return;
shownNumber = n;
var text = String(n);
for (var p = 0; p < 3; p++) {
var ch = text.length - 3 + p >= 0 ? text.charAt(text.length - 3 + p) : '';
var pattern = ch === '' ? '0000000' : DIGITS[+ch];
for (var s = 0; s < 7; s++) digits[p][s].setAttribute('class', pattern.charAt(s) === '1' ? 'on' : '');
}
}
setNumber(0);
// Radar: the rover sits at (50,60) and looks up; 200 cm reach the outer arc.
function arc(r, half) {
var a = (half * Math.PI) / 180, x = Math.sin(a) * r, y = Math.cos(a) * r;
return 'M' + (50 - x).toFixed(1) + ' ' + (60 - y).toFixed(1) + ' A' + r + ' ' + r + ' 0 0 1 ' + (50 + x).toFixed(1) + ' ' + (60 - y).toFixed(1);
}
[18, 34, 50].forEach(function (r) {
var p = document.createElementNS(NS, 'path');
p.setAttribute('d', arc(r, 62));
$('arcs').appendChild(p);
});
['M50 60 L5.9 36.5', 'M50 60 L94.1 36.5'].forEach(function (dd) {
var p = document.createElementNS(NS, 'path');
p.setAttribute('d', dd);
$('arcs').appendChild(p);
});
// Compass ribbon: 2.4 px a degree, from -180 to 540 so that it never runs out while it wraps.
var PX = 2.4, NAMES = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'], strip = $('strip');
for (var deg = -180; deg <= 540; deg += 15) {
var tick = document.createElement('i');
tick.style.left = deg * PX + 'px';
if (deg % 45 === 0) {
tick.className = 'm';
var name = NAMES[(((deg % 360) + 360) % 360) / 45];
var label = document.createElement('b');
label.textContent = name;
label.className = name === 'N' ? 'c n' : name.length === 1 ? 'c' : '';
label.style.left = deg * PX + 'px';
strip.appendChild(label);
}
strip.appendChild(tick);
}
/* ---------- the link to the rover ---------- */
var sock = null, sockOpen = false, retry = 0, polling = false, driving = false, zeros = 0;
function setLink(state) {
if (link === state) return;
link = state;
app.classList.toggle('down', state !== 'up');
app.classList.toggle('wait', state === 'wait');
var pill = $('link');
pill.className = 'pill' + (state === 'up' ? '' : ' ' + state);
if (state === 'up') {
pill.textContent = route();
} else {
pill.textContent = state === 'lost' ? 'NO LINK' : 'LINKING';
$('overT').textContent = state === 'lost' ? 'LINK LOST' : 'CONNECTING';
$('overS').textContent = state === 'lost' ? 'STICK ZEROED · RETRYING' : 'LOOKING FOR THE ROVER';
// Without a link nothing is commanded: the stick goes to zero and a drag in progress ends.
letGo();
keys = {};
stopAttract();
}
renderArm();
paint();
}
function route() {
var device = bridge && bridge.device;
return !bridge ? 'LOCAL' : device && device.path === 'relayed' ? 'RELAY' : device ? 'P2P' : 'LINKED';
}
function onTelemetry(t) {
if (!t || typeof t !== 'object') return;
tele = t;
teleAt = now();
if (typeof t.armed === 'boolean' && teleAt > armHold) armed = t.armed;
setLink('up');
renderTelemetry();
}
function openSocket() {
var s;
try { s = bridge.webSocket('/drive'); } catch (e) { lost(); return; }
sock = s;
s.onopen = function () { if (sock === s) sockOpen = true; };
s.onmessage = function (event) {
if (sock !== s || typeof event.data !== 'string') return;
var t = null;
try { t = JSON.parse(event.data); } catch (e) { /* not telemetry */ }
onTelemetry(t);
};
s.onclose = function () { if (sock === s) lost(); };
}
function lost() {
var s = sock;
sock = null;
sockOpen = false;
if (s) { try { s.close(); } catch (e) { /* already closed */ } }
setLink('lost');
if (bridge && !retry) retry = setTimeout(function () { retry = 0; openSocket(); }, 1500);
}
function request(path, body) {
var init = body === undefined ? {} : { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) };
var timer = 0;
if (typeof AbortController === 'function') {
var abort = new AbortController();
init.signal = abort.signal;
timer = setTimeout(function () { abort.abort(); }, 2500);
}
return fetch(path, init).then(function (response) {
clearTimeout(timer);
if (!response.ok) throw new Error('HTTP ' + response.status);
return response.json();
}, function (error) { clearTimeout(timer); throw error; });
}
// Without the bridge (a browser on the rover's own address) telemetry is asked for, five times a second.
function poll() {
if (polling || now() - teleAt < 180) return;
polling = true;
request('/telemetry').then(function (t) { polling = false; onTelemetry(t); }, function () { polling = false; if (link !== 'lost') lost(); });
}
function mix() {
var x = stick.x, y = stick.y;
if (Math.sqrt(x * x + y * y) < 0.06) return { l: 0, r: 0 };
var l = y + x * 0.85, r = y - x * 0.85, m = Math.max(1, Math.abs(l), Math.abs(r));
return { l: Math.round((l / m) * 100) / 100, r: Math.round((r / m) * 100) / 100 };
}
function send(m) {
if (bridge) {
if (sockOpen) { try { sock.send(JSON.stringify(m)); } catch (e) { lost(); } }
} else if (!driving) {
// One request at a time: a slow link gives fewer commands, never a queue of old ones.
driving = true;
request('/drive', m).then(function (t) { driving = false; onTelemetry(t); }, function () { driving = false; });
}
}
// 20 commands a second while something holds the stick, then a few zeros. The rover stops by itself
// 300 ms after the last command (its dead-man timer), whatever this page does.
setInterval(function () {
if (link !== 'up') return;
var active = held || attract.on || anyKey();
if (active) { zeros = 3; send(mix()); }
else if (zeros > 0) { zeros--; send({ l: 0, r: 0 }); }
}, 50);
setInterval(function () {
var t = now();
if (!bridge) poll();
if (link === 'up' && t - teleAt > 2000) lost();
if (link === 'wait' && t - startedAt > 5000) lost();
}, 200);
if (bridge) {
openSocket();
if (bridge.ready && bridge.ready.then) {
bridge.ready.then(function (device) {
var title = device && (device.title || device.name);
if (title && !/^rover$/i.test(title)) $('name').textContent = String(title); // the logo already says ROVER
if (link === 'up') $('link').textContent = route();
}, function () { /* the page works without knowing its name */ });
}
} else {
poll();
}
/* ---------- arming and stopping ---------- */
function buzz(ms) { try { if (navigator.vibrate) navigator.vibrate(ms); } catch (e) { /* no haptics */ } }
function renderArm() {
var on = armed && link === 'up';
app.classList.toggle('safe', !on);
$('arm').classList.toggle('on', armed);
$('arm').setAttribute('aria-pressed', armed ? 'true' : 'false');
$('armT').textContent = armed ? 'DISARM' : 'ARM';
$('armS').textContent = link !== 'up' ? 'NO LINK' : armed ? 'MOTORS LIVE' : 'MOTORS OFF';
$('lamp').textContent = on ? 'ARMED' : 'SAFE';
$('lamp').className = on ? 'on' : '';
$('led').setAttribute('fill', on ? '#22e6ff' : '#a58fd6');
$('beams').setAttribute('opacity', on ? '1' : '0');
if (on) $('hint').classList.remove('show');
}
function setArmed(want) {
armHold = now() + 600; // telemetry that was on its way does not flip the button back
if (!want) { armed = false; renderArm(); }
request('/arm', { armed: want }).then(function (answer) {
if (answer && typeof answer.armed === 'boolean') { armed = answer.armed; armHold = now() + 300; renderArm(); paint(); }
}, function () { /* the telemetry says what the rover really is */ });
}
function stopAll() {
letGo();
keys = {};
zeros = 3;
if (link === 'up') send({ l: 0, r: 0 });
setArmed(false);
buzz(60);
paint();
}
$('arm').addEventListener('click', function () { if (link === 'up') { setArmed(!armed); buzz(20); } });
$('stop').addEventListener('click', stopAll);
function nudge() {
$('hint').classList.add('show');
var arm = $('arm');
arm.classList.remove('nudge');
void arm.offsetWidth;
arm.classList.add('nudge');
}
/* ---------- the stick ---------- */
function letGo() {
if (pid !== null) { try { pad.releasePointerCapture(pid); } catch (e) { /* not captured */ } }
pid = null;
held = false;
stick.x = 0;
stick.y = 0;
app.classList.remove('held');
}
pad.addEventListener('pointerdown', function (event) {
if (pid !== null || (event.pointerType === 'mouse' && event.button !== 0)) return; // one finger steers; others are ignored
event.preventDefault();
if (link !== 'up') return;
pid = event.pointerId;
try { pad.setPointerCapture(pid); } catch (e) { /* older WebView */ }
held = true;
// The stick moves by how far the finger has travelled, wherever in the pad it came down: no jump at the touch.
origin.x = event.clientX;
origin.y = event.clientY;
stick.x = 0;
stick.y = 0;
app.classList.add('held');
if (!armed) nudge();
paint();
});
pad.addEventListener('pointermove', function (event) {
if (event.pointerId !== pid) return;
var x = (event.clientX - origin.x) / travel, y = (origin.y - event.clientY) / travel;
var length = Math.sqrt(x * x + y * y);
if (length > 1) { x /= length; y /= length; }
stick.x = x;
stick.y = y;
paint();
});
function pointerEnd(event) {
if (event.pointerId !== pid) return;
letGo();
zeros = 3;
if (link === 'up') send({ l: 0, r: 0 }); // at once, not at the next tick
paint();
}
pad.addEventListener('pointerup', pointerEnd);
pad.addEventListener('pointercancel', pointerEnd);
pad.addEventListener('lostpointercapture', pointerEnd);
pad.addEventListener('contextmenu', function (event) { event.preventDefault(); });
window.addEventListener('blur', function () { if (held || anyKey()) { letGo(); keys = {}; paint(); } });
document.addEventListener('visibilitychange', function () { if (document.hidden) { letGo(); keys = {}; } });
// Arrow keys or W A S D, for a browser with a keyboard. Space stops.
var KEYS = { ArrowUp: 'u', w: 'u', W: 'u', ArrowDown: 'd', s: 'd', S: 'd', ArrowLeft: 'l', a: 'l', A: 'l', ArrowRight: 'r', d: 'r', D: 'r' };
function anyKey() { return !!(keys.u || keys.d || keys.l || keys.r); }
function fromKeys() {
if (held) return;
stick.x = (keys.r ? 0.7 : 0) - (keys.l ? 0.7 : 0);
stick.y = (keys.u ? 1 : 0) - (keys.d ? 1 : 0);
paint();
}
window.addEventListener('keydown', function (event) {
if (event.key === ' ') { event.preventDefault(); stopAll(); return; }
var key = KEYS[event.key];
if (!key || link !== 'up') return;
event.preventDefault();
if (!keys[key]) { keys[key] = true; if (!armed) nudge(); fromKeys(); }
});
window.addEventListener('keyup', function (event) {
var key = KEYS[event.key];
if (key && keys[key]) { keys[key] = false; fromKeys(); }
});
/* ---------- attract mode: ONLY in the demo (window.czDemo), never on a real rover ----------
Like an arcade machine nobody plays: after four quiet seconds the stick draws a slow figure-eight by
itself. Any touch or key ends it at once. The page of a real device has no window.czDemo, so none of
this runs there: a rover never drives itself. */
var attract = { on: false, t: 0, quietSince: now() - 1500 };
var attractAllowed = !!window.czDemo && !reduced;
function stopAttract() {
attract.quietSince = now();
if (!attract.on) return;
attract.on = false;
stick.x = 0;
stick.y = 0;
zeros = 3;
$('demoTag').classList.remove('show');
}
function stepAttract(t, dt) {
if (!attractAllowed) return;
if (held || anyKey()) attract.quietSince = t; // quiet starts when the stick is let go
if (!attract.on) {
if (link !== 'up' || held || anyKey() || t - attract.quietSince < 4000) return;
attract.on = true;
attract.t = 0;
$('demoTag').classList.add('show');
$('hint').classList.remove('show');
if (!armed) setArmed(true);
}
attract.t += dt;
var ease = Math.min(1, attract.t / 1.2), a = attract.t * 0.7;
stick.x = Math.sin(a) * 0.72 * ease;
stick.y = (0.3 + Math.sin(a * 2) * 0.52) * ease;
}
if (attractAllowed) {
['pointerdown', 'keydown', 'wheel'].forEach(function (type) {
document.addEventListener(type, stopAttract, true); // capturing: before the pad starts its drag
});
}
/* ---------- drawing ---------- */
var sky = $('sky'), skyCtx = sky.getContext('2d'), trail = $('trail'), trailCtx = trail.getContext('2d');
var size = { w: 0, h: 0, ratio: 1, horizon: 0, padW: 0, padH: 0 };
var grid = { z: 0, x: 0 }, stars = [], lastHeading = null, trailLive = 0, lastKnob = { x: 0, y: 0 };
(function () {
var seed = 7;
for (var n = 0; n < 60; n++) {
seed = (seed * 16807) % 2147483647; var x = seed / 2147483647;
seed = (seed * 16807) % 2147483647; var y = seed / 2147483647;
seed = (seed * 16807) % 2147483647; stars.push({ x: x, y: y, s: 0.5 + (seed / 2147483647) * 1.1 });
}
})();
function layout() {
size.ratio = Math.min(window.devicePixelRatio || 1, 2);
size.w = window.innerWidth;
size.h = window.innerHeight;
sky.width = Math.round(size.w * size.ratio);
sky.height = Math.round(size.h * size.ratio);
var box = pad.getBoundingClientRect();
size.padW = box.width;
size.padH = box.height;
var diameter = Math.max(120, Math.min(box.width, box.height) - 10);
pad.style.setProperty('--d', diameter + 'px');
travel = diameter * 0.31;
size.horizon = Math.round(box.top + box.height / 2 - diameter * 0.2);
trail.width = Math.round(box.width * size.ratio);
trail.height = Math.round(box.height * size.ratio);
paint(true);
}
function drawSky(t) {
var c = skyCtx, w = size.w, h = size.h, hy = size.horizon, cx = w / 2, fh = h - hy;
c.setTransform(size.ratio, 0, 0, size.ratio, 0, 0);
var g = c.createLinearGradient(0, 0, 0, hy);
g.addColorStop(0, '#0d0629'); g.addColorStop(0.5, '#2a0a58'); g.addColorStop(0.86, '#7a1a8e'); g.addColorStop(1, '#e0409a');
c.fillStyle = g;
c.fillRect(0, 0, w, hy);
for (var n = 0; n < stars.length; n++) {
var st = stars[n], tw = reduced ? 0.7 : 0.55 + 0.45 * Math.sin(t / 900 + n * 1.7);
c.fillStyle = 'rgba(255,240,255,' + (tw * 0.8).toFixed(2) + ')';
c.fillRect(st.x * w, st.y * hy * 0.8, st.s, st.s);
}
// The sun: sets slowly, comes back up slowly.
var R = Math.min(w * 0.3, 140), set = reduced ? 0.3 : 0.5 - 0.5 * Math.cos((t / 1000 / 80) * 2 * Math.PI);
var sy = hy - R * 0.72 + set * R * 0.8;
var halo = c.createRadialGradient(cx, sy, R * 0.6, cx, sy, R * 2.4);
halo.addColorStop(0, 'rgba(255,90,170,.5)'); halo.addColorStop(1, 'rgba(255,90,170,0)');
c.fillStyle = halo;
c.fillRect(0, 0, w, hy);
c.save();
c.beginPath(); c.rect(0, 0, w, hy); c.clip();
var sun = c.createLinearGradient(0, sy - R, 0, sy + R);
sun.addColorStop(0, '#fff3a0'); sun.addColorStop(0.35, '#ffd23d'); sun.addColorStop(0.62, '#ff7a45'); sun.addColorStop(1, '#ff2e97');
c.fillStyle = sun;
c.beginPath(); c.arc(cx, sy, R, 0, 2 * Math.PI); c.fill();
c.beginPath(); c.arc(cx, sy, R, 0, 2 * Math.PI); c.clip();
c.fillStyle = g;
var phase = reduced ? 0.4 : (t / 5000) % 1;
for (var b = 0; b < 7; b++) {
var q = (b + phase) / 7;
c.fillRect(cx - R, sy - R * 0.12 + q * R * 1.15, 2 * R, 1 + q * R * 0.085);
}
c.restore();
// The floor and its grid, which rolls with the rover's speed and slides when it turns.
var floor = c.createLinearGradient(0, hy, 0, h);
floor.addColorStop(0, '#3a0d6e'); floor.addColorStop(0.25, '#1c0848'); floor.addColorStop(1, '#0a0322');
c.fillStyle = floor;
c.fillRect(0, hy, w, fh);
c.save();
c.beginPath(); c.rect(0, hy, w, fh); c.clip();
c.lineCap = 'round';
var gap = Math.max(70, w / 5), pass, n2;
for (pass = 0; pass < 2; pass++) {
c.lineWidth = pass ? 1.2 : 5;
c.strokeStyle = pass ? 'rgba(255,70,170,.95)' : 'rgba(255,46,151,.16)';
c.beginPath();
for (n2 = -16; n2 <= 16; n2++) {
var xb = cx + (n2 + grid.x) * gap * 2.2;
c.moveTo(cx + (xb - cx) * 0.02, hy + fh * 0.02); c.lineTo(xb, h + fh * 1.2);
}
c.stroke();
for (n2 = 0; n2 < 26; n2++) {
var zz = 1 + (n2 - grid.z) * 0.3;
if (zz < 0.7) continue;
var y = hy + fh / zz, alpha = clamp(((y - hy) / fh) * 2.2 + 0.12, 0, 1);
c.globalAlpha = alpha;
c.beginPath(); c.moveTo(0, y); c.lineTo(w, y); c.stroke();
}
c.globalAlpha = 1;
}
c.restore();
var glow = c.createLinearGradient(0, hy - 14, 0, hy + 26);
glow.addColorStop(0, 'rgba(255,120,200,0)'); glow.addColorStop(0.35, 'rgba(255,150,220,.75)'); glow.addColorStop(0.42, 'rgba(255,255,255,.9)');
glow.addColorStop(0.5, 'rgba(34,230,255,.5)'); glow.addColorStop(1, 'rgba(34,230,255,0)');
c.fillStyle = glow;
c.fillRect(0, hy - 14, w, 40);
}
function drawTrail(dt) {
var c = trailCtx, x = size.padW / 2 + k.x * travel, y = size.padH / 2 - k.y * travel;
var moved = Math.abs(x - lastKnob.x) + Math.abs(y - lastKnob.y);
if (moved > 0.3) trailLive = 1.2;
if (trailLive > 0) {
trailLive -= dt;
c.setTransform(size.ratio, 0, 0, size.ratio, 0, 0);
c.globalCompositeOperation = 'destination-out';
c.fillStyle = 'rgba(0,0,0,' + Math.min(0.5, dt * 5.5).toFixed(3) + ')';
c.fillRect(0, 0, size.padW, size.padH);
if (trailLive <= 0) c.clearRect(0, 0, size.padW, size.padH);
else if (moved > 0.3 && moved < 200) {
c.globalCompositeOperation = 'lighter';
c.lineCap = 'round';
var hot = armed ? '255,46,151' : '138,92,255';
c.strokeStyle = 'rgba(' + hot + ',.34)'; c.lineWidth = travel * 0.5;
c.beginPath(); c.moveTo(lastKnob.x, lastKnob.y); c.lineTo(x, y); c.stroke();
c.strokeStyle = 'rgba(34,230,255,.55)'; c.lineWidth = travel * 0.1;
c.beginPath(); c.moveTo(lastKnob.x, lastKnob.y); c.lineTo(x, y); c.stroke();
}
}
lastKnob.x = x;
lastKnob.y = y;
}
function renderTelemetry() {
var t = tele;
if (!t) return;
var percent = typeof t.batteryPercent === 'number' ? clamp(Math.round(t.batteryPercent), 0, 100) : null;
var lit = percent === null ? 0 : Math.max(percent > 0 ? 1 : 0, Math.round((percent / 100) * 12));
for (var n = 0; n < 12; n++) cells.children[n].className = n < lit ? 'on' : '';
$('batt').className = 'batt glass' + (percent !== null && percent <= 15 ? ' crit' : percent !== null && percent <= 35 ? ' warn' : '');
$('pct').textContent = percent === null ? '--%' : percent + '%';
$('volt').textContent = typeof t.battery === 'number' ? t.battery.toFixed(1) + ' V' : '-.- V';
var rssi = typeof t.rssi === 'number' ? Math.round(t.rssi) : null;
var bars = rssi === null ? 0 : rssi >= -55 ? 5 : rssi >= -63 ? 4 : rssi >= -71 ? 3 : rssi >= -80 ? 2 : 1;
var sig = $('sig');
for (var m = 0; m < 5; m++) sig.children[m].className = m < bars ? 'on' : '';
sig.className = 'sig' + (bars <= 2 ? ' low' : '');
$('rssi').textContent = rssi === null ? '-- dBm' : rssi + ' dBm';
var distance = typeof t.distanceCm === 'number' ? Math.max(0, t.distanceCm) : null;
show.dist = distance;
$('dist').textContent = distance === null ? '---' : String(Math.round(distance));
$('radarTile').classList.toggle('near', distance !== null && distance < 40);
var blip = $('blip');
if (distance === null || distance > 220) blip.setAttribute('opacity', '0');
else {
blip.setAttribute('opacity', '1');
blip.setAttribute('d', arc(Math.round(8 + 42 * Math.min(1, distance / 200)), 24));
}
renderArm();
if (reduced) paint(true);
}
// One frame: the knob, the gauges that glide, the background. `dt` in seconds.
function frame(t, dt) {
var live = link === 'up';
if (held || attract.on || anyKey()) {
var f = attract.on ? 1 : 1 - Math.exp(-dt * 40);
k.x += (stick.x - k.x) * f; k.y += (stick.y - k.y) * f; k.vx = 0; k.vy = 0;
} else if (reduced) {
k.x = 0; k.y = 0;
} else {
// Let go, the knob swings back on a spring and overshoots a little. Only the picture: the command is already zero.
var steps = Math.max(1, Math.ceil(dt / 0.008)), h = dt / steps;
for (var n = 0; n < steps; n++) {
k.vx += (-420 * k.x - 17 * k.vx) * h; k.vy += (-420 * k.y - 17 * k.vy) * h;
k.x += k.vx * h; k.y += k.vy * h;
}
if (Math.abs(k.x) + Math.abs(k.y) + Math.abs(k.vx) + Math.abs(k.vy) < 0.002) { k.x = 0; k.y = 0; k.vx = 0; k.vy = 0; }
}
knob.style.transform = 'translate3d(' + (k.x * travel).toFixed(1) + 'px,' + (-k.y * travel).toFixed(1) + 'px,0) scale(' + (held ? 1.06 : 1) + ')';
var m = live && armed ? mix() : { l: 0, r: 0 };
var ease = reduced ? 1 : 1 - Math.exp(-dt * 10);
show.l += (m.l - show.l) * ease;
show.r += (m.r - show.r) * ease;
bar($('barL'), live ? mix().l : 0);
bar($('barR'), live ? mix().r : 0);
show.offL -= show.l * dt * 60;
show.offR -= show.r * dt * 60;
$('trL').setAttribute('stroke-dashoffset', (show.offL % 7).toFixed(2));
$('trR').setAttribute('stroke-dashoffset', (show.offR % 7).toFixed(2));
var speed = live && tele && typeof tele.speed === 'number' ? clamp(tele.speed, 0, 1) : 0;
show.speed += (speed - show.speed) * (reduced ? 1 : 1 - Math.exp(-dt * 8));
setNumber(Math.round(show.speed * 100));
if (tele && typeof tele.heading === 'number') {
var target = ((tele.heading % 360) + 360) % 360;
if (!show.hasHeading || reduced) { show.heading = target; show.hasHeading = true; }
else {
var delta = ((target - show.heading + 540) % 360) - 180; // the short way round
show.heading = (show.heading + delta * (1 - Math.exp(-dt * 7)) + 360) % 360;
}
strip.style.transform = 'translate3d(' + (-show.heading * PX).toFixed(1) + 'px,0,0)';
var whole = Math.round(show.heading) % 360;
$('hdg').textContent = (whole < 10 ? '00' : whole < 100 ? '0' : '') + whole + '°';
if (lastHeading !== null && !reduced) grid.x -= (((show.heading - lastHeading + 540) % 360) - 180) / 45;
lastHeading = show.heading;
grid.x -= Math.floor(grid.x);
}
if (!reduced) {
var direction = show.l + show.r >= 0 ? 1 : -1;
grid.z += direction * show.speed * dt * 2.6;
grid.z -= Math.floor(grid.z);
}
drawSky(t);
drawTrail(dt);
}
function bar(element, value) {
var share = Math.abs(value) * 50;
element.style.width = share.toFixed(1) + '%';
element.style.left = (value < 0 ? 50 - share : 50).toFixed(1) + '%';
element.className = value < 0 ? 'rev' : '';
}
// With motion: a frame every animation frame. With reduced motion: only when something changed.
var last = 0;
function loop(t) {
var dt = last ? Math.min(0.05, (t - last) / 1000) : 0.016;
last = t;
stepAttract(t, dt);
frame(t, dt);
requestAnimationFrame(loop);
}
function paint(always) {
if (reduced || always) frame(now(), 0.016);
}
window.addEventListener('resize', layout);
layout();
renderArm();
if (!reduced) requestAnimationFrame(loop);
})();
</script>
</body>
</html>