Files
quartier/scripts/banc-bords.mjs
T
Jules NényandClaude Opus 5.5 c580c6f84e Lot Q bords-canards, étape A : banc des bords (scripts/banc-bords.mjs), canards.ts (même parcours), test des canards rouge
Le test tests/canards.test.ts est rouge à dessein : 2 963 poses sur 3 144 hors de l'eau
(marge 0,4 m), le centre du parcours est sur la rive. Le parcours est sorti d'animaux.js
dans maquette/canards.ts sans changer de formule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:44:06 +01:00

213 lines
15 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Banc du LOT Q-bords-canards (16/11) : « on ne voit plus le bord de la maquette, à aucun zoom ni aucune rotation ».
// node scripts/banc-bords.mjs <base-url> <label> <outdir> [quoi] quoi : captures | balayage | vol | fond | tout
//
// Le bord se juge par l'état de la vue, sans la logique de la scène : les quatre coins du socle sont projetés par la caméra
// (`__quartier.ecran`, sous `?mesure`) ; la zone vue (le canvas moins un bandeau opaque qui couvre toute la largeur, mesuré
// dans le DOM) doit tomber dedans. Une pose où un coin de la zone vue sort du socle = « bord visible ».
import { createRequire } from 'node:module';
import { writeFileSync, mkdirSync } from 'node:fs';
const require = createRequire(process.env.PW_CORE || '/home/julesneny8/.npm/_npx/9833c18b2d85bc59/node_modules/');
const { chromium } = require('playwright-core');
const [base, label, out, quoi = 'tout'] = process.argv.slice(2);
mkdirSync(out, { recursive: true });
const EXE = process.env.CHROMIUM || '/home/julesneny8/.cache/ms-playwright/chromium-1254/chrome-linux64/chrome';
const extra = process.env.EXTRA || '';
const browser = await chromium.launch({ executablePath: EXE, args: ['--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--ignore-gpu-blocklist'] });
const erreurs = [];
const pause = (p, ms) => p.waitForTimeout(ms);
async function ouvrir(w, h, { dpr = 1, hash = '', reduit = true, q = 'mesure' } = {}) {
const ctx = await browser.newContext({ viewport: { width: w, height: h }, deviceScaleFactor: dpr, hasTouch: true, reducedMotion: reduit ? 'reduce' : 'no-preference' });
const page = await ctx.newPage();
// horloge figée sous mouvement réduit : les canards et le vent au même instant, avant comme après
if (reduit) await page.addInitScript(() => { const T = 5000; performance.now = () => T; const r = window.requestAnimationFrame.bind(window); window.requestAnimationFrame = (f) => r(() => f(T)); });
page.on('console', (m) => { if (m.type() === 'error') erreurs.push(`${w}×${h} ${hash}: ${m.text()}`); });
page.on('pageerror', (e) => erreurs.push(`${w}×${h} ${hash}: ${e.message}`));
await page.goto(`${base}/?${q}${extra}${hash ? '#' + hash : ''}`, { waitUntil: 'networkidle' });
await pause(page, 3500);
const cdp = await ctx.newCDPSession(page);
return { ctx, page, cdp };
}
// ---------------------------------------------------------------- l'état : vue, socle à l'écran, zone vue, bord
async function etat(page) {
return page.evaluate(() => {
const Q = window.__quartier, v = Q.vue(), S = Q.socle, W = innerWidth, H = innerHeight;
const f = document.getElementById('fiches'), r = f && !f.hidden && f.offsetWidth ? f.getBoundingClientRect() : null;
const opaque = (e) => { const c = getComputedStyle(e).backgroundColor.match(/[\d.]+/g); return c && (c.length < 4 || +c[3] >= 0.999) && +getComputedStyle(e).opacity >= 0.999; };
const bas = r && r.left <= 1 && r.right >= W - 1 && r.bottom >= H - 1 && opaque(f) ? Math.max(0, H - r.top) : 0;
const quad = [[-S.Wm / 2, -S.Hm / 2], [S.Wm / 2, -S.Hm / 2], [S.Wm / 2, S.Hm / 2], [-S.Wm / 2, S.Hm / 2]].map(([x, z]) => { const e = Q.ecran(x, 0, z); return [e.x, e.y]; });
// distance signée (px) d'un point de l'écran au contour du socle projeté (positive dedans)
// le sens du quadrilatère à l'écran dépend de l'azimut : on l'oriente par son aire signée
let A = 0; for (let i = 0; i < 4; i++) { const a = quad[i], b = quad[(i + 1) % 4]; A += a[0] * b[1] - b[0] * a[1]; }
const sgn = A >= 0 ? -1 : 1;
const sd = ([x, y]) => { let m = Infinity; for (let i = 0; i < 4; i++) { const a = quad[i], b = quad[(i + 1) % 4], ex = b[0] - a[0], ey = b[1] - a[1], L = Math.hypot(ex, ey); m = Math.min(m, (sgn * ((x - a[0]) * ey - (y - a[1]) * ex)) / L); } return m; };
const c0 = Q.ecran(0, 0, 0); if (sd([c0.x, c0.y]) <= 0) throw new Error('banc : le centre du socle tombe hors du socle projeté');
const coins = [[0, 0], [W, 0], [W, H - bas], [0, H - bas]], d = coins.map((p) => sd(p));
const carte = window.__carte ? window.__carte.surCarte() : false;
return { v, W, H, bas, d: d.map((x) => +x.toFixed(1)), mini: +Math.min(...d).toFixed(1), bord: Math.min(...d) < -0.5, carte, cadrage: Q.cadrage() };
});
}
// ---------------------------------------------------------------- les gestes
async function molette(page, x, y, n, sens = 1) { await page.mouse.move(x, y); for (let i = 0; i < n; i++) { await page.mouse.wheel(0, 100 * sens); await pause(page, 30); } await pause(page, 80); }
/** Dézoom maximal à la molette : jusqu'à ce que la largeur ne bouge plus, puis un cran avant et un arrière (le compteur des trois crans vers la carte retombe à zéro). */
async function dezoomMolette(page) {
const { W, H } = await page.evaluate(() => ({ W: innerWidth, H: innerHeight }));
let w0 = -1;
for (let k = 0; k < 40; k++) { const w = (await etat(page)).v.w; if (Math.abs(w - w0) < 0.01) break; w0 = w; await molette(page, W * 0.65, H * 0.4, 1); }
await molette(page, W * 0.65, H * 0.4, 1, -1); await molette(page, W * 0.65, H * 0.4, 1, 1);
}
const touche = (x, y, id) => ({ x, y, id, radiusX: 4, radiusY: 4, force: 1 });
async function doigts(cdp, type, pts) { await cdp.send('Input.dispatchTouchEvent', { type, touchPoints: pts.map(([x, y], i) => touche(x, y, i)) }); }
/** Deux doigts autour de (cx, cy), à `r0` → `r1` px l'un de l'autre, tournant de `da` (rad) en `n` pas. */
async function pincer(page, cdp, cx, cy, r0, r1, da, n = 12, a0 = 0) {
const P = (r, a) => [[cx - Math.cos(a) * r, cy - Math.sin(a) * r], [cx + Math.cos(a) * r, cy + Math.sin(a) * r]];
await doigts(cdp, 'touchStart', P(r0, a0)); await pause(page, 20);
for (let k = 1; k <= n; k++) { await doigts(cdp, 'touchMove', P(r0 + ((r1 - r0) * k) / n, a0 + (da * k) / n)); await pause(page, 16); }
await doigts(cdp, 'touchEnd', []); await pause(page, 120);
}
async function dezoomPince(page, cdp) {
const { W, H } = await page.evaluate(() => ({ W: innerWidth, H: innerHeight }));
let w0 = -1;
for (let k = 0; k < 30; k++) { const w = (await etat(page)).v.w; if (Math.abs(w - w0) < 0.01) break; w0 = w; await pincer(page, cdp, W * 0.55, H * 0.4, Math.min(W, H) * 0.3, Math.min(W, H) * 0.12, 0, 8); }
}
async function glisserSouris(page, [x0, y0], [x1, y1]) { await page.mouse.move(x0, y0); await page.mouse.down(); for (let k = 1; k <= 12; k++) { await page.mouse.move(x0 + ((x1 - x0) * k) / 12, y0 + ((y1 - y0) * k) / 12); await pause(page, 12); } await page.mouse.up(); await pause(page, 100); }
async function glisserDoigt(page, cdp, [x0, y0], [x1, y1]) {
await doigts(cdp, 'touchStart', [[x0, y0]]);
for (let k = 1; k <= 12; k++) { await doigts(cdp, 'touchMove', [[x0 + ((x1 - x0) * k) / 12, y0 + ((y1 - y0) * k) / 12]]); await pause(page, 12); }
await doigts(cdp, 'touchEnd', []); await pause(page, 100);
}
/** Glisser vers un coin du socle (nord-est = haut-droite à l'azimut 0) : on tire la maquette du coin vers le coin opposé, quatre fois. */
async function versCoin(page, cdp, coin, doigt) {
const { W, H } = await page.evaluate(() => ({ W: innerWidth, H: innerHeight }));
const X = [W * 0.42, W * 0.94], Y = [H * 0.16, H * 0.6];
const [cx, cy] = { ne: [1, 0], no: [0, 0], se: [1, 1], so: [0, 1] }[coin];
const de = [X[cx], Y[cy]], vers = [X[1 - cx], Y[1 - cy]];
for (let k = 0; k < 4; k++) doigt ? await glisserDoigt(page, cdp, de, vers) : await glisserSouris(page, de, vers);
}
const res = { label, erreurs, poses: [] };
const noter = async (page, ecran, quoi) => { const e = await etat(page); res.poses.push({ ecran, quoi, ...e }); return e; };
// ---------------------------------------------------------------- sonde : l'arrivée à deux écrans (contrôle rapide du banc)
if (quoi === 'sonde') {
for (const [W, H, dpr] of [[1280, 720, 1], [390, 844, 2]]) { const { ctx, page } = await ouvrir(W, H, { dpr }); await noter(page, `${W}×${H}`, 'arrivée'); await ctx.close(); }
}
// ---------------------------------------------------------------- captures (paires avant / après)
if (quoi === 'captures' || quoi === 'tout') {
const cap = async (page, nom) => { await pause(page, 150); await page.screenshot({ path: `${out}/${nom}-${label}.png` }); await noter(page, nom, 'capture'); };
{
const { ctx, page, cdp } = await ouvrir(1280, 720);
await cap(page, '1280-arrivee');
await dezoomMolette(page); await cap(page, '1280-dezoom-max');
await pincer(page, cdp, 860, 300, 160, 160, -Math.PI / 4, 16); await dezoomMolette(page); await cap(page, '1280-dezoom-max-rotation-45');
await ctx.close();
}
{
const { ctx, page, cdp } = await ouvrir(390, 844, { dpr: 2 });
await cap(page, '390-arrivee');
await dezoomPince(page, cdp); await cap(page, '390-dezoom-max');
await ctx.close();
}
{
const { ctx, page, cdp } = await ouvrir(390, 844, { dpr: 2 });
await versCoin(page, cdp, 'ne', true); await cap(page, '390-coin-nord-est');
await ctx.close();
}
{
// la mare de près : zoom à la molette sur son milieu, jusqu'à ≈ 22 m de large, puis la mare tirée au milieu de l'écran
const { ctx, page } = await ouvrir(1280, 720);
const M = [-22, -21];
for (let k = 0; k < 30; k++) { const e = await etat(page); if (e.v.w < 24) break; const p = await page.evaluate(([x, z]) => window.__quartier.ecran(x, 0, z), M); await molette(page, p.x, p.y, 1, -1); }
const p = await page.evaluate(([x, z]) => window.__quartier.ecran(x, 0, z), M);
await glisserSouris(page, [p.x, p.y], [860, 330]);
await cap(page, '1280-mare-gros-plan');
await ctx.close();
}
}
// ---------------------------------------------------------------- balayage : trois écrans, molette et pincement, huit azimuts, quatre coins
if (quoi === 'balayage' || quoi === 'tout') {
for (const [W, H, dpr] of [[390, 844, 2], [1280, 720, 1], [1920, 1080, 1]]) {
const ec = `${W}×${H}`;
let { ctx, page, cdp } = await ouvrir(W, H, { dpr });
await noter(page, ec, 'arrivée');
await dezoomMolette(page); await noter(page, ec, 'dézoom molette');
await molette(page, W * 0.6, H * 0.4, 6, -1); await dezoomPince(page, cdp); await noter(page, ec, 'dézoom pincement');
// huit azimuts : un quart de quart de tour au pincement, puis le dézoom maximal à cet azimut
for (let k = 1; k <= 8; k++) {
await pincer(page, cdp, W * 0.6, H * 0.38, Math.min(W, H) * 0.22, Math.min(W, H) * 0.22, -Math.PI / 4, 12);
const e = await noter(page, ec, `rotation ${k * 45}°`);
await dezoomMolette(page); await noter(page, ec, `rotation ${k * 45}° + dézoom`);
if (k === 3) { await molette(page, W * 0.6, H * 0.4, 4, -1); for (const c of ['ne', 'so']) { await versCoin(page, cdp, c, W < 600); await noter(page, ec, `rotation ${k * 45}°, coin ${c}`); } }
void e;
}
await ctx.close();
// les quatre coins, à la souris puis au doigt, depuis l'arrivée, et après un dézoom maximal
for (const doigt of [false, true]) {
({ ctx, page, cdp } = await ouvrir(W, H, { dpr }));
for (const c of ['ne', 'no', 'so', 'se']) { await versCoin(page, cdp, c, doigt); await noter(page, ec, `coin ${c} ${doigt ? 'doigt' : 'souris'}`); }
await dezoomMolette(page);
for (const c of ['ne', 'so']) { await versCoin(page, cdp, c, doigt); await noter(page, ec, `dézoom, coin ${c} ${doigt ? 'doigt' : 'souris'}`); }
await ctx.close();
}
// une fiche près d'un bord (la forêt, à l'est ; les sentiers, au sud-est ; les routes)
for (const hash of ['vivant-foret', 'vivant-sentier', 'transport-voiture', 'energie-quartier']) {
({ ctx, page, cdp } = await ouvrir(W, H, { dpr, hash }));
await noter(page, ec, `fiche ${hash}`);
await dezoomMolette(page); await noter(page, ec, `fiche ${hash} + dézoom`);
await ctx.close();
}
// un redimensionnement, après un dézoom maximal (la fenêtre s'agrandit : la vue doit se resserrer)
({ ctx, page, cdp } = await ouvrir(W, H, { dpr }));
await dezoomMolette(page);
await page.setViewportSize({ width: Math.round(W * 1.15), height: Math.round(H * 1.1) }); await pause(page, 600); await noter(page, ec, 'redimensionnement (plus grand)');
await page.setViewportSize({ width: H, height: W }); await pause(page, 800); await noter(page, ec, 'redimensionnement (pivoté)');
await page.setViewportSize({ width: W, height: H }); await pause(page, 800); await noter(page, ec, 'redimensionnement (retour)');
// la carte XL : trois crans de plus au-delà du dézoom maximal l'ouvrent ; on entre de nouveau dans le quartier
await dezoomMolette(page);
await molette(page, W * 0.6, H * 0.4, 3, 1); await pause(page, 1500);
const surCarte = await page.evaluate(() => (window.__carte ? window.__carte.surCarte() : null));
res.poses.push({ ecran: ec, quoi: 'carte XL ouverte en 3 crans', carte: surCarte, bord: false, info: true });
if (surCarte) {
const q = await page.$('.quartier-c'); if (q) { const b = await q.boundingBox(); await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2); }
await pause(page, 2500);
const e = await noter(page, ec, 'retour de la carte');
res.poses.at(-1).carteApres = e.carte;
}
await ctx.close();
}
}
// ---------------------------------------------------------------- vol : sans mouvement réduit, la vue échantillonnée pendant les vols programmés
if (quoi === 'vol' || quoi === 'tout') {
for (const [W, H, dpr] of [[390, 844, 2], [1280, 720, 1]]) {
const ec = `${W}×${H} vol`;
const { ctx, page } = await ouvrir(W, H, { dpr, reduit: false });
await pause(page, 3500); // l'arrivée par la carte, puis la plongée dans l'îlot
const echant = async (quoi, ms) => { const t0 = Date.now(); while (Date.now() - t0 < ms) { const e = await etat(page); if (!e.carte) res.poses.push({ ecran: ec, quoi, ...e }); await pause(page, 90); } };
await echant('arrivée', 800);
for (let k = 0; k < 8; k++) { const b = await page.$('#b-suiv'); if (!b || !(await b.isVisible())) break; await b.click(); await echant(`fiche suivante ${k + 1}`, 1900); }
const t = await page.$('#b-tourner'); if (t && (await t.isVisible())) { await t.click(); await echant('tourner', 1200); }
await ctx.close();
}
}
// ---------------------------------------------------------------- mode fond (le parent pose la scène en fond : rien ne doit changer)
if (quoi === 'fond' || quoi === 'tout') {
for (const [w, h, dpr] of [[1280, 800, 1], [390, 844, 3]]) {
const { ctx, page } = await ouvrir(w, h, { dpr, q: 'mode=fond' });
await page.screenshot({ path: `${out}/fond-${w}-${label}.png` }); await ctx.close();
}
}
await browser.close();
const poses = res.poses.filter((p) => !p.info && !p.carte), bords = poses.filter((p) => p.bord);
res.marqueur = `bord visible : ${bords.length} sur ${poses.length} poses`;
writeFileSync(`${out}/banc-bords-${label}-${quoi}.json`, JSON.stringify(res, null, 1));
console.log(res.marqueur, '·', `${erreurs.length} erreur(s) console`);
for (const p of (quoi === 'sonde' ? res.poses : bords).slice(0, 60)) console.log(` ${p.ecran} · ${p.quoi} · ${p.bord ? 'BORD' : 'ok'} · mini ${p.mini} px · w ${p.v.w.toFixed(1)} az ${(p.v.az * 180 / Math.PI).toFixed(0)}°`);
for (const p of res.poses.filter((p) => p.info)) console.log(` ${p.ecran} · ${p.quoi} : ${p.carte}`);