Franja LED
demo-kit/ es lo que simula el dispositivo para la demostración en la página de inicio. El dispositivo en sí no lo necesita.
demo-kit/ es lo que simula el dispositivo para la demostración en la página de inicio. El dispositivo en sí no lo necesita.
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="color-scheme" content="dark">
<title>Lights</title>
<style>
:root {
--bg: #07060d;
--acc: 124,92,255; --acc2: 255,92,214; --acc3: 92,170,255; --accl: 214,204,255;
--text: #f3f1fb; --muted: rgba(235,232,255,.56);
--font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
--mono: ui-monospace, "SF Mono", "Cascadia Code", "Roboto Mono", Consolas, monospace;
--wheel: min(214px, calc(100vw - 180px));
}
* { box-sizing: border-box; }
html, body { min-height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.4 var(--font); overflow-x: hidden;
-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none; }
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; }
:focus { outline: none; }
:focus-visible { outline: 2px solid rgb(var(--accl)); outline-offset: 3px; }
/* ---------- the room: an ambient glow in the colour of the light ---------- */
.amb { position: fixed; inset: 0; z-index: 0; overflow: hidden; pointer-events: none; background: var(--bg); }
.amb i { position: absolute; display: block; border-radius: 50%; opacity: 1; transition: opacity 1s ease; will-change: transform, opacity; }
.amb .a1 { left: -35%; right: -35%; top: -230px; height: 560px;
background: radial-gradient(closest-side, rgba(var(--acc),.62), rgba(var(--acc),.2) 55%, rgba(var(--acc),0)); animation: drift1 16s ease-in-out infinite alternate; }
.amb .a2 { left: -45%; width: 110%; bottom: -200px; height: 520px;
background: radial-gradient(closest-side, rgba(var(--acc2),.34), rgba(var(--acc2),.1) 55%, rgba(var(--acc2),0)); animation: drift2 21s ease-in-out infinite alternate; }
.amb .a3 { right: -55%; width: 110%; top: 30%; height: 520px;
background: radial-gradient(closest-side, rgba(var(--acc3),.3), rgba(var(--acc3),.08) 55%, rgba(var(--acc3),0)); animation: drift3 19s ease-in-out infinite alternate; }
.amb::after { content: ""; position: absolute; inset: 0;
background: radial-gradient(120% 80% at 50% 40%, rgba(7,6,13,0) 40%, rgba(7,6,13,.72) 100%); }
@keyframes drift1 { to { transform: translate3d(6%, 26px, 0) scale(1.08); } }
@keyframes drift2 { to { transform: translate3d(14%, -40px, 0) scale(1.1); } }
@keyframes drift3 { to { transform: translate3d(-12%, 50px, 0) scale(.94); } }
body.off .amb i { opacity: .07; }
/* ---------- layout ---------- */
main { position: relative; z-index: 1; max-width: 540px; margin: 0 auto; display: flex; flex-direction: column; gap: 12px;
padding: calc(12px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left)); }
.glass { position: relative; border-radius: 24px; border: 1px solid rgba(255,255,255,.13);
background: linear-gradient(160deg, rgba(255,255,255,.11), rgba(255,255,255,.035) 45%, rgba(255,255,255,.02));
box-shadow: inset 0 1px 0 rgba(255,255,255,.22), inset 0 -1px 0 rgba(0,0,0,.25), 0 24px 44px -26px rgba(0,0,0,.85);
-webkit-backdrop-filter: blur(22px) saturate(1.5); backdrop-filter: blur(22px) saturate(1.5); }
.glass::before { content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none;
background: linear-gradient(115deg, rgba(255,255,255,.1), rgba(255,255,255,0) 28%); }
.dim { transition: opacity .7s ease, filter .7s ease; }
body.off .dim { opacity: .5; filter: saturate(.45); }
/* ---------- header ---------- */
header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 2px 2px 0 6px; }
h1 { margin: 0; font-size: 25px; line-height: 1.15; font-weight: 700; letter-spacing: -.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
padding: 22px; margin: -22px; text-shadow: 0 0 20px rgba(var(--acc),.75); }
.who { min-width: 0; flex: 1; }
.sub { position: relative; margin: 3px 0 0; display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.led { width: 7px; height: 7px; border-radius: 50%; flex: none; background: rgb(var(--accl)); box-shadow: 0 0 10px 2px rgba(var(--acc),.95); transition: background .4s, box-shadow .4s; }
body.off .led { background: rgba(255,255,255,.3); box-shadow: none; }
body.offline .led { background: #ffb347; box-shadow: 0 0 10px 2px rgba(255,160,60,.8); animation: blink 1s ease-in-out infinite; }
@keyframes blink { 50% { opacity: .25; } }
.power { position: relative; flex: none; width: 62px; height: 62px; border-radius: 50%; display: grid; place-items: center;
border: 1px solid rgba(255,255,255,.4); color: #fff;
background: radial-gradient(circle at 32% 24%, rgba(255,255,255,.6), rgba(255,255,255,0) 46%), rgba(var(--acc),.82);
box-shadow: 0 0 0 5px rgba(var(--acc),.16), 0 0 34px 6px rgba(var(--acc),.7), inset 0 -8px 14px rgba(0,0,0,.28), inset 0 1px 0 rgba(255,255,255,.6);
transition: background-color .6s, box-shadow .6s, color .6s, border-color .6s, transform .15s; }
.power svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; filter: drop-shadow(0 1px 2px rgba(0,0,0,.45)); }
.power:active { transform: scale(.93); }
.power::after { content: ""; position: absolute; inset: -5px; border-radius: 50%; border: 1px solid rgba(var(--accl),.55); opacity: 0; pointer-events: none; }
.power.ping::after { animation: ping .7s ease-out; }
@keyframes ping { from { opacity: .9; transform: scale(1); } to { opacity: 0; transform: scale(1.7); } }
body.off .power { color: rgba(255,255,255,.55); border-color: rgba(255,255,255,.16);
background: radial-gradient(circle at 32% 24%, rgba(255,255,255,.2), rgba(255,255,255,0) 46%), rgba(255,255,255,.05);
box-shadow: 0 0 0 5px rgba(255,255,255,.03), 0 0 0 0 rgba(var(--acc),0), inset 0 -8px 14px rgba(0,0,0,.3), inset 0 1px 0 rgba(255,255,255,.2); }
/* ---------- the strip ---------- */
.stage { overflow: hidden; background: linear-gradient(180deg, rgba(10,9,20,.5), rgba(10,9,20,.72)); }
#strip { display: block; width: 100%; height: 132px; }
.cap { position: absolute; left: 16px; right: 16px; top: 11px; display: flex; justify-content: space-between; align-items: baseline;
font-size: 10.5px; letter-spacing: .16em; text-transform: uppercase; color: var(--muted); pointer-events: none; }
.cap b { font-weight: 600; color: rgb(var(--accl)); text-shadow: 0 0 12px rgba(var(--acc),.9); transition: color .5s; }
body.off .cap b { color: var(--muted); text-shadow: none; }
/* ---------- colour ---------- */
.pick { display: flex; align-items: center; gap: 14px; padding: 14px; }
.wheel { position: relative; flex: none; width: var(--wheel); height: var(--wheel); border-radius: 50%; touch-action: none; cursor: crosshair;
box-shadow: 0 0 0 1px rgba(255,255,255,.22), 0 0 0 7px rgba(255,255,255,.05), 0 0 0 8px rgba(255,255,255,.1), 0 0 46px 4px rgba(var(--acc),.5), 0 18px 30px -12px rgba(0,0,0,.8); }
.wheel canvas { display: block; width: 100%; height: 100%; border-radius: 50%; }
.wheel::after { content: ""; position: absolute; inset: 0; border-radius: 50%; pointer-events: none;
background: radial-gradient(90% 70% at 30% 12%, rgba(255,255,255,.3), rgba(255,255,255,0) 52%);
box-shadow: inset 0 0 22px rgba(0,0,0,.42), inset 0 -10px 26px rgba(0,0,0,.3), inset 0 1px 1px rgba(255,255,255,.5); }
.knob { position: absolute; left: 0; top: 0; width: 34px; height: 34px; margin: -17px 0 0 -17px; border-radius: 50%; pointer-events: none; z-index: 2;
border: 3px solid #fff; background: rgb(var(--pick, 124,92,255));
box-shadow: 0 0 0 1px rgba(0,0,0,.35), 0 0 20px 5px rgba(var(--pick, 124,92,255),.9), 0 4px 10px rgba(0,0,0,.5), inset 0 2px 3px rgba(255,255,255,.55);
transition: width .15s, height .15s, margin .15s; }
.knob.ease { transition: transform .7s cubic-bezier(.3,.9,.3,1), width .15s, height .15s, margin .15s; }
.wheel.drag .knob { width: 42px; height: 42px; margin: -21px 0 0 -21px; }
.side { flex: 1; min-width: 86px; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.read { text-align: center; }
.hex { font: 600 15px/1.2 var(--mono); letter-spacing: .04em; color: rgb(var(--pickl, 214,204,255)); text-shadow: 0 0 14px rgba(var(--pick, 124,92,255),.9); }
.hs { margin-top: 2px; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.presets { display: grid; grid-template-columns: repeat(2, 38px); gap: 10px; }
.presets button { width: 38px; height: 38px; border-radius: 50%; background: radial-gradient(circle at 34% 26%, rgba(255,255,255,.75), rgba(255,255,255,0) 48%), rgb(var(--c));
border: 1px solid rgba(255,255,255,.45); box-shadow: 0 0 16px 1px rgba(var(--c),.6), inset 0 -5px 8px rgba(0,0,0,.25); transition: transform .15s, box-shadow .3s; }
.presets button:active { transform: scale(.9); }
.presets button.sel { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px #fff, 0 0 20px 4px rgba(var(--c),.85), inset 0 -5px 8px rgba(0,0,0,.25); }
/* ---------- sliders ---------- */
.sliders { padding: 12px 16px 8px; display: grid; gap: 4px; }
.row label { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; letter-spacing: .01em; }
.row svg { width: 17px; height: 17px; fill: none; stroke: rgb(var(--accl)); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; filter: drop-shadow(0 0 6px rgba(var(--acc),.9)); }
.row output { margin-left: auto; font: 500 12.5px var(--mono); color: var(--muted); font-variant-numeric: tabular-nums; }
.rng { position: relative; height: 36px; --p: .5; }
.rng::before { content: ""; position: absolute; left: 0; right: 0; top: 13px; height: 10px; border-radius: 99px; background: rgba(255,255,255,.09);
box-shadow: inset 0 1px 3px rgba(0,0,0,.6), 0 1px 0 rgba(255,255,255,.07); }
.rng i { position: absolute; left: 0; top: 13px; height: 10px; border-radius: 99px; width: calc(14px + (100% - 28px) * var(--p)); pointer-events: none;
background: linear-gradient(90deg, rgba(var(--acc3),.75), rgb(var(--acc)) 55%, rgb(var(--accl)));
box-shadow: 0 0 14px 1px rgba(var(--acc),.85), 0 0 30px 2px rgba(var(--acc),.4), inset 0 1px 0 rgba(255,255,255,.5); }
.rng input { position: absolute; inset: 0; width: 100%; height: 36px; margin: 0; background: none; -webkit-appearance: none; appearance: none; touch-action: pan-y; cursor: pointer; }
.rng input::-webkit-slider-runnable-track { height: 36px; background: none; }
.rng input::-moz-range-track { height: 36px; background: none; }
.rng input::-webkit-slider-thumb { -webkit-appearance: none; width: 28px; height: 28px; margin-top: 4px; border-radius: 50%; border: 0;
background: radial-gradient(circle at 36% 28%, #fff, #e9e6f7 55%, #b8b3d6);
box-shadow: 0 0 0 1px rgba(255,255,255,.7), 0 0 18px 4px rgba(var(--acc),.85), 0 4px 9px rgba(0,0,0,.55); }
.rng input::-moz-range-thumb { width: 28px; height: 28px; border-radius: 50%; border: 0;
background: radial-gradient(circle at 36% 28%, #fff, #e9e6f7 55%, #b8b3d6);
box-shadow: 0 0 0 1px rgba(255,255,255,.7), 0 0 18px 4px rgba(var(--acc),.85), 0 4px 9px rgba(0,0,0,.55); }
/* ---------- effects ---------- */
h2 { margin: 4px 6px 0; font-size: 10.5px; font-weight: 600; letter-spacing: .16em; text-transform: uppercase; color: var(--muted); }
.chips { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.chip { position: relative; min-width: 0; padding: 8px 6px 9px; border-radius: 16px; text-align: center; border: 1px solid rgba(255,255,255,.11);
background: linear-gradient(160deg, rgba(255,255,255,.09), rgba(255,255,255,.025));
box-shadow: inset 0 1px 0 rgba(255,255,255,.16), 0 12px 22px -16px rgba(0,0,0,.9);
-webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
transition: border-color .35s, box-shadow .35s, background-color .35s, transform .15s; }
.chip:active { transform: scale(.95); }
.chip canvas { display: block; width: 100%; height: 24px; }
.chip span { display: block; margin-top: 3px; font-size: 11.5px; font-weight: 600; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; transition: color .35s; }
.chip.sel { border-color: rgba(var(--accl),.85); background-color: rgba(var(--acc),.2);
box-shadow: inset 0 1px 0 rgba(255,255,255,.3), 0 0 0 1px rgba(var(--acc),.5), 0 0 24px 1px rgba(var(--acc),.6), inset 0 0 18px rgba(var(--acc),.28); }
.chip.sel span { color: #fff; text-shadow: 0 0 10px rgba(var(--acc),.9); }
/* ---------- offline ---------- */
.net { position: fixed; z-index: 5; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom)); transform: translate(-50%, 90px); opacity: 0;
display: flex; align-items: center; gap: 9px; padding: 10px 16px; border-radius: 99px; font-size: 13px; white-space: nowrap;
color: #ffe2bd; background: rgba(40,24,10,.82); border: 1px solid rgba(255,170,80,.45); box-shadow: 0 0 24px rgba(255,140,40,.3), 0 10px 24px rgba(0,0,0,.5);
-webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px); transition: transform .4s cubic-bezier(.3,1.3,.5,1), opacity .3s; }
body.offline .net { transform: translate(-50%, 0); opacity: 1; }
.net i { width: 8px; height: 8px; border-radius: 50%; background: #ffb347; box-shadow: 0 0 10px #ff9a2e; animation: blink 1s ease-in-out infinite; }
@media (max-width: 345px) { .presets { gap: 8px; } .chips { gap: 6px; } :root { --wheel: calc(100vw - 166px); } }
/* a phone frame on a web page is shorter than a phone: everything stays on one screen */
@media (max-height: 800px) and (max-width: 599px) {
:root { --wheel: min(200px, calc(100vw - 180px)); }
main { gap: 10px; padding-top: calc(8px + env(safe-area-inset-top)); padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
h1 { font-size: 23px; } .power { width: 54px; height: 54px; } .power svg { width: 23px; height: 23px; }
#strip { height: 128px; } .pick { padding: 12px 14px; }
.presets { grid-template-columns: repeat(2, 34px); gap: 9px; } .presets button { width: 34px; height: 34px; }
.sliders { padding: 9px 16px 4px; gap: 0; } .rng, .rng input { height: 32px; } .rng::before, .rng i { top: 11px; }
.rng input::-webkit-slider-runnable-track { height: 32px; } .rng input::-webkit-slider-thumb { width: 26px; height: 26px; margin-top: 3px; }
.chip { padding: 6px 6px 8px; } .chip canvas { height: 22px; } h2 { margin-top: 2px; }
}
@media (max-height: 739px) and (max-width: 599px) { :root { --wheel: min(178px, calc(100vw - 180px)); } #strip { height: 110px; } .side { gap: 8px; } }
@media (min-width: 600px) { main { gap: 16px; padding-top: 24px; } #strip { height: 150px; } :root { --wheel: 250px; } .presets { grid-template-columns: repeat(3, 42px); gap: 14px; } .presets button { width: 42px; height: 42px; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
</style>
</head>
<body>
<div class="amb" aria-hidden="true"><i class="a1"></i><i class="a2"></i><i class="a3"></i></div>
<main>
<header>
<div class="who">
<h1 id="title">Lights</h1>
<p class="sub"><span class="led"></span><span id="sub">Connecting…</span></p>
</div>
<button class="power" id="power" type="button" aria-pressed="true" aria-label="Power">
<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3.5v8"/><path d="M6.6 6.9a8 8 0 1 0 10.8 0"/></svg>
</button>
</header>
<section class="glass stage" aria-label="Live preview of the strip">
<canvas id="strip" role="img" aria-label="Preview of the LED strip"></canvas>
<div class="cap"><b id="fxname"> </b><span id="count"></span></div>
</section>
<section class="glass pick dim" aria-label="Colour">
<div class="wheel" id="wheel" tabindex="0" role="slider" aria-label="Colour: arrow keys change hue and saturation" aria-valuemin="0" aria-valuemax="359" aria-valuenow="0">
<canvas id="wheelc"></canvas>
<div class="knob" id="knob"></div>
</div>
<div class="side">
<div class="read"><div class="hex" id="hex">#------</div><div class="hs" id="hs"> </div></div>
<div class="presets" id="presets"></div>
</div>
</section>
<section class="glass sliders dim">
<div class="row">
<label for="bri"><svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="4"/><path d="M12 2.5v2.5M12 19v2.5M2.5 12H5M19 12h2.5M5.3 5.3l1.8 1.8M16.9 16.9l1.8 1.8M5.3 18.7l1.8-1.8M16.9 7.1l1.8-1.8"/></svg>Brightness<output id="briv" for="bri"></output></label>
<div class="rng" id="brir"><i></i><input id="bri" type="range" min="0" max="255" step="1" value="128"></div>
</div>
<div class="row">
<label for="spd"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4.5 17a8.5 8.5 0 1 1 15 0"/><path d="M12 14l4-5.5"/><circle cx="12" cy="14" r="1.4"/></svg>Speed<output id="spdv" for="spd"></output></label>
<div class="rng" id="spdr"><i></i><input id="spd" type="range" min="0" max="100" step="1" value="50"></div>
</div>
</section>
<section class="dim" aria-label="Effects">
<h2>Effects</h2>
<div class="chips" id="chips" style="margin-top:9px"></div>
</section>
</main>
<div class="net" role="status" aria-live="polite"><i></i><span>Offline, retrying…</span></div>
<script>
(function () {
'use strict';
var $ = function (id) { return document.getElementById(id); };
var RM = !!(window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches);
var BG = '#07060d';
var POLL_MS = 1500, SEND_MS = 110;
// Shown until the device has answered GET /effects (and if it never does).
var effects = [['solid', 'Solid'], ['rainbow', 'Rainbow'], ['breathe', 'Breathe'], ['chase', 'Chase'], ['sparkle', 'Sparkle'], ['fire', 'Fire'], ['aurora', 'Aurora'], ['sunrise', 'Sunrise']]
.map(function (e) { return { id: e[0], name: e[1] }; });
var OWN = { rainbow: 1, fire: 1, sunrise: 1 }; // effects that bring their own colours
var PRESETS = ['#ffb46b', '#ff3d8b', '#ff7a1a', '#2dffb3', '#26c6ff', '#7c5cff'];
// What the device last said, changed at once by what the person does here.
var S = { on: true, brightness: 204, color: '#7c5cff', effect: 'solid', speed: 55, leds: 60 };
var known = false; // false until the first answer
var hue = 252, sat = 0.64; // the wheel's own position: the hue survives a trip through white
// ---------- colour maths ----------
function clamp(v, a, b) { return v < a ? a : v > b ? b : v; }
function hsv(h, s, v, o) {
h = (((h % 360) + 360) % 360) / 60;
var i = Math.floor(h), f = h - i, p = v * (1 - s), q = v * (1 - s * f), u = v * (1 - s * (1 - f));
if (i === 0) { o[0] = v; o[1] = u; o[2] = p; } else if (i === 1) { o[0] = q; o[1] = v; o[2] = p; }
else if (i === 2) { o[0] = p; o[1] = v; o[2] = u; } else if (i === 3) { o[0] = p; o[1] = q; o[2] = v; }
else if (i === 4) { o[0] = u; o[1] = p; o[2] = v; } else { o[0] = v; o[1] = p; o[2] = q; }
return o;
}
function toHsv(r, g, b) {
var mx = Math.max(r, g, b), mn = Math.min(r, g, b), d = mx - mn, h = 0;
if (d > 0) {
if (mx === r) h = ((g - b) / d) % 6; else if (mx === g) h = (b - r) / d + 2; else h = (r - g) / d + 4;
h *= 60; if (h < 0) h += 360;
}
return [h, mx > 0 ? d / mx : 0, mx];
}
function hexRgb(hex) { var n = parseInt(hex.slice(1), 16); return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255]; }
function rgbHex(c) {
var n = (Math.round(clamp(c[0], 0, 1) * 255) << 16) | (Math.round(clamp(c[1], 0, 1) * 255) << 8) | Math.round(clamp(c[2], 0, 1) * 255);
return '#' + ('000000' + n.toString(16)).slice(-6);
}
function triple(c) { return Math.round(c[0] * 255) + ',' + Math.round(c[1] * 255) + ',' + Math.round(c[2] * 255); }
function lighten(c, k) { return [c[0] + (1 - c[0]) * k, c[1] + (1 - c[1]) * k, c[2] + (1 - c[2]) * k]; }
function lum(c) {
var f = function (v) { return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); };
return 0.2126 * f(c[0]) + 0.7152 * f(c[1]) + 0.0722 * f(c[2]);
}
function contrast(a, b) { var x = lum(a), y = lum(b); return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05); }
// ---------- the effects, as the firmware computes them: LED i of n at effect time t ----------
var BH = 252, BS = 0.64; // hue and saturation of the colour being shown
function hash(a, b) { var x = Math.sin(a * 127.1 + b * 311.7) * 43758.5453; return x - Math.floor(x); }
function noise(x, y) {
var xi = Math.floor(x), yi = Math.floor(y), xf = x - xi, yf = y - yi;
xf = xf * xf * (3 - 2 * xf); yf = yf * yf * (3 - 2 * yf);
var a = hash(xi, yi), b = hash(xi + 1, yi), c = hash(xi, yi + 1), d = hash(xi + 1, yi + 1);
return a + (b - a) * xf + (c - a) * yf + (a - b - c + d) * xf * yf;
}
var DAWN = [[0, 0.14, 0, 0.03], [0.3, 0.75, 0.06, 0], [0.55, 1, 0.34, 0], [0.8, 1, 0.62, 0.18], [1, 1, 0.86, 0.6]];
function scale(c, l, o) { o[0] = c[0] * l; o[1] = c[1] * l; o[2] = c[2] * l; }
var FX = {
solid: function (i, n, t, c, o) { scale(c, 1, o); },
rainbow: function (i, n, t, c, o) { hsv((i / n + t * 0.12) * 360, 1, 1, o); },
breathe: function (i, n, t, c, o) { scale(c, 0.1 + 0.9 * (0.5 - 0.5 * Math.cos(t * 1.6)), o); },
chase: function (i, n, t, c, o) {
var best = 0, tail = n * 0.22;
for (var k = 0; k < 3; k++) {
var d = (((t * 0.3 + k / 3) % 1) * n - i) % n; if (d < 0) d += n;
if (d < tail) { var v = 1 - d / tail; v *= v; if (v > best) best = v; }
}
scale(c, 0.04 + 0.96 * best, o);
},
sparkle: function (i, n, t, c, o) {
var s = t / 1.1 + hash(i, 1), k = Math.floor(s), f = s - k;
var v = hash(i, k + 7) < 0.3 ? Math.exp(-f * 4.5) : 0;
scale(c, 0.1 + 0.5 * v, o);
o[0] += 0.5 * v * v; o[1] += 0.5 * v * v; o[2] += 0.5 * v * v;
},
fire: function (i, n, t, c, o) {
var j = i * 60 / n, h = clamp(0.08 + 0.58 * noise(j * 0.3, t * 2.4) + 0.3 * noise(j * 0.8 + 31, t * 5), 0, 1) * 3;
o[0] = Math.min(1, h); o[1] = clamp(h - 1, 0, 1) * 0.9; o[2] = clamp(h - 2, 0, 1) * 0.5;
},
aurora: function (i, n, t, c, o) {
var j = i * 60 / n;
var v = 0.5 + 0.5 * Math.sin(j * 0.17 + t * 0.55 + 2 * Math.sin(t * 0.13));
hsv(BH + 50 * Math.sin(j * 0.11 + t * 0.4) + 22 * Math.sin(j * 0.23 - t * 0.27), 0.5 + 0.5 * BS, 0.2 + 0.8 * v * v, o);
},
sunrise: function (i, n, t, c, o) {
var q = t % 26, p = q < 19 ? q / 19 : q < 23 ? 1 : (26 - q) / 3;
p = p * p * (3 - 2 * p);
var l = 0.1 + 0.9 * clamp(p * 1.9 - Math.abs((i + 0.5) / n - 0.5) * 1.8, 0, 1), a = DAWN[0], b = DAWN[1];
for (var k = 1; k < DAWN.length && DAWN[k][0] < l; k++) { a = DAWN[k]; b = DAWN[Math.min(k + 1, DAWN.length - 1)]; }
var f = b[0] > a[0] ? clamp((l - a[0]) / (b[0] - a[0]), 0, 1) : 0;
o[0] = a[1] + (b[1] - a[1]) * f; o[1] = a[2] + (b[2] - a[2]) * f; o[2] = a[3] + (b[3] - a[3]) * f;
},
};
function fx(id) { return FX[id] || FX.solid; }
function rate(speed) { return 0.2 + Math.pow(speed / 100, 1.5) * 2.8; }
// ---------- what is on screen, moving towards S ----------
var D = { c: hexRgb(S.color), bri: S.brightness / 255, lvl: 0, briUi: S.brightness, spdUi: S.speed, amb: [0.49, 0.36, 1] };
var curFx = S.effect, prevFx = S.effect, mix = 1, tEff = 3.1, tChip = 3.1;
// ---------- elements ----------
var body = document.body, root = document.documentElement;
var strip = $('strip'), sx = strip.getContext('2d'), wheel = $('wheel'), wheelc = $('wheelc'), knob = $('knob');
var bri = $('bri'), spd = $('spd'), chipsEl = $('chips'), presetsEl = $('presets');
var dpr = 1, sw = 0, sh = 0, sn = 0, px = [], py = [], floorY = 0, wheelSize = 0;
var chips = []; // { id, el, canvas, ctx, w, h }
var device = null;
function setVar(name, value) { if (setVar[name] !== value) { setVar[name] = value; root.style.setProperty(name, value); } }
// ---------- the wheel ----------
function paintWheel() {
var size = Math.round(wheel.clientWidth * dpr);
if (!size || size === wheelc.width) return;
wheelc.width = wheelc.height = size;
var g = wheelc.getContext('2d'), img = g.createImageData(size, size), data = img.data, R = size / 2, o = [0, 0, 0], at = 0;
for (var y = 0; y < size; y++) {
for (var x = 0; x < size; x++, at += 4) {
var dx = x + 0.5 - R, dy = y + 0.5 - R, r = Math.sqrt(dx * dx + dy * dy) / R;
if (r > 1.01) continue;
hsv(Math.atan2(dy, dx) * 180 / Math.PI + 90, Math.min(1, r), 1, o);
data[at] = o[0] * 255; data[at + 1] = o[1] * 255; data[at + 2] = o[2] * 255; data[at + 3] = 255;
}
}
g.putImageData(img, 0, 0);
}
function placeKnob(ease) {
var R = wheel.clientWidth / 2, a = (hue - 90) * Math.PI / 180, r = sat * (R - 4);
knob.classList.toggle('ease', !!ease && !RM);
knob.style.transform = 'translate(' + (R + Math.cos(a) * r).toFixed(1) + 'px,' + (R + Math.sin(a) * r).toFixed(1) + 'px)';
wheel.setAttribute('aria-valuenow', String(Math.round(hue) % 360));
wheel.setAttribute('aria-valuetext', S.color);
}
function pickAt(event) {
var box = wheel.getBoundingClientRect(), R = box.width / 2, dx = event.clientX - box.left - R, dy = event.clientY - box.top - R;
var r = Math.min(1, Math.sqrt(dx * dx + dy * dy) / (R - 4));
if (r > 0.02) hue = (Math.atan2(dy, dx) * 180 / Math.PI + 90 + 360) % 360;
sat = r < 0.04 ? 0 : r;
pickColour(false);
}
function pickColour(ease) {
change({ color: rgbHex(hsv(hue, sat, 1, [0, 0, 0])) });
placeKnob(ease);
}
var dragging = false;
wheel.addEventListener('pointerdown', function (event) {
dragging = true; wheel.classList.add('drag');
try { wheel.setPointerCapture(event.pointerId); } catch (error) { /* an old WebView */ }
pickAt(event); event.preventDefault();
});
wheel.addEventListener('pointermove', function (event) { if (dragging) pickAt(event); });
['pointerup', 'pointercancel', 'lostpointercapture'].forEach(function (name) {
wheel.addEventListener(name, function () { dragging = false; wheel.classList.remove('drag'); });
});
wheel.addEventListener('keydown', function (event) {
var k = event.key;
if (k === 'ArrowLeft') hue = (hue + 354) % 360; else if (k === 'ArrowRight') hue = (hue + 6) % 360;
else if (k === 'ArrowUp') sat = clamp(sat + 0.05, 0, 1); else if (k === 'ArrowDown') sat = clamp(sat - 0.05, 0, 1); else return;
event.preventDefault(); pickColour(false);
});
PRESETS.forEach(function (hex) {
var b = document.createElement('button');
b.type = 'button'; b.setAttribute('aria-label', 'Colour ' + hex); b.dataset.hex = hex;
b.style.setProperty('--c', triple(hexRgb(hex)));
b.addEventListener('click', function () {
var h = toHsv.apply(null, hexRgb(hex)); hue = h[0]; sat = h[1];
change({ color: hex }); placeKnob(true);
});
presetsEl.appendChild(b);
});
// ---------- sliders, power, effects ----------
var holding = 0; // a slider under a finger is not moved by the device
[[bri, 'brightness'], [spd, 'speed']].forEach(function (pair) {
var input = pair[0], key = pair[1];
input.addEventListener('input', function () {
var patch = {}; patch[key] = +input.value;
if (key === 'brightness') D.briUi = +input.value; else D.spdUi = +input.value;
change(patch);
});
input.addEventListener('pointerdown', function () { holding++; });
});
['pointerup', 'pointercancel'].forEach(function (name) { window.addEventListener(name, function () { holding = 0; }); });
$('power').addEventListener('click', function () {
var p = $('power'); p.classList.remove('ping'); void p.offsetWidth; p.classList.add('ping');
change({ on: !S.on });
});
function buildChips() {
while (chipsEl.firstChild) chipsEl.removeChild(chipsEl.firstChild);
chips = effects.map(function (effect) {
var el = document.createElement('button'), canvas = document.createElement('canvas'), label = document.createElement('span');
el.type = 'button'; el.className = 'chip'; el.setAttribute('aria-pressed', 'false');
label.textContent = effect.name; // text, never HTML: the name comes from the device
el.appendChild(canvas); el.appendChild(label);
el.addEventListener('click', function () { change({ effect: effect.id }); });
chipsEl.appendChild(el);
return { id: effect.id, el: el, canvas: canvas, ctx: canvas.getContext('2d'), w: 0, h: 0 };
});
sizeChips(); show();
}
function sizeChips() {
chips.forEach(function (chip) {
chip.w = chip.canvas.clientWidth; chip.h = chip.canvas.clientHeight;
chip.canvas.width = Math.round(chip.w * dpr); chip.canvas.height = Math.round(chip.h * dpr);
});
}
// ---------- state to screen (everything but the canvases) ----------
function effectName(id) {
for (var i = 0; i < effects.length; i++) if (effects[i].id === id) return effects[i].name;
return id;
}
function show() {
body.classList.toggle('off', !S.on);
$('power').setAttribute('aria-pressed', String(S.on));
$('power').setAttribute('aria-label', S.on ? 'Turn off' : 'Turn on');
$('fxname').textContent = known ? effectName(S.effect) : ' ';
$('count').textContent = known ? S.leds + ' LEDs' : '';
var link = device ? (device.path === 'direct' ? ' · direct' : ' · relayed') : '';
$('sub').textContent = !known ? 'Connecting…' : (S.on ? 'On' : 'Off') + ' · ' + Math.round(S.brightness / 2.55) + '%' + link;
$('hex').textContent = S.color.toUpperCase();
$('hs').textContent = OWN[S.effect] ? 'own colours' : Math.round(hue) % 360 + '° · ' + Math.round(sat * 100) + '%';
$('briv').textContent = Math.round(S.brightness / 2.55) + '%';
$('spdv').textContent = S.speed + '%';
var pick = hexRgb(S.color);
setVar('--pick', triple(pick)); setVar('--pickl', triple(lighten(pick, 0.55)));
for (var i = 0; i < chips.length; i++) {
var on = chips[i].id === S.effect;
chips[i].el.classList.toggle('sel', on); chips[i].el.setAttribute('aria-pressed', String(on));
}
var presets = presetsEl.children;
for (var k = 0; k < presets.length; k++) presets[k].classList.toggle('sel', presets[k].dataset.hex === S.color);
if (S.effect !== curFx) { prevFx = curFx; curFx = S.effect; mix = RM ? 1 : 0; }
themeSoon();
if (RM) frame(0);
}
// The app's bar follows the colour: a fixed dark background, and the colour lightened until it reads well on it.
var themeTimer = null, themeSent = '';
function themeSoon() {
var host = window.connectivitizer;
if (!host || typeof host.setTheme !== 'function' || !known) return;
clearTimeout(themeTimer);
themeTimer = setTimeout(function () {
var dark = hexRgb(BG), fg = hsv(hue, sat, 1, [0, 0, 0]), k = 0.35;
fg = lighten(fg, k);
while (contrast(fg, dark) < 7 && k < 1) { k += 0.1; fg = lighten(hsv(hue, sat, 1, [0, 0, 0]), Math.min(1, k)); }
var hex = rgbHex(fg);
if (hex === themeSent || contrast(fg, dark) < 4.5) return;
themeSent = hex;
try { host.setTheme({ background: BG, foreground: hex }); } catch (error) { /* the bar keeps the manifest's colours */ }
}, 450);
}
// ---------- talking to the device ----------
var lastLocal = 0, pending = null, inflight = false, sendTimer = null, fails = 0;
function request(path, init) {
return new Promise(function (resolve, reject) {
var timer = setTimeout(function () { reject(new Error('timeout')); }, 8000);
fetch(path, init).then(function (response) {
clearTimeout(timer);
if (!response.ok) throw new Error('HTTP ' + response.status);
return response.json();
}).then(resolve, function (error) { clearTimeout(timer); reject(error); });
});
}
function online(ok) {
fails = ok ? 0 : fails + 1;
body.classList.toggle('offline', fails >= 2 || (!ok && !known));
}
// What the device answered becomes the state, field by field and only what has the right shape.
function apply(state) {
if (!state || typeof state !== 'object') return;
var first = !known; known = true;
if (typeof state.on === 'boolean') S.on = state.on;
if (typeof state.brightness === 'number') S.brightness = clamp(Math.round(state.brightness), 0, 255);
if (typeof state.speed === 'number') S.speed = clamp(Math.round(state.speed), 0, 100);
if (typeof state.leds === 'number' && state.leds >= 1) { var n = Math.round(state.leds); if (n !== S.leds) { S.leds = n; layout(); } }
if (typeof state.effect === 'string' && state.effect) S.effect = state.effect;
if (typeof state.color === 'string' && /^#[0-9a-f]{6}$/i.test(state.color)) {
var hex = state.color.toLowerCase();
if (hex !== S.color || first) {
S.color = hex;
if (hex !== rgbHex(hsv(hue, sat, 1, [0, 0, 0]))) {
var h = toHsv.apply(null, hexRgb(hex)); if (h[1] > 0.01) hue = h[0]; sat = h[1];
}
placeKnob(!first);
}
}
if (first) { curFx = prevFx = S.effect; mix = 1; D.c = hexRgb(S.color); D.briUi = S.brightness; D.spdUi = S.speed; D.bri = S.brightness / 255; }
show();
}
function change(patch) {
for (var key in patch) S[key] = patch[key];
lastLocal = Date.now();
pending = pending || {};
for (var k in patch) pending[k] = patch[k];
show();
if (!inflight && !sendTimer) sendTimer = setTimeout(send, SEND_MS);
}
function send() {
sendTimer = null;
if (!pending || inflight) return;
var sent = pending; pending = null; inflight = true;
request('/state', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(sent) }).then(function (state) {
inflight = false; online(true);
if (pending) sendTimer = setTimeout(send, SEND_MS); // more has happened since: the newest wins
else if (!dragging && !holding) apply(state);
}, function () {
inflight = false; online(false);
for (var key in pending || {}) sent[key] = pending[key];
pending = sent; // nothing is lost: it goes out again
sendTimer = setTimeout(send, 1500);
});
}
function poll() {
if (document.hidden || inflight || pending || dragging || holding || Date.now() - lastLocal < 1200) return;
var asked = lastLocal;
request('/state').then(function (state) {
online(true);
if (asked === lastLocal && !inflight && !pending && !dragging && !holding) apply(state);
}, function () { online(false); });
}
function loadEffects() {
request('/effects').then(function (list) {
if (!Array.isArray(list)) return;
var clean = list.filter(function (e) { return e && typeof e.id === 'string' && e.id; })
.map(function (e) { return { id: e.id, name: typeof e.name === 'string' && e.name ? e.name.slice(0, 24) : e.id }; });
if (clean.length) { effects = clean; buildChips(); }
}, function () { setTimeout(loadEffects, 3000); });
}
// ---------- drawing ----------
function layout() {
dpr = Math.min(window.devicePixelRatio || 1, 3);
sw = strip.clientWidth; sh = strip.clientHeight;
strip.width = Math.round(sw * dpr); strip.height = Math.round(sh * dpr);
var pad = 22, room = sw - pad * 2;
sn = Math.max(2, Math.min(S.leds, Math.floor(room / 5.4)));
px = []; py = []; floorY = sh * 0.71;
for (var i = 0; i < sn; i++) {
var u = sn > 1 ? i / (sn - 1) : 0.5;
px.push(pad + u * room); py.push(sh * 0.53 - sh * 0.13 * Math.sin(u * Math.PI));
}
paintWheel(); placeKnob(false); sizeChips();
}
var A = [0, 0, 0], B = [0, 0, 0], P = [0, 0, 0];
function pixel(i, n, t, c) {
fx(curFx)(i, n, t, c, A);
if (mix < 1) { fx(prevFx)(i, n, t, c, B); A[0] = B[0] + (A[0] - B[0]) * mix; A[1] = B[1] + (A[1] - B[1]) * mix; A[2] = B[2] + (A[2] - B[2]) * mix; }
return A;
}
function glow(g, x, y, R, rgb, a) {
var gr = g.createRadialGradient(x, y, 0, x, y, R);
gr.addColorStop(0, 'rgba(' + rgb + ',' + a.toFixed(3) + ')');
gr.addColorStop(0.3, 'rgba(' + rgb + ',' + (a * 0.42).toFixed(3) + ')');
gr.addColorStop(0.65, 'rgba(' + rgb + ',' + (a * 0.1).toFixed(3) + ')');
gr.addColorStop(1, 'rgba(' + rgb + ',0)');
g.fillStyle = gr; g.fillRect(x - R, y - R, R * 2, R * 2);
}
function drawStrip() {
var g = sx, i;
g.setTransform(dpr, 0, 0, dpr, 0, 0); g.clearRect(0, 0, sw, sh);
// the tape the LEDs sit on
g.lineCap = 'round'; g.lineJoin = 'round'; g.beginPath();
for (i = 0; i < sn; i++) { if (i) g.lineTo(px[i], py[i]); else g.moveTo(px[i], py[i]); }
g.lineWidth = 12; g.strokeStyle = 'rgba(255,255,255,.085)'; g.stroke();
g.lineWidth = 10; g.strokeStyle = 'rgba(9,8,18,.82)'; g.stroke();
// the table it is mirrored in
var line = g.createLinearGradient(0, 0, sw, 0);
line.addColorStop(0, 'rgba(255,255,255,0)'); line.addColorStop(0.5, 'rgba(255,255,255,.12)'); line.addColorStop(1, 'rgba(255,255,255,0)');
g.fillStyle = line; g.fillRect(0, floorY, sw, 1);
var power = D.lvl * (0.3 + 0.7 * Math.pow(D.bri, 0.7)), wide = 26 + 16 * D.bri, cores = [];
g.globalCompositeOperation = 'lighter';
for (i = 0; i < sn; i++) {
var c = pixel(i, sn, tEff, D.c), m = Math.max(c[0], c[1], c[2]);
if (m < 0.004 || power < 0.004) { cores.push(null); continue; }
var a = Math.pow(Math.min(1, m), 0.8) * power;
P[0] = c[0] / m; P[1] = c[1] / m; P[2] = c[2] / m;
var rgb = triple(P);
glow(g, px[i], py[i], wide, rgb, a * 0.3);
glow(g, px[i], py[i], 9, rgb, a * 0.85);
g.save(); g.translate(px[i], floorY + (floorY - py[i]) * 0.42 + 3); g.scale(1, 0.45);
glow(g, 0, 0, wide, rgb, a * 0.2);
glow(g, 0, 0, 7, rgb, a * 0.4);
g.restore();
cores.push([triple(lighten(P, 0.2 + 0.5 * a)), Math.min(1, 0.3 + a * 1.2)]);
}
g.globalCompositeOperation = 'source-over';
for (i = 0; i < sn; i++) {
g.beginPath(); g.arc(px[i], py[i], 2.4, 0, 6.2832);
g.fillStyle = cores[i] ? 'rgba(' + cores[i][0] + ',' + cores[i][1].toFixed(3) + ')' : 'rgba(255,255,255,.13)';
g.fill();
}
}
var CHIP_N = 9;
function drawChips() {
for (var k = 0; k < chips.length; k++) {
var chip = chips[k], g = chip.ctx, f = fx(chip.id);
if (!chip.w) continue;
g.setTransform(dpr, 0, 0, dpr, 0, 0); g.clearRect(0, 0, chip.w, chip.h);
g.globalCompositeOperation = 'lighter';
var gap = Math.min(7, (chip.w - 22) / (CHIP_N - 1)), x0 = (chip.w - gap * (CHIP_N - 1)) / 2, y = chip.h / 2;
for (var i = 0; i < CHIP_N; i++) {
f(i, CHIP_N, tChip + k * 1.7, D.c, A);
var m = Math.max(A[0], A[1], A[2]);
if (m < 0.01) continue;
P[0] = A[0] / m; P[1] = A[1] / m; P[2] = A[2] / m; m = Math.min(1, m);
var rgb = triple(P), x = x0 + i * gap;
glow(g, x, y, 8, rgb, m * 0.4);
g.beginPath(); g.arc(x, y, 2, 0, 6.2832); g.fillStyle = 'rgba(' + triple(lighten(P, 0.15 + 0.35 * m)) + ',' + Math.min(1, 0.2 + m).toFixed(3) + ')'; g.fill();
}
g.globalCompositeOperation = 'source-over';
}
}
var RAINBOW = [0, 0, 0];
function ambientTarget(t) {
if (S.effect === 'fire') return [1, 0.42, 0.1];
if (S.effect === 'sunrise') return [1, 0.6, 0.22];
if (S.effect === 'rainbow') return hsv(t * 14, 0.85, 1, RAINBOW);
return hsv(hue, sat, 1, RAINBOW);
}
function setAmbient(c) {
var h = toHsv(c[0], c[1], c[2]), o = [0, 0, 0];
setVar('--acc', triple(c));
setVar('--accl', triple(lighten(c, 0.62)));
setVar('--acc2', triple(hsv(h[0] + 42, Math.max(h[1], 0.35), 1, o)));
setVar('--acc3', triple(hsv(h[0] - 36, Math.max(h[1], 0.35), 1, o)));
}
var last = 0, ticks = 0, brir = $('brir'), spdr = $('spdr');
function fill(el, share) { var p = share.toFixed(3); if (el.czp !== p) { el.czp = p; el.style.setProperty('--p', p); } }
function frame(now) {
var dt = RM || !last ? 0 : Math.min(0.1, (now - last) / 1000), k = RM ? 1 : 1 - Math.exp(-dt * 7), slow = RM ? 1 : 1 - Math.exp(-dt * 3.2);
last = now; ticks++;
var target = hexRgb(S.color), amb = ambientTarget(now / 1000), i;
for (i = 0; i < 3; i++) { D.c[i] += (target[i] - D.c[i]) * k; D.amb[i] += (amb[i] - D.amb[i]) * slow; }
D.bri += (S.brightness / 255 - D.bri) * k;
D.lvl += ((S.on && known ? 1 : 0) - D.lvl) * slow;
if (mix < 1) mix = Math.min(1, mix + dt / 0.7);
var h = toHsv(D.c[0], D.c[1], D.c[2]); if (h[1] > 0.02) BH = h[0]; BS = h[1];
tEff += dt * rate(S.speed); tChip += dt;
if (!holding) {
D.briUi += (S.brightness - D.briUi) * k; D.spdUi += (S.speed - D.spdUi) * k;
var b = String(Math.round(D.briUi)), s = String(Math.round(D.spdUi));
if (bri.value !== b) bri.value = b;
if (spd.value !== s) spd.value = s;
}
fill(brir, D.briUi / 255); fill(spdr, D.spdUi / 100);
setAmbient(D.amb);
drawStrip();
if (RM || ticks % 3 === 0) drawChips();
if (!RM) requestAnimationFrame(frame);
}
// ---------- start ----------
buildChips();
layout();
show();
var resizeTimer = null;
window.addEventListener('resize', function () {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function () { layout(); if (RM) frame(0); }, 120);
});
if (RM) frame(0); else requestAnimationFrame(frame);
var host = window.connectivitizer;
if (host && host.ready && typeof host.ready.then === 'function') {
host.ready.then(function (info) {
if (!info) return;
device = info;
var title = String(info.title || info.name || '').slice(0, 40);
if (title) { $('title').textContent = title; document.title = title; }
show();
}, function () { /* opened in a browser: no host */ });
}
loadEffects();
poll();
setInterval(poll, POLL_MS);
})();
</script>
</body>
</html>