/** * @file Ressources communes des particules persistantes : sprites des motifs (scintillements, lucioles), * silhouettes procédurales de rayon 1 rendues une fois par couleur avec halo et cœur clair puis * recopiées à chaque image, gardées dans un cache partagé entre toutes les cartes ; et surface de * dessin d'un canvas réservée seulement pendant l'animation. */ const TAU = Math.PI * 2; export const SPRITE_SIZE = 96; // Angle d'affichage d'un motif. La bulle ne tourne jamais : son reflet reste en haut à droite, comme la lumière qui // l'éclaire. Le cœur reste à l'endroit, à peine incliné. Les autres motifs prennent l'angle libre qu'on leur donne. export function motifAngle(motifId, angle) { if (motifId === "bubble") return 0; if (motifId === "heart") return Math.sin(angle) * 0.25; return angle; } // Silhouette d'un motif centrée sur l'origine, de rayon 1 ; `stroke` pour les motifs en traits. function motifPath(context, motifId) { context.beginPath(); if (motifId === "four-point-star" || motifId === "glint") { const k = motifId === "glint" ? 0.06 : 0.16; context.moveTo(0, -1); context.quadraticCurveTo(k, -k, 1, 0); context.quadraticCurveTo(k, k, 0, 1); context.quadraticCurveTo(-k, k, -1, 0); context.quadraticCurveTo(-k, -k, 0, -1); } else if (motifId === "star") { for (let index = 0; index < 10; index++) { const radius = index % 2 ? 0.45 : 1; const angle = -Math.PI / 2 + (index * Math.PI) / 5; context.lineTo(Math.cos(angle) * radius, Math.sin(angle) * radius); } context.closePath(); } else if (motifId === "diamond") { context.moveTo(0, -1); context.lineTo(0.62, 0); context.lineTo(0, 1); context.lineTo(-0.62, 0); context.closePath(); } else if (motifId === "heart") { context.moveTo(0, 0.9); context.bezierCurveTo(-1.1, 0.1, -0.85, -0.85, 0, -0.35); context.bezierCurveTo(0.85, -0.85, 1.1, 0.1, 0, 0.9); } else if (motifId === "snowflake") { for (let arm = 0; arm < 6; arm++) { const angle = (arm * Math.PI) / 3; const point = (radius, offset = 0) => [Math.cos(angle + offset) * radius, Math.sin(angle + offset) * radius]; context.moveTo(0, 0); context.lineTo(...point(1)); context.moveTo(...point(0.55)); context.lineTo(...point(0.8, 0.32)); context.moveTo(...point(0.55)); context.lineTo(...point(0.8, -0.32)); } } else if (motifId === "firefly") { context.arc(0, 0, 0.55, 0, TAU); } else { context.arc(0, 0, 1, 0, TAU); } } // Bulle : une sphère translucide (voile coloré et contour lumineux) avec son reflet clair décalé en haut à droite, aux // bords à peine adoucis par un dégradé (un filtre de flou n'est pas pris en charge partout sur un canvas). function bubble(context, radius, color, core) { context.save(); context.scale(radius, radius); context.beginPath(); context.arc(0, 0, 1, 0, TAU); context.globalAlpha = 0.28; context.fillStyle = color; context.fill(); context.globalAlpha = 1; context.lineWidth = 0.09; context.strokeStyle = color; context.shadowColor = color; context.shadowBlur = 6; context.stroke(); context.shadowBlur = 0; const highlight = context.createRadialGradient(0.36, -0.36, 0, 0.36, -0.36, 0.3); highlight.addColorStop(0, core); highlight.addColorStop(0.75, core); highlight.addColorStop(1, "transparent"); context.globalAlpha = 0.9; context.fillStyle = highlight; context.beginPath(); context.arc(0.36, -0.36, 0.3, 0, TAU); context.fill(); context.restore(); } // Sprite d'un motif dans une couleur : halo, silhouette colorée lumineuse, puis cœur clair. Rendu une seule fois (le flou y // est permis), puis seulement recopié à chaque image. function drawMotifSprite(motifId, color, core) { const canvas = document.createElement("canvas"); canvas.width = SPRITE_SIZE; canvas.height = SPRITE_SIZE; const context = canvas.getContext("2d"); const half = SPRITE_SIZE / 2; const halo = context.createRadialGradient(half, half, 0, half, half, half); halo.addColorStop(0, color); halo.addColorStop(0.2, color); // La luciole rayonne plus loin que les motifs dessinés. halo.addColorStop(motifId === "firefly" ? 1 : 0.75, "transparent"); // La luciole est surtout un halo : petit cœur, large lueur. context.globalAlpha = motifId === "bubble" ? 0.3 : motifId === "firefly" ? 0.8 : 0.55; context.fillStyle = halo; context.fillRect(0, 0, SPRITE_SIZE, SPRITE_SIZE); context.globalAlpha = 1; context.translate(half, half); if (motifId === "bubble") { bubble(context, half * 0.34, color, core); return canvas; } const stroked = motifId === "snowflake"; for (const [scale, style, blur, width] of [[0.34, color, 10, 0.16], [0.2, core, 4, 0.1]]) { context.save(); context.scale(half * scale, half * scale); context.shadowColor = color; context.shadowBlur = blur; motifPath(context, motifId); if (stroked) { context.lineCap = "round"; context.lineWidth = width; context.strokeStyle = style; context.stroke(); } else { context.fillStyle = style; context.fill(); } context.restore(); } return canvas; } // Éclat en croix de l'apogée : deux traits fins dégradés, clairs au centre. function drawStreakSprite(color, core) { const canvas = document.createElement("canvas"); canvas.width = SPRITE_SIZE * 2; canvas.height = SPRITE_SIZE * 2; const context = canvas.getContext("2d"); const size = SPRITE_SIZE * 2; for (const vertical of [false, true]) { const gradient = vertical ? context.createLinearGradient(0, 0, 0, size) : context.createLinearGradient(0, 0, size, 0); gradient.addColorStop(0, "transparent"); gradient.addColorStop(0.4, color); gradient.addColorStop(0.5, core); gradient.addColorStop(0.6, color); gradient.addColorStop(1, "transparent"); context.fillStyle = gradient; const thickness = vertical ? 4 : 5; if (vertical) context.fillRect(size / 2 - thickness / 2, size * 0.18, thickness, size * 0.64); else context.fillRect(0, size / 2 - thickness / 2, size, thickness); } return canvas; } // Cache partagé : une grille de cartes mythiques réutilise les mêmes sprites au lieu d'en refaire par carte. Borné, les plus // anciens partent d'abord. const SPRITE_CACHE_LIMIT = 160; const spriteCache = new Map(); function cached(key, build) { if (!spriteCache.has(key)) { if (spriteCache.size >= SPRITE_CACHE_LIMIT) spriteCache.delete(spriteCache.keys().next().value); spriteCache.set(key, build()); } return spriteCache.get(key); } export const motifSprite = (motifId, color, core) => cached(`motif|${motifId}|${color}|${core}`, () => drawMotifSprite(motifId, color, core)); export const streakSprite = (color, core) => cached(`streak|${color}|${core}`, () => drawStreakSprite(color, core)); // Surface de dessin d'un canvas : réservée (largeur x hauteur x densité de pixels) seulement quand le moteur anime ou fige une // image, libérée (0 x 0) au repos. Une grille de cartes au repos ne garde ainsi aucun pixel en mémoire. export function canvasBacking(canvas) { let size = { width: 0, height: 0, ratio: 1 }; let attached = false; const attach = () => { const width = Math.max(1, Math.round(size.width * size.ratio)); const height = Math.max(1, Math.round(size.height * size.ratio)); if (attached && canvas.width === width && canvas.height === height) return; canvas.width = width; canvas.height = height; attached = true; }; const release = () => { canvas.width = 0; canvas.height = 0; attached = false; }; release(); return { resize(width, height, ratio) { size = { width, height, ratio }; if (attached) { attached = false; attach(); } }, attach, release }; }