// Un plateau qu'on glisse et qu'on zoome (souris, molette, un ou deux doigts) : la planche de l'eau et les murs de // documents. Le contenu (`el`) mesure W × H unités ; la vue (`vp`) le montre à l'échelle s, décalé de (x, y). export function creerPlateau(vp, el, { W, H, reduit, max = 2.5, onBouge = () => {}, onToucher = null, onGeste = () => {} }) { const C = { s: 1, x: 0, y: 0, vol: null, W, H }; const applique = () => { el.style.transform = `translate(${C.x}px, ${C.y}px) scale(${C.s})`; onBouge(C); }; const ajuste = () => { const r = vp.getBoundingClientRect(); return Math.min(r.width / C.W, r.height / C.H); }; const mini = () => ajuste() * 0.9; // la table peut déborder d'un cinquième de la vue : une pièce posée au bord se centre quand on s'en approche (09/10) function borne(o, r) { const w = C.W * o.s, h = C.H * o.s, mx = r.width * 0.2, my = r.height * 0.2; o.x = w >= r.width ? Math.min(mx, Math.max(r.width - w - mx, o.x)) : (r.width - w) / 2; o.y = h >= r.height ? Math.min(my, Math.max(r.height - h - my, o.y)) : (r.height - h) / 2; return o; } function viser(x, y, s, duree = 900) { const r = vp.getBoundingClientRect(), fin = borne({ s, x: r.width / 2 - x * s, y: r.height / 2 - y * s }, r); cancelAnimationFrame(C.vol); if (reduit || !duree) { Object.assign(C, fin); applique(); return; } const debut = { s: C.s, x: C.x, y: C.y }, t0 = performance.now(); const pas = (now) => { const k = Math.min(1, (now - t0) / duree), e = k < 0.5 ? 2 * k * k : 1 - Math.pow(-2 * k + 2, 2) / 2; C.s = debut.s + (fin.s - debut.s) * e; C.x = debut.x + (fin.x - debut.x) * e; C.y = debut.y + (fin.y - debut.y) * e; applique(); if (k < 1) C.vol = requestAnimationFrame(pas); }; C.vol = requestAnimationFrame(pas); } /** Cadre le rectangle (unités du plateau) dans la vue, avec une marge. */ function cadrer(x0, y0, x1, y1, duree, marge = 0.06) { const r = vp.getBoundingClientRect(), w = x1 - x0, h = y1 - y0; const s = Math.min(max, Math.min(r.width / (w * (1 + 2 * marge)), r.height / (h * (1 + 2 * marge)))); viser((x0 + x1) / 2, (y0 + y1) / 2, Math.max(s, mini()), duree); } function taille(w, h) { C.W = w; C.H = h; el.style.width = w + 'px'; el.style.height = h + 'px'; } const zoomA = (px, py, ns) => { ns = Math.max(mini(), Math.min(max, ns)); const k = ns / C.s; C.x = px - (px - C.x) * k; C.y = py - (py - C.y) * k; C.s = ns; }; const pp = new Map(); let parcouru = 0, depart = null; vp.addEventListener('pointerdown', (e) => { if (e.target.closest('button, a, .pe')) return; vp.setPointerCapture(e.pointerId); pp.set(e.pointerId, [e.clientX, e.clientY]); cancelAnimationFrame(C.vol); onGeste(); if (pp.size === 1) { parcouru = 0; depart = e; } }); vp.addEventListener('pointermove', (e) => { if (!pp.has(e.pointerId)) return; const av = [...pp.values()].map((p) => [...p]); pp.set(e.pointerId, [e.clientX, e.clientY]); const ap = [...pp.values()]; const c = (L) => [L.reduce((s, p) => s + p[0], 0) / L.length, L.reduce((s, p) => s + p[1], 0) / L.length], a = c(av), b = c(ap); parcouru += Math.hypot(b[0] - a[0], b[1] - a[1]); C.x += b[0] - a[0]; C.y += b[1] - a[1]; if (av.length === 2 && ap.length === 2) { const r = vp.getBoundingClientRect(), dA = Math.hypot(av[0][0] - av[1][0], av[0][1] - av[1][1]), dB = Math.hypot(ap[0][0] - ap[1][0], ap[0][1] - ap[1][1]); zoomA(b[0] - r.left, b[1] - r.top, C.s * (dB / dA)); parcouru += 99; } applique(); }); const fin = (e) => { if (!pp.has(e.pointerId)) return; pp.delete(e.pointerId); if (!pp.size && depart && parcouru < 6 && onToucher) { // un toucher sans glisser : on dit où, en unités du plateau const r = vp.getBoundingClientRect(); onToucher((e.clientX - r.left - C.x) / C.s, (e.clientY - r.top - C.y) / C.s, e); } if (!pp.size) depart = null; }; vp.addEventListener('pointerup', fin); vp.addEventListener('pointercancel', (e) => { parcouru = 99; fin(e); }); vp.addEventListener('wheel', (e) => { e.preventDefault(); onGeste(); const r = vp.getBoundingClientRect(); zoomA(e.clientX - r.left, e.clientY - r.top, C.s * (e.deltaY > 0 ? 0.9 : 1.11)); applique(); }, { passive: false }); return { C, applique, ajuste, viser, cadrer, taille, toutVoir: (d = 0) => cadrer(0, 0, C.W, C.H, d, 0.02) }; }