The 669 // comments sitting right above a function, method or hook become /** */ blocks, unchanged in wording; eslint directives stay right above the line they target. website/AGENTS.md now asks for JSDoc on function comments and // inside function bodies. ExpeditionPlanner.jsx grew past 300 lines and gets its full header, as the header test requires. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
198 lines
8.4 KiB
JavaScript
198 lines
8.4 KiB
JavaScript
/**
|
|
* @file Moteur des scintillements holographiques d'une carte mythique (Canvas 2D) : des motifs lumineux
|
|
* (étoile, reflet, losange, cœur, flocon, bulle…) apparaissent en grossissant, vivent un instant avec
|
|
* un éclat en croix à leur apogée, puis disparaissent en rétrécissant. Nombre moyen à l'écran, vitesse
|
|
* et intensité réglables ; couleurs de la palette ; temps réel ; aucune boucle au repos. Même interface
|
|
* que le moteur des éclairs.
|
|
*/
|
|
|
|
import { canvasBacking, motifAngle, motifSprite, SPRITE_SIZE, streakSprite } from "./flareSprites.js";
|
|
|
|
const TAU = Math.PI * 2;
|
|
|
|
// Les motifs proposés (identifiants et libellés) sont déclarés par le catalogue (`flareEffects`).
|
|
|
|
// Paliers 1 à 5 : nombre moyen de motifs visibles (pour une illustration de carte `lg`), durée de vie d'un motif, intensité
|
|
// (taille et éclat).
|
|
export const TWINKLE_COUNTS = [2, 3, 4, 6, 9];
|
|
const LIFETIME_MS = [3400, 2600, 1900, 1350, 950];
|
|
const INTENSITY_SCALE = [0.55, 0.65, 0.8, 1, 1.25];
|
|
// Surface de référence : l'illustration d'une carte `lg`. Une carte plus petite montre moins de motifs, une zone « toute la
|
|
// carte » un peu plus.
|
|
const REFERENCE_AREA = 280 * 240;
|
|
// Quand la carte n'est plus survolée, les motifs encore en vie s'effacent ensemble en une demi-seconde au lieu de finir leur
|
|
// vie : l'animation s'arrête vite au lieu de tourner plusieurs secondes pour rien.
|
|
const OUTRO_MS = 500;
|
|
|
|
const easeOutBack = (t) => 1 + 2.4 * (t - 1) ** 3 + 1.4 * (t - 1) ** 2;
|
|
|
|
/** Phase d'un motif selon son âge relatif (0 à 1) : apparition (35 %) avec un léger dépassement, apogée (20 %), disparition. */
|
|
export function twinkleState(t) {
|
|
if (t < 0.35) {
|
|
const p = t / 0.35;
|
|
return { scale: Math.max(0, easeOutBack(p)), alpha: Math.min(1, p * 1.6), peak: 0 };
|
|
}
|
|
if (t < 0.55) {
|
|
const p = (t - 0.35) / 0.2;
|
|
return { scale: 1, alpha: 1, peak: Math.sin(p * Math.PI) };
|
|
}
|
|
const p = Math.min(1, (t - 0.55) / 0.45);
|
|
return { scale: 1 - p * 0.75, alpha: 1 - p, peak: 0 };
|
|
}
|
|
|
|
export function createTwinkle(canvas, options) {
|
|
const context = canvas.getContext("2d");
|
|
const backing = canvasBacking(canvas);
|
|
const random = options.random || Math.random;
|
|
let settings = { ...options };
|
|
let size = { width: 0, height: 0, ratio: 1 };
|
|
let sprites = [];
|
|
let streaks = [];
|
|
let items = [];
|
|
let pointer = null;
|
|
let active = false;
|
|
let frame = 0;
|
|
let nextSpawn = 0;
|
|
let leftAt = 0;
|
|
let fade = 1;
|
|
let destroyed = false;
|
|
|
|
const intensity = () => INTENSITY_SCALE[(settings.intensity || 3) - 1];
|
|
const lifetime = () => LIFETIME_MS[(settings.level || 3) - 1];
|
|
const areaFactor = () => Math.min(1.6, Math.max(0.3, (size.width * size.height) / REFERENCE_AREA));
|
|
const average = () => TWINKLE_COUNTS[(settings.count || 3) - 1] * areaFactor();
|
|
const sizeFactor = () => Math.min(1.2, Math.max(0.5, Math.min(size.width, size.height) / 240));
|
|
|
|
function buildSprites() {
|
|
const core = settings.core || "#ffffff";
|
|
sprites = settings.colors.map((color) => motifSprite(settings.motifId || "four-point-star", color, core));
|
|
streaks = settings.colors.map((color) => streakSprite(color, core));
|
|
}
|
|
|
|
/**
|
|
* Position libre : marges de 6 %, à distance des motifs vivants ; un tiers des motifs naissent près du pointeur, là où
|
|
* la lumière frappe.
|
|
*/
|
|
function position(radius) {
|
|
let best = null;
|
|
for (let attempt = 0; attempt < 4; attempt++) {
|
|
const near = pointer && random() < 0.35;
|
|
const x = near ? (pointer.x + (random() - 0.5) * 0.36) * size.width : (0.06 + random() * 0.88) * size.width;
|
|
const y = near ? (pointer.y + (random() - 0.5) * 0.36) * size.height : (0.06 + random() * 0.88) * size.height;
|
|
const candidate = { x: Math.min(size.width * 0.96, Math.max(size.width * 0.04, x)), y: Math.min(size.height * 0.96, Math.max(size.height * 0.04, y)) };
|
|
const crowded = items.some((item) => Math.hypot(item.x - candidate.x, item.y - candidate.y) < (item.radius + radius) * 1.6);
|
|
best = candidate;
|
|
if (!crowded) break;
|
|
}
|
|
return best;
|
|
}
|
|
|
|
function spawn(now) {
|
|
// Tailles contrastées : surtout des motifs moyens, et de temps en temps un grand éclat qui attire l'œil.
|
|
const radius = (9 + random() ** 2.5 * 18) * intensity() * sizeFactor();
|
|
const color = Math.floor(random() * settings.colors.length);
|
|
const life = lifetime() * (0.75 + random() * 0.5);
|
|
items.push({ ...position(radius), radius, color, born: now, life, rotation: random() * TAU, spin: (random() - 0.5) * 1.2 });
|
|
// Intervalle moyen = durée de vie / nombre moyen voulu : le nombre visible se stabilise autour de la moyenne.
|
|
nextSpawn = now + (lifetime() / average()) * (0.4 + random() * 1.2);
|
|
}
|
|
|
|
function drawItem(item, state) {
|
|
const spriteSize = item.radius * 2 * (SPRITE_SIZE / (SPRITE_SIZE * 0.34)) * state.scale;
|
|
context.save();
|
|
context.translate(item.x, item.y);
|
|
context.rotate(motifAngle(settings.motifId, item.rotation + item.spin * state.age));
|
|
context.globalAlpha = fade * state.alpha * Math.min(1, 0.75 + 0.25 * intensity());
|
|
context.drawImage(sprites[item.color], -spriteSize / 2, -spriteSize / 2, spriteSize, spriteSize);
|
|
// Le reflet garde sa croix de lumière toute sa vie ; les autres motifs la font jaillir à leur apogée.
|
|
const peak = settings.motifId === "glint" ? Math.max(state.peak, state.alpha * 0.7) : state.peak;
|
|
if (peak > 0 && settings.motifId !== "bubble") {
|
|
const streak = item.radius * 10 * (0.55 + 0.45 * peak) * Math.min(1.3, intensity());
|
|
context.globalAlpha = fade * peak;
|
|
context.drawImage(streaks[item.color], -streak / 2, -streak / 2, streak, streak);
|
|
}
|
|
context.restore();
|
|
}
|
|
|
|
function draw(now) {
|
|
const { width, height, ratio } = size;
|
|
context.setTransform(ratio, 0, 0, ratio, 0, 0);
|
|
context.clearRect(0, 0, width, height);
|
|
context.globalCompositeOperation = "lighter";
|
|
for (const item of items) {
|
|
const age = (now - item.born) / item.life;
|
|
drawItem(item, { ...twinkleState(age), age });
|
|
}
|
|
context.globalAlpha = 1;
|
|
context.globalCompositeOperation = "source-over";
|
|
}
|
|
|
|
function tick(now) {
|
|
frame = 0;
|
|
if (destroyed || !size.width) return;
|
|
// Plusieurs naissances par image si la fréquence l'exige (beaucoup de motifs très rapides).
|
|
while (active && now >= nextSpawn && items.length < average() * 2.5) spawn(now);
|
|
items = items.filter((item) => now - item.born < item.life);
|
|
fade = leftAt ? Math.max(0, 1 - (now - leftAt) / OUTRO_MS) : 1;
|
|
if (!fade) items = [];
|
|
draw(now);
|
|
if (active || items.length) frame = requestAnimationFrame(tick);
|
|
else backing.release();
|
|
}
|
|
|
|
const schedule = () => {
|
|
if (!frame && !destroyed) frame = requestAnimationFrame(tick);
|
|
};
|
|
|
|
// Sprites construits à la première animation (et pris dans le cache partagé) : une carte jamais survolée n'en crée pas.
|
|
return {
|
|
resize(width, height, ratio = 1) {
|
|
size = { width, height, ratio };
|
|
backing.resize(width, height, ratio);
|
|
items = [];
|
|
if (active) schedule();
|
|
},
|
|
setActive(next) {
|
|
if (next && !active) nextSpawn = 0;
|
|
if (!next && active) leftAt = performance.now();
|
|
if (next) leftAt = 0;
|
|
if (next) {
|
|
if (!sprites.length) buildSprites();
|
|
backing.attach();
|
|
}
|
|
active = next;
|
|
if (active) schedule();
|
|
},
|
|
setPointer(next) {
|
|
pointer = next;
|
|
},
|
|
setOptions(next) {
|
|
settings = { ...settings, ...next };
|
|
sprites = [];
|
|
streaks = [];
|
|
},
|
|
/** Mouvement réduit : quelques motifs fixes, à mi-éclat. */
|
|
renderStatic() {
|
|
if (!size.width) return;
|
|
if (!sprites.length) buildSprites();
|
|
backing.attach();
|
|
items = [];
|
|
const count = Math.max(2, Math.round(average() / 2));
|
|
for (let index = 0; index < count; index++) {
|
|
const radius = 10 * intensity() * sizeFactor();
|
|
items.push({ ...position(radius), radius, color: index % settings.colors.length, born: 0, life: 1, rotation: random() * TAU, spin: 0 });
|
|
}
|
|
const { width, height, ratio } = size;
|
|
context.setTransform(ratio, 0, 0, ratio, 0, 0);
|
|
context.clearRect(0, 0, width, height);
|
|
context.globalCompositeOperation = "lighter";
|
|
for (const item of items) drawItem(item, { scale: 0.85, alpha: 0.6, peak: 0, age: 0 });
|
|
context.globalCompositeOperation = "source-over";
|
|
items = [];
|
|
},
|
|
destroy() {
|
|
destroyed = true;
|
|
cancelAnimationFrame(frame);
|
|
}
|
|
};
|
|
}
|