Prototype lightning, twinkle and firefly particles in the visual lab
The Particles tab of the visual lab now previews three persistent particle animations for mythic cards, drawn on a canvas inside the illustration or over the whole card. Lightning bolts grow from the edges towards a random point near the hero and pass behind a cut-out hero with the prismatic parallax. Twinkles pop chosen motifs in and out with a light cross at their peak. Fireflies drift on winding paths, pulse and move away from the pointer. Each animation offers a palette, a zone and its own sliders, runs only on a large or hovered card, and stays still under reduced motion. Nothing is saved on the variations yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
9f70bea523
commit
f77a0ca577
15 changed files with 1779 additions and 16 deletions
184
website/src/features/cards/holoTwinkle.js
Normal file
184
website/src/features/cards/holoTwinkle.js
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
// Rôle : 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 { motifAngle, motifSprite, SPRITE_SIZE, streakSprite } from "./flareSprites.js";
|
||||
|
||||
const TAU = Math.PI * 2;
|
||||
|
||||
export const TWINKLE_MOTIFS = [
|
||||
{ id: "four-point-star", label: "Étoile à quatre branches" },
|
||||
{ id: "glint", label: "Reflet" },
|
||||
{ id: "star", label: "Étoile" },
|
||||
{ id: "diamond", label: "Losange" },
|
||||
{ id: "heart", label: "Cœur" },
|
||||
{ id: "snowflake", label: "Flocon" },
|
||||
{ id: "bubble", label: "Bulle" }
|
||||
];
|
||||
|
||||
// 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;
|
||||
|
||||
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 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 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 = 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 = 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);
|
||||
draw(now);
|
||||
if (active || items.length) frame = requestAnimationFrame(tick);
|
||||
else context.clearRect(0, 0, size.width, size.height);
|
||||
}
|
||||
|
||||
const schedule = () => {
|
||||
if (!frame && !destroyed) frame = requestAnimationFrame(tick);
|
||||
};
|
||||
|
||||
buildSprites();
|
||||
return {
|
||||
resize(width, height, ratio = 1) {
|
||||
size = { width, height, ratio };
|
||||
canvas.width = Math.max(1, Math.round(width * ratio));
|
||||
canvas.height = Math.max(1, Math.round(height * ratio));
|
||||
items = [];
|
||||
if (active) schedule();
|
||||
},
|
||||
setActive(next) {
|
||||
if (next && !active) nextSpawn = 0;
|
||||
active = next;
|
||||
if (active) schedule();
|
||||
},
|
||||
setPointer(next) {
|
||||
pointer = next;
|
||||
},
|
||||
setOptions(next) {
|
||||
settings = { ...settings, ...next };
|
||||
buildSprites();
|
||||
},
|
||||
// Mouvement réduit : quelques motifs fixes, à mi-éclat.
|
||||
renderStatic() {
|
||||
if (!size.width) return;
|
||||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue