Files
ploxion-arbre-a-son/app.blade.php

250 lines
14 KiB
PHP

{{-- PL0XI0N arbre-a-son 🌳 — l'arbre à basses (RS-7) : un arbre stylisé (canvas, tronc + branches récursives)
dont les feuilles sont des cercles-haut-parleurs qui PULSENT, façon LedFx/cava.
IN ploxion.arbre-a-son.drive {bands:[...]} (câblé depuis ondes.spectrum) : chaque bande pilote l'amplitude
d'une feuille — les graves = les grosses feuilles du bas, les aigus = les petites feuilles de la cime.
IN ploxion.arbre-a-son.track {title} (câblé depuis mixer.now) : le titre en cours s'affiche au pied de
l'arbre — même son, chacun sa basse. Sans signal depuis ~1.6 s : respiration lente autonome.
Aucun port out. Payloads invalides + métadonnées routeur (__wired/__via) ignorés. Thème via --xr-*
(lisible clair chill ET sombre chill-tekk, couleurs canvas relues du thème), zéro lib, mobile-first. --}}
<style>
.arbson{position:relative;height:100%;display:flex;flex-direction:column;overflow:hidden;background:var(--xr-bg,#0d141c);color:var(--xr-text,#dfe9ef);font-family:var(--xr-font-ui,system-ui,-apple-system,sans-serif)}
.arbson *{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
.arbson__bar{flex:0 0 auto;display:flex;align-items:center;gap:8px;padding:10px 12px;border-bottom:1px solid var(--xr-border,rgba(128,148,164,.28))}
.arbson__bar b{font-size:1rem}
.arbson__sub{font-size:.66rem;color:var(--xr-muted,#8496a6);white-space:nowrap}
.arbson__pill{margin-left:auto;font-size:.66rem;padding:3px 9px;border-radius:999px;border:1px solid var(--xr-border,rgba(128,148,164,.4));color:var(--xr-muted,#8496a6);white-space:nowrap}
.arbson__pill.is-live{color:var(--xr-accent,#2fae74);border-color:var(--xr-accent,#2fae74);font-weight:700}
.arbson__scene{flex:1;min-height:0;position:relative}
.arbson__scene canvas{position:absolute;inset:0;width:100%;height:100%;display:block}
.arbson__foot{flex:0 0 auto;display:flex;align-items:center;gap:8px;padding:10px 14px;border-top:1px solid var(--xr-border,rgba(128,148,164,.28));min-width:0}
.arbson__note{flex:0 0 auto;font-size:1rem}
.arbson__title{flex:1;min-width:0;font-size:.86rem;font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.arbson__title--empty{color:var(--xr-muted,#8496a6);font-weight:400;font-style:italic}
</style>
<script>
(function () {
"use strict";
var ID = 'arbre-a-son';
// in drive : {bands:[...]} tolérant — accepte aussi {spectrum|levels|fft|values:[...]}, un array nu,
// une string JSON, ou la forme routée {value,__wired,__via} (métadonnées routeur jamais lues).
// Valeurs 0..1 ou 0..255 (FFT Uint8) → normalisées 0..1. Payload invalide → null (ignoré).
function bandsOf(p) {
if (p == null) return null;
if (typeof p === 'string') { try { p = JSON.parse(p); } catch (e) { return null; } }
if (p && typeof p === 'object' && !Array.isArray(p)
&& p.bands == null && p.spectrum == null && p.levels == null && p.fft == null && p.values == null
&& p.value != null) p = p.value;
if (typeof p === 'string') { try { p = JSON.parse(p); } catch (e2) { return null; } }
if (p && typeof p === 'object' && !Array.isArray(p)) p = p.bands || p.spectrum || p.levels || p.fft || p.values || null;
if (!Array.isArray(p) || !p.length) return null;
var out = [], mx = 0, i, v;
for (i = 0; i < p.length && i < 96; i++) { v = +p[i]; if (!isFinite(v) || v < 0) v = 0; out.push(v); if (v > mx) mx = v; }
if (mx > 2) { for (i = 0; i < out.length; i++) out[i] = out[i] / 255; }
for (i = 0; i < out.length; i++) { if (out[i] > 1) out[i] = 1; }
return out;
}
// in track : {title} tolérant — string nue, {title|name|now|label}, {track:{title,artist}},
// forme routée {value:...} ; ajoute « — artiste » si présent. Invalide → null.
function titleOf(p) {
if (p == null) return null;
if (typeof p === 'string') {
var s = p.trim(); if (!s) return null;
if (s.charAt(0) === '{' || s.charAt(0) === '[') { try { return titleOf(JSON.parse(s)); } catch (e) {} }
return s.slice(0, 140);
}
if (typeof p !== 'object') return null;
if (typeof p.value === 'string' || (p.value && typeof p.value === 'object')) { var w = titleOf(p.value); if (w) return w; }
var t = p.title || p.name || p.now || p.label || null;
if (t && typeof t === 'object') t = t.title || t.name || null;
if (!t && p.track) t = (typeof p.track === 'string') ? p.track : (p.track.title || p.track.name || null);
if (typeof t !== 'string' || !t.trim()) return null;
t = t.trim();
var art = p.artist || p.by || (p.track && typeof p.track === 'object' ? p.track.artist : null);
if (typeof art === 'string' && art.trim()) t = t + ' — ' + art.trim();
return t.slice(0, 140);
}
function nowMs() { try { return window.performance.now(); } catch (e) { return Date.now(); } }
var LIVE = null; // posé au mount : { title, driven }
function describe() {
var ti = LIVE ? LIVE.title() : null;
var driven = LIVE ? LIVE.driven() : false;
return { v: 1, id: ID, title: 'Arbre à son', icon: '🌳', items: [
{ p: 1, kind: 'stat', label: driven ? '♪ en écoute' : 'silence', value: ti || '—' }
] };
}
function mount(container, ctx) {
container.innerHTML =
'<div class="arbson">'
+ ' <div class="arbson__bar"><span style="font-size:1.15rem">🌳</span><b>Arbre à son</b><span class="arbson__sub" data-n></span><span class="arbson__pill" data-pill>◌ respiration</span></div>'
+ ' <div class="arbson__scene" data-scene><canvas></canvas></div>'
+ ' <div class="arbson__foot"><span class="arbson__note">🎶</span><span class="arbson__title arbson__title--empty" data-track>en attente du bon son…</span></div>'
+ '</div>';
var root = container.querySelector('.arbson');
var scene = root.querySelector('[data-scene]');
var cv = root.querySelector('canvas');
var g = cv.getContext('2d');
var pill = root.querySelector('[data-pill]');
var elN = root.querySelector('[data-n]');
var elTitle = root.querySelector('[data-track]');
var alive = true, raf = 0, frame = 0, lastMode = '';
var W = 0, H = 0, segs = [], leaves = [], ground = { y: 0, span: 0 };
var bands = null, lastDrive = 0;
var cols = { accent: '#2fae74', trunk: '#8a93a0', border: '#8a93a0', leaf: '#4aa066' };
function readTheme() {
try {
var cs = window.getComputedStyle(root);
var v = function (n, fb) { var s = cs.getPropertyValue(n); s = s ? s.trim() : ''; return s || fb; };
cols.accent = v('--xr-accent', '#2fae74');
cols.trunk = v('--xr-muted', '#8a93a0');
cols.border = v('--xr-border', '#8a93a0');
} catch (e) {}
}
// arbre déterministe (LCG seedé) : mêmes branches à chaque rebuild, juste rescalées.
function buildTree() {
segs = []; leaves = [];
if (W < 60 || H < 60) { ground.span = 0; return; }
var seed = 1337;
var rnd = function () { seed = (seed * 1664525 + 1013904223) >>> 0; return seed / 4294967296; };
var groundY = H - Math.max(22, H * 0.09);
var span = Math.min(W * 0.48, H * 0.44);
var branch = function (x, y, ang, len, wd, depth) {
var x2 = x + Math.cos(ang) * len, y2 = y + Math.sin(ang) * len;
segs.push({ x1: x, y1: y, x2: x2, y2: y2, w: wd });
if (depth <= 0) { leaves.push({ x: x2, y: y2, a: 0.12, ph: rnd() * 6.283 }); return; }
var nb = (depth === 1 || rnd() < 0.65) ? 2 : 3, i;
for (i = 0; i < nb; i++) {
var na = ang + (i - (nb - 1) / 2) * (0.5 + rnd() * 0.3) + (rnd() - 0.5) * 0.22;
branch(x2, y2, na, len * (0.66 + rnd() * 0.12), Math.max(1.3, wd * 0.6), depth - 1);
}
};
branch(W / 2, groundY, -Math.PI / 2 + (rnd() - 0.5) * 0.06, span * 0.42, Math.max(5, span * 0.05), 4);
// rang 0 = la feuille la plus BASSE = la plus grosse = les graves ; la cime = les aigus.
leaves.sort(function (a, b) { return b.y - a.y; });
var n = leaves.length, rMax = Math.max(9, span * 0.085), rMin = Math.max(4, span * 0.03), i, t;
for (i = 0; i < n; i++) { t = n > 1 ? i / (n - 1) : 0; leaves[i].r = rMax + (rMin - rMax) * t; leaves[i].k = t; }
ground.y = groundY; ground.span = span;
if (elN) elN.textContent = '· ' + n + ' basses-feuilles';
}
function resize() {
var r = scene.getBoundingClientRect();
W = Math.max(1, Math.round(r.width)); H = Math.max(1, Math.round(r.height));
var dpr = Math.min(2, window.devicePixelRatio || 1);
cv.width = Math.round(W * dpr); cv.height = Math.round(H * dpr);
g.setTransform(dpr, 0, 0, dpr, 0, 0);
buildTree();
}
function circle(x, y, r) { g.beginPath(); g.arc(x, y, Math.max(0.5, r), 0, 6.2832); }
function draw() {
frame++;
if (frame % 60 === 1) readTheme();
g.clearRect(0, 0, W, H);
if (!ground.span) return;
var nw = nowMs(), t = nw / 1000;
var driven = !!(bands && bands.length && (nw - lastDrive) < 1600);
var mode = driven ? 'live' : 'respiration';
if (mode !== lastMode) {
lastMode = mode;
pill.textContent = driven ? '● live' : '◌ respiration';
pill.classList.toggle('is-live', driven);
}
// sol
g.globalAlpha = 0.22; g.fillStyle = cols.border;
g.beginPath(); g.ellipse(W / 2, ground.y + 3, ground.span * 0.55, Math.max(4, ground.span * 0.04), 0, 0, 6.2832); g.fill();
// tronc + branches
g.globalAlpha = 0.9; g.strokeStyle = cols.trunk; g.lineCap = 'round';
var i, s;
for (i = 0; i < segs.length; i++) {
s = segs[i]; g.lineWidth = s.w;
g.beginPath(); g.moveTo(s.x1, s.y1); g.lineTo(s.x2, s.y2); g.stroke();
}
// basses-feuilles : cercle = feuille, anneau + cône central = la basse qui pulse
var n = leaves.length, lv, target, j, R, y;
for (i = 0; i < n; i++) {
lv = leaves[i];
if (driven) { j = Math.min(bands.length - 1, Math.floor(i * bands.length / n)); target = Math.pow(bands[j], 0.7); }
else { target = 0.1 + 0.13 * (0.5 + 0.5 * Math.sin(t * 0.55 + lv.ph)); }
lv.a += (target - lv.a) * (target > lv.a ? 0.45 : 0.1);
R = lv.r * (0.75 + 0.85 * lv.a);
y = lv.y + Math.sin(t * 0.8 + lv.ph) * 1.6;
if (lv.a > 0.5 && lv.k < 0.35) { g.globalAlpha = (lv.a - 0.5) * 0.5; g.fillStyle = cols.accent; circle(lv.x, y, R * 1.9); g.fill(); }
g.globalAlpha = 0.28 + 0.5 * lv.a; g.fillStyle = cols.leaf; circle(lv.x, y, R); g.fill();
g.globalAlpha = 0.75; g.strokeStyle = cols.accent; g.lineWidth = 1.5; circle(lv.x, y, R); g.stroke();
g.globalAlpha = 0.35 + 0.65 * lv.a; g.fillStyle = cols.accent; circle(lv.x, y, R * (0.3 + 0.28 * lv.a)); g.fill();
}
g.globalAlpha = 1;
}
function loop() { if (!alive) return; draw(); raf = window.requestAnimationFrame(loop); }
var curTitle = null;
function setTitle(ti, persist) {
curTitle = ti;
elTitle.textContent = ti; // textContent = injection-safe (équivalent esc(), aucun HTML interprété)
elTitle.classList.remove('arbson__title--empty');
if (!persist) return;
try {
if (window.jOSStore) { var st = window.jOSStore(ID); if (st && st.upsert) st.upsert(function (x) { return x && x.key === 'track'; }, { key: 'track', title: ti, at: Date.now() }); }
else window.localStorage.setItem('arbson.track', ti);
} catch (e) {}
}
// dernier titre connu (store partagé si dispo, sinon localStorage)
try {
var saved = null;
if (window.jOSStore) { var st0 = window.jOSStore(ID); if (st0 && st0.find) { var row = st0.find(function (x) { return x && x.key === 'track'; }); if (row && typeof row.title === 'string' && row.title) saved = row.title; } }
if (saved == null) { var ls = window.localStorage.getItem('arbson.track'); if (ls) saved = ls; }
if (saved) setTitle(saved, false);
} catch (e) {}
// bus — les 2 ins du contrat (aucun out)
var offs = [];
if (window.jOSPorts && window.jOSPorts.on) {
try {
// in drive ← ondes.spectrum : chaque bande pilote une feuille (graves = grosses feuilles du bas)
offs.push(window.jOSPorts.on('ploxion.arbre-a-son.drive', function (p) {
var b = bandsOf(p); if (!b) return;
bands = b; lastDrive = nowMs();
}));
// in track ← mixer.now : le son master est spatialisé sur l'arbre, son titre s'affiche au pied
offs.push(window.jOSPorts.on('ploxion.arbre-a-son.track', function (p) {
var ti = titleOf(p); if (!ti) return;
setTitle(ti, true);
}));
} catch (e) {}
}
resize(); readTheme();
var ro = null, onWin = null;
if (window.ResizeObserver) { ro = new ResizeObserver(function () { resize(); }); ro.observe(scene); }
else { onWin = function () { resize(); }; window.addEventListener('resize', onWin); }
raf = window.requestAnimationFrame(loop);
LIVE = { title: function () { return curTitle; }, driven: function () { return !!(bands && bands.length && (nowMs() - lastDrive) < 1600); } };
return { unmount: function () {
LIVE = null;
alive = false;
try { if (raf) window.cancelAnimationFrame(raf); } catch (e) {}
for (var i = 0; i < offs.length; i++) { try { offs[i](); } catch (e) {} }
try { if (ro) ro.disconnect(); } catch (e) {}
try { if (onWin) window.removeEventListener('resize', onWin); } catch (e) {}
container.innerHTML = '';
} };
}
window.jOSApps = window.jOSApps || {};
window.jOSApps[ID] = { manifest: { id: ID, name: 'Arbre à son', icon: '🌳' }, mount: mount, describe: describe };
})();
</script>