@handysolver/festive 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,515 @@
1
+ import { media } from '../core.js';
2
+ // The celebration: one simulation in viewport coordinates, drawn onto several canvases (behind the
3
+ // page, inside the top bar, inside the sidebar). Each shows its own part of the same scene, so a
4
+ // ribbon seems to fall through the whole frame while always staying behind text and cards.
5
+ export const CELEBRATION_MS = 3500;
6
+ const LIFE_S = CELEBRATION_MS / 1000;
7
+ const MAX_PIECES = 360; // several celebrations in quick succession
8
+ const MIN_GAP_MS = 300;
9
+ // The party popper: how fast the air slows a piece down, and how hard it falls. The equivalent of
10
+ // vx *= 0.96 and vy += 0.4 a frame at 60 frames a second, whatever the screen's real rate.
11
+ const AIR = -Math.log(0.96) * 60;
12
+ const GRAVITY = 0.4 * 60 * 60;
13
+ // The party popper's pieces, per festival
14
+ const POPPER = {
15
+ parade: ['ribbon', 'ribbon', 'ribbon', 'star', 'snowflake'],
16
+ fireworks: ['spark', 'star', 'star', 'flare', 'ribbon'],
17
+ cannons: ['ribbon', 'ribbon', 'star'],
18
+ petals: ['petal', 'petal', 'ribbon'],
19
+ tricolour: ['ribbon', 'ribbon', 'ribbon', 'star'],
20
+ clouds: ['cloud'],
21
+ };
22
+ export const prefersReducedMotion = () => media('(prefers-reduced-motion: reduce)')?.matches ?? false;
23
+ const rand = (min, max) => min + Math.random() * (max - min);
24
+ const pick = (list) => list[Math.floor(Math.random() * list.length)];
25
+ // A colour moved towards white (amount > 0) or black (amount < 0)
26
+ const shades = new Map();
27
+ function shade(hex, amount) {
28
+ const key = hex + amount;
29
+ let value = shades.get(key);
30
+ if (!value) {
31
+ const target = amount > 0 ? 255 : 0;
32
+ const mix = [1, 3, 5].map((i) => {
33
+ const c = parseInt(hex.slice(i, i + 2), 16);
34
+ return Math.round(c + (target - c) * Math.abs(amount));
35
+ });
36
+ value = `rgb(${mix.join(',')})`;
37
+ shades.set(key, value);
38
+ }
39
+ return value;
40
+ }
41
+ const SIZES = {
42
+ ribbon: () => [rand(11, 16), rand(6, 8)],
43
+ star: () => {
44
+ const size = rand(13, 20);
45
+ return [size, size];
46
+ },
47
+ snowflake: () => {
48
+ const size = rand(12, 18);
49
+ return [size, size];
50
+ },
51
+ petal: () => [rand(13, 19), rand(8, 11)],
52
+ spark: () => [rand(16, 28), rand(2.5, 3.5)],
53
+ flare: () => [rand(12, 16), rand(20, 28)],
54
+ cloud: () => {
55
+ const size = rand(150, 260);
56
+ return [size, size];
57
+ },
58
+ };
59
+ function make(shape, colors, at) {
60
+ const [w, h] = SIZES[shape]();
61
+ return {
62
+ shape,
63
+ color: pick(colors),
64
+ x: 0,
65
+ y: 0,
66
+ vx: 0,
67
+ vy: 0,
68
+ w,
69
+ h,
70
+ fall: rand(190, 520),
71
+ drag: rand(1.6, 2.6),
72
+ sway: rand(20, 60),
73
+ phase: rand(0, Math.PI * 2),
74
+ tilt: rand(0, Math.PI * 2),
75
+ spin: rand(-2, 2),
76
+ rotationX: rand(0, Math.PI * 2),
77
+ rotationY: rand(0, Math.PI * 2),
78
+ spinX: rand(3, 8),
79
+ spinY: rand(3, 8),
80
+ age: 0,
81
+ life: rand(2.5, 2.9),
82
+ ...at,
83
+ };
84
+ }
85
+ // A selector that is wrong must never break the app
86
+ function boxOf(selector) {
87
+ try {
88
+ return document.querySelector(selector)?.getBoundingClientRect();
89
+ }
90
+ catch {
91
+ return undefined;
92
+ }
93
+ }
94
+ function measure(layout) {
95
+ const width = window.innerWidth;
96
+ const height = window.innerHeight;
97
+ const bar = boxOf(layout.header ?? 'header');
98
+ const side = boxOf(layout.sidebar ?? 'aside');
99
+ const main = boxOf(layout.content ?? 'main');
100
+ const sideRight = side && side.right > 0 ? side.right : 0;
101
+ const lanes = [];
102
+ if (side && sideRight)
103
+ lanes.push([side.left, side.right]);
104
+ if (main) {
105
+ lanes.push([sideRight, main.left + 16], [main.right - 16, width]);
106
+ }
107
+ return {
108
+ width,
109
+ height,
110
+ top: bar?.bottom ?? 68,
111
+ left: bar?.left ?? 0,
112
+ lanes: lanes.filter(([a, b]) => b - a >= 12),
113
+ };
114
+ }
115
+ // Mostly in the open columns, where cards do not hide it
116
+ const laneX = (f, chance = 0.78) => {
117
+ if (!f.lanes.length || Math.random() > chance)
118
+ return rand(0, f.width);
119
+ const [a, b] = pick(f.lanes);
120
+ return rand(a, b);
121
+ };
122
+ const many = (n, build) => Array.from({ length: n }, (_, i) => build(i));
123
+ // Pops up out of the top bar, then falls
124
+ const fromBar = (f, over = {}) => ({
125
+ x: laneX(f),
126
+ y: f.top - rand(0, 30),
127
+ vx: rand(-160, 160),
128
+ vy: rand(-190, 40),
129
+ age: -rand(0, 0.7),
130
+ ...over,
131
+ });
132
+ const around = (x, y, speed) => {
133
+ const angle = rand(0, Math.PI * 2);
134
+ const v = rand(0.35, 1) * speed;
135
+ return { x, y, vx: Math.cos(angle) * v, vy: Math.sin(angle) * v };
136
+ };
137
+ const SCENES = {
138
+ parade: (f, colors) => [
139
+ ...many(66, () => make('ribbon', colors, fromBar(f))),
140
+ ...many(26, () => make('star', colors.slice(0, 3), fromBar(f, { fall: rand(150, 420) }))),
141
+ ...many(12, () => make('snowflake', ['#7dd3fc', '#bae6fd'], fromBar(f, { fall: rand(130, 380), sway: rand(40, 80) }))),
142
+ ],
143
+ fireworks: (f, colors) => [
144
+ ...many(34, () => make('spark', colors, {
145
+ x: laneX(f, 0.7),
146
+ y: f.height + rand(0, 40),
147
+ vy: rand(-760, -480),
148
+ fall: -rand(120, 260),
149
+ sway: rand(4, 14),
150
+ age: -rand(0, 0.8),
151
+ })),
152
+ ...many(14, () => make('flare', colors.slice(0, 2), {
153
+ x: laneX(f, 0.8),
154
+ y: f.height + 20,
155
+ vy: rand(-780, -420),
156
+ fall: -rand(90, 260),
157
+ sway: rand(10, 24),
158
+ age: -rand(0, 0.7),
159
+ tilt: 0,
160
+ spin: 0,
161
+ })),
162
+ // Six golden starbursts: in the open columns, and along the top bar
163
+ ...[0.1, 0.35, 0.65, 0.95, 1.2, 1.45].flatMap((delay, i) => {
164
+ const x = i % 2 ? f.left + (f.width - f.left) * rand(0.5, 0.85) : laneX(f, 1);
165
+ const y = i % 2 ? f.top - 22 : f.height * rand(0.35, 0.65);
166
+ return many(14, (n) => make(n % 3 ? 'spark' : 'star', colors.slice(0, 3), {
167
+ ...around(x, y, 520),
168
+ drag: 3.2,
169
+ fall: rand(60, 120),
170
+ sway: 4,
171
+ age: -delay,
172
+ life: rand(1.4, 2),
173
+ }));
174
+ }),
175
+ ],
176
+ // Two cannons, three volleys each
177
+ cannons: (f, colors) => many(110, (i) => {
178
+ const fromLeft = i % 2 === 0;
179
+ const angle = rand(-0.25, 0.95);
180
+ const power = rand(350, 1900);
181
+ return make(i % 5 === 0 ? 'star' : 'ribbon', colors, {
182
+ x: fromLeft ? 0 : f.width,
183
+ y: f.top + rand(-20, 10),
184
+ vx: Math.cos(angle) * power * (fromLeft ? 1 : -1),
185
+ vy: Math.sin(angle) * power * 0.5 - 60,
186
+ fall: rand(170, 460),
187
+ age: -Math.floor(i / 37) * 0.32 - rand(0, 0.08),
188
+ });
189
+ }),
190
+ petals: (f, colors) => [
191
+ ...many(64, () => make('petal', colors, fromBar(f, { vx: rand(-40, 150), fall: rand(140, 420), sway: rand(50, 100), spinX: rand(1.5, 4) }))),
192
+ ...many(26, () => make('ribbon', ['#ea580c', '#f97316'], fromBar(f, { w: rand(16, 22), h: 5 }))),
193
+ ...many(14, () => make('spark', ['#fbbf24', '#f59e0b'], fromBar(f, { w: rand(12, 18), fall: rand(120, 360), sway: 10 }))),
194
+ ],
195
+ // Three bands side by side, falling together
196
+ tricolour: (f, colors) => many(110, (i) => {
197
+ const at = fromBar(f, { vx: rand(-60, 60), vy: rand(-120, 0), sway: 34, phase: rand(0, 0.8), fall: rand(190, 480) });
198
+ return make(i % 9 === 8 ? 'star' : 'ribbon', [colors[i % 3]], at);
199
+ }),
200
+ clouds: (f, colors) => [
201
+ ...many(10, (i) => make('cloud', [colors[i % colors.length]], {
202
+ x: i < 6 ? (f.width * (i + 0.5)) / 6 : laneX(f, 1),
203
+ y: i < 6 ? f.top - rand(0, 30) : f.height * rand(0.3, 0.9),
204
+ fall: rand(8, 24),
205
+ sway: 12,
206
+ age: -rand(0, 0.5),
207
+ life: rand(2.7, 3),
208
+ })),
209
+ ...many(76, () => make('ribbon', colors, fromBar(f, { w: rand(8, 12), h: rand(6, 9) }))),
210
+ ...many(12, () => make('star', colors, fromBar(f))),
211
+ ],
212
+ };
213
+ // ---- Drawing ----
214
+ const isPale = (color) => color === '#ffffff' || color === '#f0f9ff' || color === '#f1f5f9';
215
+ // Glowing shapes are drawn once and reused: blurring a shadow for every piece, on every canvas, in
216
+ // every frame is what makes canvas animations stutter
217
+ const sprites = new Map();
218
+ function sprite(key, size, paint) {
219
+ let canvas = sprites.get(key);
220
+ if (!canvas) {
221
+ canvas = document.createElement('canvas');
222
+ canvas.width = canvas.height = size;
223
+ const ctx = canvas.getContext('2d');
224
+ if (ctx) {
225
+ ctx.translate(size / 2, size / 2);
226
+ paint(ctx);
227
+ }
228
+ sprites.set(key, canvas);
229
+ }
230
+ return canvas;
231
+ }
232
+ function starPath(ctx, r) {
233
+ ctx.beginPath();
234
+ for (let i = 0; i < 8; i++) {
235
+ const a = (i * Math.PI) / 4;
236
+ const d = i % 2 ? r * 0.3 : r;
237
+ ctx.lineTo(Math.cos(a) * d, Math.sin(a) * d);
238
+ }
239
+ ctx.closePath();
240
+ }
241
+ // Each is painted at twice the size it is usually shown, with room around it for the glow
242
+ const SPRITES = {
243
+ star: (color, dark) => sprite(`star${color}${dark}`, 80, (ctx) => {
244
+ ctx.shadowBlur = 16;
245
+ ctx.shadowColor = color;
246
+ ctx.fillStyle = !dark && isPale(color) ? '#94a3b8' : color;
247
+ starPath(ctx, 20);
248
+ ctx.fill();
249
+ ctx.shadowBlur = 0;
250
+ ctx.fillStyle = shade(color, 0.65);
251
+ starPath(ctx, 10);
252
+ ctx.fill();
253
+ }),
254
+ snowflake: (color, dark) => sprite(`snow${color}${dark}`, 80, (ctx) => {
255
+ const r = 20;
256
+ ctx.strokeStyle = dark ? color : '#0284c7';
257
+ ctx.lineWidth = 4;
258
+ ctx.lineCap = 'round';
259
+ ctx.shadowBlur = 14;
260
+ ctx.shadowColor = color;
261
+ ctx.beginPath();
262
+ for (let i = 0; i < 6; i++) {
263
+ ctx.rotate(Math.PI / 3);
264
+ ctx.moveTo(0, 0);
265
+ ctx.lineTo(0, -r);
266
+ ctx.moveTo(-r * 0.28, -r * 0.85);
267
+ ctx.lineTo(0, -r * 0.55);
268
+ ctx.lineTo(r * 0.28, -r * 0.85);
269
+ }
270
+ ctx.stroke();
271
+ }),
272
+ // A streak 40 long and 6 thick, its head at +20
273
+ spark: (color) => sprite(`spark${color}`, 80, (ctx) => {
274
+ const trail = ctx.createLinearGradient(-20, 0, 20, 0);
275
+ trail.addColorStop(0, 'transparent');
276
+ trail.addColorStop(1, color);
277
+ ctx.strokeStyle = trail;
278
+ ctx.lineWidth = 6;
279
+ ctx.lineCap = 'round';
280
+ ctx.shadowBlur = 14;
281
+ ctx.shadowColor = color;
282
+ ctx.beginPath();
283
+ ctx.moveTo(-20, 0);
284
+ ctx.lineTo(20, 0);
285
+ ctx.stroke();
286
+ }),
287
+ // A diya's flame, 20 wide and 40 tall
288
+ flare: (color) => sprite(`flare${color}`, 96, (ctx) => {
289
+ ctx.shadowBlur = 24;
290
+ ctx.shadowColor = color;
291
+ for (const [size, fill] of [[1, color], [0.55, '#fef08a']]) {
292
+ const w = 12 * size;
293
+ const h = 20 * size;
294
+ ctx.fillStyle = fill;
295
+ ctx.beginPath();
296
+ ctx.moveTo(0, -h);
297
+ ctx.bezierCurveTo(w * 1.2, -h * 0.1, w, h, 0, h);
298
+ ctx.bezierCurveTo(-w, h, -w * 1.2, -h * 0.1, 0, -h);
299
+ ctx.fill();
300
+ ctx.shadowBlur = 0;
301
+ }
302
+ }),
303
+ // Powder, soft to its edge
304
+ cloud: (color) => sprite(`cloud${color}`, 160, (ctx) => {
305
+ const powder = ctx.createRadialGradient(0, 0, 0, 0, 0, 80);
306
+ powder.addColorStop(0, color);
307
+ powder.addColorStop(0.5, `${color}99`);
308
+ powder.addColorStop(1, `${color}00`);
309
+ ctx.fillStyle = powder;
310
+ ctx.fillRect(-80, -80, 160, 160);
311
+ }),
312
+ };
313
+ // `w` and `h` are the size the whole sprite, glow included, is shown at
314
+ const stamp = (ctx, image, w, h = w) => ctx.drawImage(image, -w / 2, -h / 2, w, h);
315
+ function drawPiece(ctx, p, dark) {
316
+ const left = 1 - p.age / p.life;
317
+ ctx.save();
318
+ ctx.translate(p.x, p.y);
319
+ ctx.globalAlpha = Math.max(0, Math.min(1, p.age / 0.06, left / 0.28));
320
+ switch (p.shape) {
321
+ case 'ribbon':
322
+ case 'petal': {
323
+ // Turning about X and Y flattens the shape and shows its darker back, like paper in the air
324
+ const faceX = Math.cos(p.rotationX);
325
+ const faceY = Math.cos(p.rotationY);
326
+ const front = faceX * faceY > 0;
327
+ ctx.rotate(p.tilt);
328
+ ctx.scale(Math.sign(faceY || 1) * Math.max(0.35, Math.abs(faceY)), Math.max(0.4, Math.abs(faceX)));
329
+ if (p.shape === 'ribbon') {
330
+ // A bright stripe travels along it as it turns, for the foil
331
+ const shine = 0.5 + 0.4 * Math.sin(p.rotationY);
332
+ const foil = ctx.createLinearGradient(-p.w / 2, 0, p.w / 2, 0);
333
+ const base = front ? p.color : shade(p.color, -0.25);
334
+ foil.addColorStop(0, base);
335
+ foil.addColorStop(shine, front ? shade(p.color, 0.45) : base);
336
+ foil.addColorStop(1, base);
337
+ ctx.fillStyle = foil;
338
+ ctx.fillRect(-p.w / 2, -p.h / 2, p.w, p.h);
339
+ // White and silver need an edge to show on a white page
340
+ if (!dark && isPale(p.color)) {
341
+ ctx.strokeStyle = '#94a3b8';
342
+ ctx.lineWidth = 1;
343
+ ctx.strokeRect(-p.w / 2, -p.h / 2, p.w, p.h);
344
+ }
345
+ }
346
+ else {
347
+ ctx.fillStyle = front ? p.color : shade(p.color, -0.2);
348
+ ctx.beginPath();
349
+ ctx.moveTo(-p.w / 2, 0);
350
+ ctx.bezierCurveTo(-p.w / 4, -p.h * 0.8, p.w / 3, -p.h * 0.7, p.w / 2, 0);
351
+ ctx.bezierCurveTo(p.w / 3, p.h * 0.7, -p.w / 4, p.h * 0.8, -p.w / 2, 0);
352
+ ctx.fill();
353
+ ctx.strokeStyle = shade(p.color, -0.3);
354
+ ctx.lineWidth = 0.8;
355
+ ctx.beginPath();
356
+ ctx.moveTo(-p.w / 2, 0);
357
+ ctx.lineTo(p.w / 4, 0);
358
+ ctx.stroke();
359
+ }
360
+ break;
361
+ }
362
+ case 'star': {
363
+ const twinkle = 0.75 + 0.25 * Math.sin(p.age * 9 + p.phase);
364
+ ctx.rotate(p.tilt * 0.25);
365
+ stamp(ctx, SPRITES.star(p.color, dark), p.w * 2 * twinkle);
366
+ break;
367
+ }
368
+ case 'snowflake':
369
+ ctx.rotate(p.tilt);
370
+ stamp(ctx, SPRITES.snowflake(p.color, dark), p.w * 2);
371
+ break;
372
+ case 'spark':
373
+ // Points the way it is travelling, its head at the piece's position
374
+ ctx.rotate(Math.atan2(p.vy, p.vx));
375
+ ctx.translate(-p.w / 2, 0);
376
+ stamp(ctx, SPRITES.spark(p.color), p.w * 2, (p.h / 6) * 80);
377
+ break;
378
+ case 'flare': {
379
+ const flicker = 1 + 0.12 * Math.sin(p.age * 17 + p.phase);
380
+ stamp(ctx, SPRITES.flare(p.color), (p.w / 24) * 96, (p.h / 40) * 96 * flicker);
381
+ break;
382
+ }
383
+ case 'cloud':
384
+ // Spreads out as it goes
385
+ ctx.globalAlpha *= dark ? 0.4 : 0.34;
386
+ stamp(ctx, SPRITES.cloud(p.color), p.w * (0.4 + 0.6 * Math.sqrt(Math.min(1, p.age / 1.2))));
387
+ break;
388
+ }
389
+ ctx.restore();
390
+ }
391
+ // Fired from a point: out in an arc to the left, centre and right. Most are thrown exactly
392
+ // as hard as it takes to come down in one of the open columns, since the cards hide the rest.
393
+ function popper(f, { motion, confetti }, from) {
394
+ const puffs = motion === 'clouds';
395
+ return many(puffs ? 60 : 78, (i) => {
396
+ const shape = pick(POPPER[motion]);
397
+ const arm = i % 3; // left, centre, right
398
+ const lanes = f.lanes.filter(([a, b]) => (arm === 0 ? (a + b) / 2 < from.x : (a + b) / 2 > from.x));
399
+ const lane = arm !== 1 && lanes.length ? pick(lanes) : null;
400
+ // Air resistance brings it to a stop after travelling speed / AIR
401
+ const vx = lane ? (rand(lane[0], lane[1]) - from.x) * AIR : rand(-620, 620) * (arm === 1 ? 1 : 1.6);
402
+ const size = rand(50, 120);
403
+ return make(shape, confetti, {
404
+ x: from.x,
405
+ y: from.y,
406
+ vx: vx * rand(0.96, 1.06),
407
+ // Gently upwards at most: the badge is at the top of the screen, and higher would be out of sight
408
+ vy: rand(-190, 260),
409
+ drag: AIR,
410
+ // Light foil falls slower than its weight: between a fifth of gravity and all of it
411
+ fall: (GRAVITY / AIR) * (puffs ? rand(0.12, 0.5) : rand(0.22, 1)),
412
+ sway: shape === 'spark' ? 4 : rand(14, 46),
413
+ spinX: rand(6, 14),
414
+ spinY: rand(6, 14),
415
+ age: -rand(0, 0.1),
416
+ life: rand(2.7, 3.3),
417
+ ...(puffs ? { w: size, h: size } : {}),
418
+ });
419
+ });
420
+ }
421
+ export class Celebration {
422
+ constructor() {
423
+ this.dark = false;
424
+ this.layout = {};
425
+ this.pieces = [];
426
+ this.surfaces = new Set();
427
+ this.painted = new WeakSet(); // those with something on them
428
+ this.frame = 0;
429
+ this.last = 0;
430
+ this.lastBurst = -Infinity;
431
+ this.tick = (now) => {
432
+ const dt = this.last ? Math.min((now - this.last) / 1000, 0.05) : 0;
433
+ this.last = now;
434
+ for (const p of this.pieces) {
435
+ p.age += dt;
436
+ if (p.age <= 0)
437
+ continue;
438
+ const ease = 1 - Math.exp(-p.drag * dt);
439
+ p.vx -= p.vx * ease;
440
+ p.vy += (p.fall - p.vy) * ease;
441
+ p.x += (p.vx + Math.sin(p.age * 2.6 + p.phase) * p.sway) * dt;
442
+ p.y += p.vy * dt;
443
+ p.tilt += p.spin * dt;
444
+ p.rotationX += p.spinX * dt;
445
+ p.rotationY += p.spinY * dt;
446
+ }
447
+ this.pieces = this.pieces.filter((p) => p.age < p.life && p.age < LIFE_S);
448
+ const ratio = Math.min(window.devicePixelRatio || 1, 1.5);
449
+ this.surfaces.forEach((canvas) => this.paint(canvas, ratio));
450
+ // Nothing left: stop drawing altogether
451
+ this.frame = this.pieces.length ? requestAnimationFrame(this.tick) : 0;
452
+ };
453
+ }
454
+ addSurface(canvas) {
455
+ this.surfaces.add(canvas);
456
+ return () => {
457
+ this.surfaces.delete(canvas);
458
+ };
459
+ }
460
+ // The festival's opening scene, or with a point, the party popper fired from there
461
+ burst(festival, from) {
462
+ const now = performance.now();
463
+ if (prefersReducedMotion() || now - this.lastBurst < MIN_GAP_MS)
464
+ return;
465
+ this.lastBurst = now;
466
+ const frame = measure(this.layout);
467
+ const added = from ? popper(frame, festival, from) : SCENES[festival.motion](frame, festival.confetti);
468
+ this.pieces = [...this.pieces, ...added].slice(-MAX_PIECES);
469
+ if (!this.frame) {
470
+ this.last = 0;
471
+ this.frame = requestAnimationFrame(this.tick);
472
+ }
473
+ }
474
+ clear() {
475
+ cancelAnimationFrame(this.frame);
476
+ this.frame = 0;
477
+ this.pieces = [];
478
+ this.lastBurst = -Infinity;
479
+ // Setting the size also wipes the canvas, whatever its transform
480
+ this.surfaces.forEach((canvas) => {
481
+ canvas.width = canvas.width;
482
+ this.painted.delete(canvas);
483
+ });
484
+ sprites.clear();
485
+ shades.clear();
486
+ }
487
+ paint(canvas, ratio) {
488
+ const ctx = canvas.getContext('2d');
489
+ const box = canvas.getBoundingClientRect();
490
+ if (!ctx)
491
+ return;
492
+ const visible = this.pieces.filter((p) => {
493
+ const reach = Math.max(p.w, p.h) + 16;
494
+ return p.age > 0 && p.x > box.left - reach && p.x < box.right + reach && p.y > box.top - reach && p.y < box.bottom + reach;
495
+ });
496
+ // Nothing to show and nothing to wipe
497
+ if (!visible.length && !this.painted.has(canvas))
498
+ return;
499
+ const width = Math.round(box.width * ratio);
500
+ const height = Math.round(box.height * ratio);
501
+ if (canvas.width !== width || canvas.height !== height) {
502
+ canvas.width = width;
503
+ canvas.height = height;
504
+ }
505
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
506
+ ctx.clearRect(0, 0, width, height);
507
+ // The canvas's own corner becomes its place in the shared scene
508
+ ctx.setTransform(ratio, 0, 0, ratio, -box.left * ratio, -box.top * ratio);
509
+ visible.forEach((p) => drawPiece(ctx, p, this.dark));
510
+ if (visible.length)
511
+ this.painted.add(canvas);
512
+ else
513
+ this.painted.delete(canvas);
514
+ }
515
+ }
@@ -0,0 +1,30 @@
1
+ export type FestivalKind = 'diwali' | 'dussehra' | 'christmas' | 'new-year' | 'republic-day' | 'independence-day' | 'holi';
2
+ export type FestiveMotion = 'parade' | 'fireworks' | 'cannons' | 'petals' | 'tricolour' | 'clouds';
3
+ export type FestiveGarland = 'lights' | 'diyas' | 'marigolds' | 'shimmer' | 'silk' | 'iridescent';
4
+ export interface FestivalTheme {
5
+ accent: string;
6
+ palette: string[];
7
+ confetti: string[];
8
+ motion: FestiveMotion;
9
+ garland: FestiveGarland;
10
+ motif: string | null;
11
+ badge: string;
12
+ pill: {
13
+ border: string;
14
+ shadow: string;
15
+ };
16
+ line: string;
17
+ }
18
+ export interface ActiveFestival extends FestivalTheme {
19
+ id: string;
20
+ kind: FestivalKind;
21
+ isPreview: boolean;
22
+ }
23
+ export declare const FESTIVALS: Record<FestivalKind, FestivalTheme>;
24
+ export declare function festivalFromId(id: string, isPreview?: boolean, today?: Date): ActiveFestival | null;
25
+ export declare function activeFromReply(reply: unknown, date: string): ActiveFestival | null;
26
+ export declare const dismissKey: (app: string, festivalId: string, today?: Date) => string;
27
+ export declare function previewFromAddress({ search, hash }: {
28
+ search: string;
29
+ hash: string;
30
+ }): ActiveFestival | null;
@@ -0,0 +1,112 @@
1
+ // How each festival looks in this app. Which one is on today comes from the shared Festival API;
2
+ // anything it returns that is not listed here is ignored.
3
+ const TRICOLOUR = "linear-gradient(to right in oklab, oklch(70.5% 0.213 47.604), #fff, oklch(62.7% 0.194 149.214))";
4
+ export const FESTIVALS = {
5
+ christmas: {
6
+ accent: '#059669',
7
+ palette: ['#059669', '#e11d48', '#facc15'],
8
+ confetti: ['#e11d48', '#059669', '#facc15', '#f0f9ff'],
9
+ motion: 'parade',
10
+ garland: 'lights',
11
+ motif: '🎄',
12
+ badge: 'Season’s Greetings',
13
+ pill: { border: "oklch(69.6% 0.17 162.48)", shadow: "oklch(59.6% 0.145 163.225 / 0.25)" },
14
+ line: "linear-gradient(to right in oklab, oklch(50.8% 0.118 165.612), oklch(69.6% 0.17 162.48), oklch(50.8% 0.118 165.612))",
15
+ },
16
+ diwali: {
17
+ accent: '#d97706',
18
+ palette: ['#eab308', '#f97316', '#d97706'],
19
+ confetti: ['#eab308', '#f97316', '#d97706', '#e11d48'],
20
+ motion: 'fireworks',
21
+ garland: 'diyas',
22
+ motif: '🪔',
23
+ badge: 'Shubh Deepavali',
24
+ pill: { border: "oklch(76.9% 0.188 70.08)", shadow: "oklch(76.9% 0.188 70.08 / 0.3)" },
25
+ line: "linear-gradient(to right in oklab, oklch(76.9% 0.188 70.08), oklch(70.5% 0.213 47.604), oklch(76.9% 0.188 70.08))",
26
+ },
27
+ dussehra: {
28
+ accent: '#ea580c',
29
+ palette: ['#ea580c', '#f59e0b', '#be123c'],
30
+ confetti: ['#f59e0b', '#ea580c', '#fbbf24', '#be123c'],
31
+ motion: 'petals',
32
+ garland: 'marigolds',
33
+ motif: '🏹',
34
+ badge: 'Happy Dussehra',
35
+ pill: { border: "oklch(70.5% 0.213 47.604)", shadow: "oklch(70.5% 0.213 47.604 / 0.3)" },
36
+ line: "linear-gradient(to right in oklab, oklch(64.6% 0.222 41.116), oklch(82.8% 0.189 84.429), oklch(64.6% 0.222 41.116))",
37
+ },
38
+ 'new-year': {
39
+ accent: '#ca8a04',
40
+ palette: ['#fbbf24', '#6366f1', '#fbbf24'],
41
+ confetti: ['#fbbf24', '#6366f1', '#f1f5f9', '#ca8a04'],
42
+ motion: 'cannons',
43
+ garland: 'shimmer',
44
+ motif: '✨',
45
+ badge: 'Happy New Year',
46
+ pill: { border: "oklch(79.5% 0.184 86.047)", shadow: "oklch(79.5% 0.184 86.047 / 0.3)" },
47
+ line: "linear-gradient(to right in oklab, oklch(79.5% 0.184 86.047), oklch(58.5% 0.233 277.117), oklch(79.5% 0.184 86.047))",
48
+ },
49
+ 'republic-day': {
50
+ accent: '#f97316',
51
+ palette: ['#f97316', '#1e3a8a', '#16a34a'],
52
+ confetti: ['#f97316', '#ffffff', '#16a34a'],
53
+ motion: 'tricolour',
54
+ garland: 'silk',
55
+ motif: null,
56
+ badge: 'Happy Republic Day',
57
+ pill: { border: "oklch(75% 0.183 55.934)", shadow: "oklch(70.5% 0.213 47.604 / 0.2)" },
58
+ line: TRICOLOUR,
59
+ },
60
+ 'independence-day': {
61
+ accent: '#16a34a',
62
+ palette: ['#f97316', '#ffffff', '#16a34a'],
63
+ confetti: ['#f97316', '#ffffff', '#16a34a'],
64
+ motion: 'tricolour',
65
+ garland: 'silk',
66
+ motif: null,
67
+ badge: 'Independence Day',
68
+ pill: { border: "oklch(62.7% 0.194 149.214)", shadow: "oklch(62.7% 0.194 149.214 / 0.2)" },
69
+ line: TRICOLOUR,
70
+ },
71
+ holi: {
72
+ accent: '#db2777',
73
+ palette: ['#ec4899', '#eab308', '#06b6d4', '#8b5cf6'],
74
+ confetti: ['#ec4899', '#eab308', '#06b6d4', '#8b5cf6'],
75
+ motion: 'clouds',
76
+ garland: 'iridescent',
77
+ motif: '🌸',
78
+ badge: 'Happy Holi',
79
+ pill: { border: "transparent", shadow: "oklch(65.6% 0.241 354.308 / 0.25)" },
80
+ line: "",
81
+ },
82
+ };
83
+ // "christmas" or "christmas-2026"
84
+ export function festivalFromId(id, isPreview = false, today = new Date()) {
85
+ const [, kind = '', statedYear] = id.match(/^([a-z-]+?)(?:-(\d{4}))?$/) ?? [];
86
+ // Late in the year, New Year means the coming one
87
+ const isComing = kind === 'new-year' && today.getMonth() >= 6;
88
+ const year = statedYear || String(today.getFullYear() + (isComing ? 1 : 0));
89
+ if (!Object.prototype.hasOwnProperty.call(FESTIVALS, kind))
90
+ return null;
91
+ const theme = FESTIVALS[kind];
92
+ return {
93
+ ...theme,
94
+ id: `${kind}-${year}`,
95
+ kind: kind,
96
+ badge: kind === 'new-year' ? `${theme.badge} ${year}` : theme.badge,
97
+ isPreview,
98
+ };
99
+ }
100
+ // The festival in a Festival API reply, if it is one of ours and really covers the date
101
+ export function activeFromReply(reply, date) {
102
+ const f = reply?.festival;
103
+ if (!f || typeof f.id !== 'string' || typeof f.start !== 'string' || typeof f.end !== 'string')
104
+ return null;
105
+ return f.start <= date && date <= f.end ? festivalFromId(f.id) : null;
106
+ }
107
+ export const dismissKey = (app, festivalId, today = new Date()) => `festive_dismissed_${app}_${festivalId}_${today.getFullYear()}`;
108
+ // ?festival=christmas, before or after the # in the address
109
+ export function previewFromAddress({ search, hash }) {
110
+ const id = new URLSearchParams(search).get('festival') || new URLSearchParams(hash.split('?')[1] || '').get('festival');
111
+ return id ? festivalFromId(id.trim(), true) : null;
112
+ }
@@ -0,0 +1 @@
1
+ export declare const css = "\n[class^=\"hsx-\"]{box-sizing:border-box}\n.hsx-backdrop{position:fixed;inset:0;z-index:0;overflow:hidden;pointer-events:none}\n.hsx-header-backdrop{position:absolute;inset:0;z-index:-10;pointer-events:none;clip-path:inset(0)}\n.hsx-surface{display:block;position:absolute;inset:0;width:100%;height:100%;max-width:none;border-radius:inherit;pointer-events:none}\n.hsx-behind{z-index:-10}\n.hsx-bloom{position:fixed;inset:0;pointer-events:none;transition:opacity 1s cubic-bezier(.4,0,.2,1)}\n.hsx-bloom-light{position:absolute;left:0;right:0;top:-40px;height:300px;filter:blur(64px)}\n.hsx-fill{position:absolute;inset:0}\n.hsx-garland{position:absolute;left:20px;right:20px;top:100%;margin-top:-2px;height:3px;pointer-events:none}\n.hsx-runner{position:absolute;inset:0;border-radius:9999px}\n.hsx-strand{position:absolute;left:0;right:0;top:0}\n.hsx-wire{display:block;position:absolute;left:0;top:0;max-width:none;overflow:visible}\n.hsx-wire-bulb{stroke:oklch(37.8% .077 168.94)}\n.hsx-wire-marigold{stroke:oklch(45.3% .124 130.933)}\nhtml[data-festive-theme=dark] .hsx-wire-bulb{stroke:oklch(50.8% .118 165.612)}\nhtml[data-festive-theme=dark] .hsx-wire-marigold{stroke:oklch(64.8% .2 131.684)}\n.hsx-hang{position:absolute;width:0;transform-origin:top}\n.hsx-diyas{position:absolute;left:4%;right:4%;top:-1px;display:flex;justify-content:space-between;line-height:24px}\n.hsx-diya{display:block;width:17px;height:17px;overflow:visible}\n.hsx-block{display:block}\n.hsx-wide-only{display:none}\n.hsx-badge{position:relative;display:flex;align-items:center;gap:4px;flex-shrink:0;padding:4px 6px 4px 10px;border:2px solid var(--hsx-border);border-radius:9999px;\nbackground-color:rgb(255 255 255 / .95);color:oklch(20.8% .042 265.755);box-shadow:0 20px 25px -5px var(--hsx-shadow),0 8px 10px -6px var(--hsx-shadow);\nbackdrop-filter:blur(24px);font-size:14px;line-height:20px;font-weight:600;white-space:nowrap}\nhtml[data-festive-theme=dark] .hsx-badge{background-color:oklch(20.8% .042 265.755 / .95);color:oklch(96.8% .007 247.896)}\n.hsx-badge button{margin:0;border:0;background:transparent;color:inherit;font:inherit;letter-spacing:inherit;text-transform:none;cursor:pointer;box-shadow:none;min-width:0;min-height:0}\n.hsx-badge .hsx-celebrate{position:relative;display:flex;align-items:center;gap:10px;padding:2px 0;border-radius:9999px;transition:transform .15s cubic-bezier(.4,0,.2,1)}\n.hsx-celebrate:focus-visible{outline:2px solid currentColor}\n.hsx-celebrate:active{scale:.97}\n.hsx-motif{font-size:16px;line-height:1}\n.hsx-flag{width:20px;height:14px;border-radius:3px;box-shadow:0 0 0 1px rgb(0 0 0 / .2);background:linear-gradient(#f97316 33%,#fff 33% 67%,#16a34a 67%)}\n.hsx-greeting,.hsx-chip{display:none}\n.hsx-chip{padding:2px 8px;border-radius:9999px;font-size:12px;line-height:16px;background-color:oklch(20.8% .042 265.755 / .05);transition:background-color .15s cubic-bezier(.4,0,.2,1)}\n.hsx-celebrate:hover .hsx-chip{background-color:oklch(20.8% .042 265.755 / .1)}\nhtml[data-festive-theme=dark] .hsx-chip{background-color:rgb(255 255 255 / .1)}\nhtml[data-festive-theme=dark] .hsx-celebrate:hover .hsx-chip{background-color:rgb(255 255 255 / .2)}\n.hsx-badge .hsx-dismiss{position:relative;padding:6px;border-radius:9999px;opacity:.6;transition:color .15s,background-color .15s}\n.hsx-badge .hsx-dismiss:hover{opacity:1;background-color:rgb(0 0 0 / .1)}\nhtml[data-festive-theme=dark] .hsx-badge .hsx-dismiss:hover{background-color:rgb(255 255 255 / .15)}\n.hsx-dismiss svg{display:block;width:14px;height:14px}\n@media (min-width:640px){.hsx-wide-only{display:block}}\n@media (min-width:1024px){.hsx-badge{padding-left:16px}.hsx-greeting{display:inline}}\n@media (min-width:1280px){.hsx-chip{display:inline}}\n@media print{.hsx-backdrop,.hsx-header-backdrop,.hsx-surface,.hsx-garland{display:none}}\n.hsx-aurora {\n animation: hsx-breathe 4s ease-in-out 3;\n}\n/* Softens where the sidebar's part of the celebration begins and ends */\n.festive-fade {\n mask-image: linear-gradient(transparent, #000 40px, #000 calc(100% - 40px), transparent);\n}\n.hsx-star {\n position: absolute;\n clip-path: polygon(50% 0, 60% 40%, 100% 50%, 60% 60%, 50% 100%, 40% 60%, 0 50%, 40% 40%);\n opacity: 0.35;\n animation: hsx-twinkle 6s ease-in-out infinite;\n}\n\n/* The line under the top bar */\n.hsx-runner[data-garland='diyas'] {\n box-shadow: 0 1px 10px 1px rgb(245 158 11 / 0.75);\n}\n.hsx-runner::after {\n content: '';\n position: absolute;\n inset: 0;\n border-radius: 9999px;\n}\n.hsx-runner[data-garland='diyas']::after {\n box-shadow: 0 0 24px 6px rgb(249 115 22 / 0.8);\n animation: hsx-glow 2.6s ease-in-out infinite;\n}\n.hsx-runner[data-garland='shimmer'] {\n box-shadow: 0 1px 8px rgb(202 138 4 / 0.45);\n}\n.hsx-runner[data-garland='shimmer'],\n.hsx-runner[data-garland='iridescent'] {\n overflow: hidden;\n}\n.hsx-runner[data-garland='shimmer']::after {\n width: 25%;\n background: linear-gradient(90deg, transparent, rgb(255 255 255 / 0.9), transparent);\n transform: translateX(-110%);\n animation: hsx-glint 4s ease-in-out infinite;\n}\n.hsx-runner[data-garland='iridescent'] {\n box-shadow: 0 1px 8px rgb(236 72 153 / 0.35);\n}\n/* The colours run twice across double the width, so every position shows the whole range */\n.hsx-runner[data-garland='iridescent']::after {\n width: 200%;\n background: linear-gradient(90deg, #f472b6, #fbbf24, #22d3ee, #a78bfa, #f472b6, #fbbf24, #22d3ee, #a78bfa, #f472b6);\n animation: hsx-shift 8s linear infinite;\n}\n\n/* Fairy lights: --lit (0 to 1) is set by the pointer. Centred on the wire, which has no width */\n.hsx-bulb,\n.hsx-marigold {\n position: absolute;\n left: 0;\n translate: -50% 0;\n}\n.hsx-bulb {\n top: 2px;\n width: 7px;\n height: 10px;\n border-radius: 42% 42% 50% 50%;\n background: radial-gradient(circle at 35% 30%, rgb(255 255 255 / 0.85), transparent 45%), currentColor;\n filter: drop-shadow(0 0 7px currentColor);\n animation: hsx-breathe-light 2.4s ease-in-out infinite;\n}\nhtml:not([data-festive-theme=dark]) .hsx-bulb {\n box-shadow: inset 0 0 0 0.75px rgb(15 23 42 / 0.28);\n}\n.hsx-bulb::before {\n content: '';\n position: absolute;\n left: 50%;\n top: -2.5px;\n width: 3.5px;\n height: 3.5px;\n margin-left: -1.75px;\n border-radius: 1px;\n background: #14532d;\n}\n.hsx-bulb::after {\n content: '';\n position: absolute;\n inset: 0;\n border-radius: inherit;\n background: radial-gradient(circle, #fff, transparent 75%);\n box-shadow: 0 0 14px 5px currentColor;\n opacity: var(--lit, 0);\n}\n.hsx-marigold {\n top: -1px;\n width: 11px;\n height: 11px;\n border-radius: 50%;\n background: radial-gradient(circle, #fde047 0 18%, #f59e0b 20% 48%, #ea580c 52% 62%, #f97316 64%);\n box-shadow: 0 1px 4px rgb(194 65 12 / 0.5), 0 0 calc(var(--lit, 0) * 14px) calc(var(--lit, 0) * 4px) #f59e0b;\n}\n/* Every third is a pair of mango leaves */\nspan:nth-child(3n) > .hsx-marigold {\n top: 1px;\n width: 6px;\n height: 13px;\n border-radius: 0 100% 0 100%;\n rotate: 45deg;\n background: linear-gradient(135deg, #4d7c0f, #84cc16);\n}\n\n.hsx-diya {\n filter: drop-shadow(0 0 5px rgb(245 158 11 / 0.9));\n}\n.hsx-diya-flame {\n transform-box: fill-box;\n transform-origin: 50% 100%;\n animation: hsx-flicker 1.5s ease-in-out infinite;\n}\n\n/* A band of silk: a wave cut out of the tricolour, drifting, with a highlight passing over it */\n.hsx-silk {\n position: absolute;\n inset: -3px 0 auto;\n height: 10px;\n filter: drop-shadow(0 1px 1px rgb(15 23 42 / 0.35)) drop-shadow(0 2px 5px rgb(249 115 22 / 0.3));\n}\n.hsx-silk {\n overflow: hidden;\n}\n/* The cut-out drifts one wavelength while the cloth inside moves back by as much, so the colours stay put */\n.hsx-silk-wave {\n position: absolute;\n inset: 0 0 0 -120px;\n overflow: hidden;\n mask: url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='10'%3E%3Cpath d='M-30 5 Q0 1 30 5 T90 5 T150 5' fill='none' stroke='%23000' stroke-width='5'/%3E%3C/svg%3E\") repeat-x 0 0 / 120px 10px;\n animation: hsx-wave 5s linear infinite;\n}\n.hsx-silk-cloth {\n position: absolute;\n inset: 0 0 0 120px;\n overflow: visible;\n animation: hsx-wave-back 5s linear infinite;\n}\n.hsx-silk-cloth::after {\n content: '';\n position: absolute;\n inset: 0;\n width: 40%;\n background: linear-gradient(90deg, transparent, rgb(255 255 255 / 0.75), transparent);\n transform: translateX(-100%);\n animation: hsx-silk-shine 5s linear infinite;\n}\n\n/* Metric cards pick up the season on hover, at night only */\nhtml[data-festival] .festive-card {\n transition: border-color 300ms, box-shadow 300ms;\n}\nhtml[data-festive-theme=dark] .festive-card:hover {\n border-color: color-mix(in srgb, var(--festive-accent) 45%, transparent);\n box-shadow: 0 4px 20px color-mix(in srgb, var(--festive-accent) 16%, transparent);\n}\n\n.hsx-badge[data-festival='holi'] {\n border-color: transparent;\n background: linear-gradient(#fff, #fff) padding-box,\n linear-gradient(90deg, #ec4899, #eab308, #06b6d4, #8b5cf6) border-box;\n}\nhtml[data-festive-theme=dark] .hsx-badge[data-festival='holi'] {\n background: linear-gradient(#0f172a, #0f172a) padding-box,\n linear-gradient(90deg, #f472b6, #facc15, #22d3ee, #a78bfa) border-box;\n}\n.hsx-badge[data-festival='diwali'] .hsx-motif {\n animation: hsx-flame 2.4s ease-in-out infinite;\n}\n.hsx-shimmer {\n position: absolute;\n inset: 0;\n overflow: hidden;\n border-radius: 9999px;\n pointer-events: none;\n}\n.hsx-shimmer::before {\n content: '';\n position: absolute;\n inset: 0;\n background: linear-gradient(110deg, transparent 30%, color-mix(in srgb, var(--festive-accent) 35%, transparent) 50%, transparent 70%);\n transform: translateX(-110%);\n animation: hsx-sweep 1.6s ease-out 0.4s 2;\n}\n\n@keyframes hsx-breathe {\n 50% { transform: translateX(5%) scaleY(1.25); }\n}\n@keyframes hsx-twinkle {\n 0%, 100% { opacity: 0.15; transform: translateY(0) scale(0.7); }\n 50% { opacity: 0.8; transform: translateY(10px) scale(1); }\n}\n@keyframes hsx-glow {\n 0%, 100% { opacity: 1; }\n 50% { opacity: 0.35; }\n}\n@keyframes hsx-glint {\n 60%, 100% { transform: translateX(430%); }\n}\n@keyframes hsx-shift {\n to { transform: translateX(-50%); }\n}\n@keyframes hsx-breathe-light {\n 0%, 100% { opacity: 1; }\n 50% { opacity: 0.7; }\n}\n@keyframes hsx-flicker {\n 0%, 100% { transform: scale(1, 1) skewX(0); }\n 25% { transform: scale(0.94, 1.1) skewX(3deg); }\n 55% { transform: scale(1.05, 0.93) skewX(-3deg); }\n 80% { transform: scale(0.97, 1.05) skewX(2deg); }\n}\n@keyframes hsx-wave {\n to { transform: translateX(120px); }\n}\n@keyframes hsx-wave-back {\n to { transform: translateX(-120px); }\n}\n@keyframes hsx-silk-shine {\n to { transform: translateX(260%); }\n}\n@keyframes hsx-flame {\n 50% { filter: drop-shadow(0 0 5px #f59e0b); }\n}\n@keyframes hsx-sweep {\n to { transform: translateX(110%); }\n}\n@media (prefers-reduced-motion: reduce) {\n .hsx-aurora,\n .hsx-star,\n .hsx-runner,\n .hsx-runner::after,\n .hsx-bulb,\n .hsx-diya-flame,\n .hsx-silk-wave,\n .hsx-silk-cloth,\n .hsx-silk-cloth::after,\n .hsx-badge .hsx-motif,\n .hsx-shimmer::before { animation: none !important; }\n}\n";