@dicebear/core 10.7.0 → 11.0.0-rc.1

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.
package/README.md CHANGED
@@ -11,7 +11,7 @@ PRNG and rendering pipeline, producing identical SVG output for the same seed,
11
11
  style, and options, regardless of the language used.
12
12
 
13
13
  [Playground](https://www.dicebear.com/playground) |
14
- [Documentation](https://www.dicebear.com/how-to-use/js-library/)
14
+ [Documentation](https://www.dicebear.com/integrations/javascript/)
15
15
 
16
16
  ## Installation
17
17
 
package/lib/Options.d.ts CHANGED
@@ -25,6 +25,12 @@ export declare class Options<D = unknown> {
25
25
  rotate(): Range | undefined;
26
26
  translateX(): Range | undefined;
27
27
  translateY(): Range | undefined;
28
+ /**
29
+ * Returns the animation switch as given for booleans, and normalizes a
30
+ * single animation name to a one-element array.
31
+ */
32
+ animation(): boolean | readonly string[] | undefined;
33
+ animationSpeed(): Range | undefined;
28
34
  /**
29
35
  * Returns the global `tags` filter as parsed tokens, or an empty array when
30
36
  * unset. Each raw token (`category` / `category:value`, optionally
package/lib/Options.js CHANGED
@@ -65,6 +65,20 @@ export class Options {
65
65
  translateY() {
66
66
  return __classPrivateFieldGet(this, _Options_instances, "m", _Options_toRange).call(this, __classPrivateFieldGet(this, _Options_data, "f").translateY);
67
67
  }
68
+ /**
69
+ * Returns the animation switch as given for booleans, and normalizes a
70
+ * single animation name to a one-element array.
71
+ */
72
+ animation() {
73
+ const raw = __classPrivateFieldGet(this, _Options_data, "f").animation;
74
+ if (raw === undefined || typeof raw === 'boolean') {
75
+ return raw;
76
+ }
77
+ return __classPrivateFieldGet(this, _Options_instances, "m", _Options_asArray).call(this, raw);
78
+ }
79
+ animationSpeed() {
80
+ return __classPrivateFieldGet(this, _Options_instances, "m", _Options_toRange).call(this, __classPrivateFieldGet(this, _Options_data, "f").animationSpeed);
81
+ }
68
82
  /**
69
83
  * Returns the global `tags` filter as parsed tokens, or an empty array when
70
84
  * unset. Each raw token (`category` / `category:value`, optionally
@@ -30,7 +30,11 @@ interface RangeField {
30
30
  readonly min?: number;
31
31
  readonly max?: number;
32
32
  }
33
- export type FieldDescriptor = StringField | NumberField | BooleanField | EnumField | ColorField | RangeField;
33
+ interface AnimationField {
34
+ readonly type: 'animation';
35
+ readonly values: readonly string[];
36
+ }
37
+ export type FieldDescriptor = StringField | NumberField | BooleanField | EnumField | ColorField | RangeField | AnimationField;
34
38
  export type Descriptor = Record<string, FieldDescriptor>;
35
39
  /**
36
40
  * Builds a descriptor of every option a given style accepts. Tooling such as
@@ -107,6 +107,15 @@ _a = OptionsDescriptor, _OptionsDescriptor_descriptor = new WeakMap(), _OptionsD
107
107
  open: true,
108
108
  };
109
109
  }
110
+ // Only advertise the animation options when the style carries declarative
111
+ // animations — on a static style both are accepted but have no effect.
112
+ if (__classPrivateFieldGet(this, _OptionsDescriptor_style, "f").hasAnimations()) {
113
+ result.animation = {
114
+ type: 'animation',
115
+ values: __classPrivateFieldGet(this, _OptionsDescriptor_style, "f").animationNames(),
116
+ };
117
+ result.animationSpeed = { type: 'range', min: 0.1, max: 10 };
118
+ }
110
119
  return result;
111
120
  };
112
121
  _OptionsDescriptor_rotateRange = { value: { type: 'range', min: -360, max: 360 } };
package/lib/Renderer.js CHANGED
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _Renderer_instances, _Renderer_style, _Renderer_resolver, _Renderer_defs, _Renderer_cachedSeedHash, _Renderer_cachedInitials, _Renderer_applyFlip, _Renderer_applyScale, _Renderer_applyBorderRadius, _Renderer_applyRotate, _Renderer_applyTranslate, _Renderer_renderBackground, _Renderer_randomizeIds, _Renderer_renderElements, _Renderer_renderElement, _Renderer_renderSvgElement, _Renderer_renderTextElement, _Renderer_renderComponentElement, _Renderer_buildTransforms, _Renderer_renderAttributes, _Renderer_resolveAttributeValue, _Renderer_resolveColorReference, _Renderer_buildGradientDef, _Renderer_resolveValue, _Renderer_resolveVariable, _Renderer_initials, _Renderer_hashSeed;
12
+ var _Renderer_instances, _a, _Renderer_trackOrder, _Renderer_directionCss, _Renderer_easingKeywordCss, _Renderer_style, _Renderer_resolver, _Renderer_defs, _Renderer_cachedSeedHash, _Renderer_cachedInitials, _Renderer_cachedAnimationHash, _Renderer_cachedRandomSuffix, _Renderer_keyframesCss, _Renderer_animationCss, _Renderer_keyframesByContent, _Renderer_keyframesCounter, _Renderer_animationClassCounter, _Renderer_applyFlip, _Renderer_applyScale, _Renderer_applyBorderRadius, _Renderer_applyRotate, _Renderer_applyTranslate, _Renderer_renderBackground, _Renderer_randomSuffix, _Renderer_randomizeIds, _Renderer_renderElements, _Renderer_renderElement, _Renderer_renderSvgElement, _Renderer_renderTextElement, _Renderer_renderComponentElement, _Renderer_buildTransforms, _Renderer_renderAttributes, _Renderer_resolveAttributeValue, _Renderer_resolveColorReference, _Renderer_buildGradientDef, _Renderer_resolveValue, _Renderer_resolveVariable, _Renderer_initials, _Renderer_hashSeed, _Renderer_animationHash, _Renderer_activeAnimations, _Renderer_attributesWithoutAnimatedOpacity, _Renderer_applyAnimations, _Renderer_buildAnimationCss, _Renderer_keyframesBody, _Renderer_trackDeclaration, _Renderer_easingCss, _Renderer_registerAnimationStyle;
13
13
  import { Fnv1a } from './Prng/Fnv1a.js';
14
14
  import { Initials } from './Utils/Initials.js';
15
15
  import { License } from './Utils/License.js';
@@ -29,6 +29,13 @@ export class Renderer {
29
29
  _Renderer_defs.set(this, new Map());
30
30
  _Renderer_cachedSeedHash.set(this, void 0);
31
31
  _Renderer_cachedInitials.set(this, void 0);
32
+ _Renderer_cachedAnimationHash.set(this, void 0);
33
+ _Renderer_cachedRandomSuffix.set(this, void 0);
34
+ _Renderer_keyframesCss.set(this, []);
35
+ _Renderer_animationCss.set(this, []);
36
+ _Renderer_keyframesByContent.set(this, new Map());
37
+ _Renderer_keyframesCounter.set(this, 0);
38
+ _Renderer_animationClassCounter.set(this, 0);
32
39
  __classPrivateFieldSet(this, _Renderer_style, style, "f");
33
40
  __classPrivateFieldSet(this, _Renderer_resolver, resolver, "f");
34
41
  }
@@ -46,6 +53,7 @@ export class Renderer {
46
53
  body = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyRotate).call(this, body, canvas);
47
54
  body = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyTranslate).call(this, body, canvas);
48
55
  body = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyBorderRadius).call(this, `${background}${body}`, canvas);
56
+ __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_registerAnimationStyle).call(this);
49
57
  const metadata = License.xml(__classPrivateFieldGet(this, _Renderer_style, "f").meta());
50
58
  const defs = __classPrivateFieldGet(this, _Renderer_defs, "f").size > 0
51
59
  ? `<defs>${Array.from(__classPrivateFieldGet(this, _Renderer_defs, "f").values()).join('')}</defs>`
@@ -79,7 +87,7 @@ export class Renderer {
79
87
  return svg;
80
88
  }
81
89
  }
82
- _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_defs = new WeakMap(), _Renderer_cachedSeedHash = new WeakMap(), _Renderer_cachedInitials = new WeakMap(), _Renderer_instances = new WeakSet(), _Renderer_applyFlip = function _Renderer_applyFlip(body, canvas) {
90
+ _a = Renderer, _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_defs = new WeakMap(), _Renderer_cachedSeedHash = new WeakMap(), _Renderer_cachedInitials = new WeakMap(), _Renderer_cachedAnimationHash = new WeakMap(), _Renderer_cachedRandomSuffix = new WeakMap(), _Renderer_keyframesCss = new WeakMap(), _Renderer_animationCss = new WeakMap(), _Renderer_keyframesByContent = new WeakMap(), _Renderer_keyframesCounter = new WeakMap(), _Renderer_animationClassCounter = new WeakMap(), _Renderer_instances = new WeakSet(), _Renderer_applyFlip = function _Renderer_applyFlip(body, canvas) {
83
91
  const flip = __classPrivateFieldGet(this, _Renderer_resolver, "f").flip();
84
92
  if (flip === 'none') {
85
93
  return body;
@@ -137,10 +145,12 @@ _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_d
137
145
  return '';
138
146
  }
139
147
  return `<rect width="${Number.format(canvas.width())}" height="${Number.format(canvas.height())}" fill="${Xml.escape(__classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_resolveColorReference).call(this, 'background'))}"/>`;
140
- }, _Renderer_randomizeIds = function _Renderer_randomizeIds(svg) {
141
- const suffix = Math.floor(Math.random() * 0xffffff)
148
+ }, _Renderer_randomSuffix = function _Renderer_randomSuffix() {
149
+ return (__classPrivateFieldSet(this, _Renderer_cachedRandomSuffix, __classPrivateFieldGet(this, _Renderer_cachedRandomSuffix, "f") ?? Math.floor(Math.random() * 0xffffff)
142
150
  .toString(16)
143
- .padStart(6, '0');
151
+ .padStart(6, '0'), "f"));
152
+ }, _Renderer_randomizeIds = function _Renderer_randomizeIds(svg) {
153
+ const suffix = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_randomSuffix).call(this);
144
154
  const ids = new Set();
145
155
  for (const match of svg.matchAll(/\bid="([^"]+)"/g)) {
146
156
  ids.add(match[1]);
@@ -178,7 +188,7 @@ _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_d
178
188
  }
179
189
  return '';
180
190
  }
181
- const attrs = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_renderAttributes).call(this, element.attributes());
191
+ const attrs = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_renderAttributes).call(this, __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_attributesWithoutAnimatedOpacity).call(this, element.attributes(), element));
182
192
  const children = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_renderElements).call(this, element.children());
183
193
  if (children.length === 0) {
184
194
  // A wrapper whose children all rendered to nothing, because an optional
@@ -190,9 +200,9 @@ _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_d
190
200
  element.attributes()?.id === undefined) {
191
201
  return '';
192
202
  }
193
- return `<${name}${attrs}/>`;
203
+ return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<${name}${attrs}/>`, element);
194
204
  }
195
- return `<${name}${attrs}>${children}</${name}>`;
205
+ return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<${name}${attrs}>${children}</${name}>`, element);
196
206
  }, _Renderer_renderTextElement = function _Renderer_renderTextElement(element) {
197
207
  const value = element.value();
198
208
  return value !== undefined ? Xml.escape(__classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_resolveValue).call(this, value)) : '';
@@ -219,7 +229,7 @@ _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_d
219
229
  __classPrivateFieldGet(this, _Renderer_defs, "f").set(id, `<g id="${id}">${body}</g>`);
220
230
  }
221
231
  const transforms = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_buildTransforms).call(this, component);
222
- const userAttributes = element.attributes();
232
+ const userAttributes = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_attributesWithoutAnimatedOpacity).call(this, element.attributes(), element);
223
233
  let mergedAttributes = userAttributes;
224
234
  if (transforms.length > 0) {
225
235
  const userTransform = userAttributes?.transform;
@@ -229,7 +239,7 @@ _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_d
229
239
  mergedAttributes = { ...userAttributes, transform: allParts.join(' ') };
230
240
  }
231
241
  const attrs = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_renderAttributes).call(this, mergedAttributes);
232
- return `<use${attrs} href="#${id}"/>`;
242
+ return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<use${attrs} href="#${id}"/>`, element);
233
243
  }, _Renderer_buildTransforms = function _Renderer_buildTransforms(component) {
234
244
  const { rotate, translateX, translateY, scale } = __classPrivateFieldGet(this, _Renderer_resolver, "f").componentTransform(component.name());
235
245
  if (translateX === 0 && translateY === 0 && rotate === 0 && scale === 1) {
@@ -323,4 +333,186 @@ _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_d
323
333
  return (__classPrivateFieldSet(this, _Renderer_cachedInitials, __classPrivateFieldGet(this, _Renderer_cachedInitials, "f") ?? Initials.fromSeed(__classPrivateFieldGet(this, _Renderer_resolver, "f").seed()), "f"));
324
334
  }, _Renderer_hashSeed = function _Renderer_hashSeed() {
325
335
  return (__classPrivateFieldSet(this, _Renderer_cachedSeedHash, __classPrivateFieldGet(this, _Renderer_cachedSeedHash, "f") ?? Fnv1a.hex((__classPrivateFieldGet(this, _Renderer_style, "f").meta().source().name() ?? '') + ':' + __classPrivateFieldGet(this, _Renderer_resolver, "f").seed()), "f"));
336
+ }, _Renderer_animationHash = function _Renderer_animationHash() {
337
+ const selection = __classPrivateFieldGet(this, _Renderer_resolver, "f").animation();
338
+ const names = Array.isArray(selection)
339
+ ? ':' + [...new Set(selection)].sort().join(',')
340
+ : '';
341
+ const random = __classPrivateFieldGet(this, _Renderer_resolver, "f").idRandomization()
342
+ ? ':' + __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_randomSuffix).call(this)
343
+ : '';
344
+ return (__classPrivateFieldSet(this, _Renderer_cachedAnimationHash, __classPrivateFieldGet(this, _Renderer_cachedAnimationHash, "f") ?? Fnv1a.hex((__classPrivateFieldGet(this, _Renderer_style, "f").meta().source().name() ?? '') +
345
+ ':' +
346
+ __classPrivateFieldGet(this, _Renderer_resolver, "f").seed() +
347
+ ':' +
348
+ Number.format(__classPrivateFieldGet(this, _Renderer_resolver, "f").animationSpeed()) +
349
+ names +
350
+ random), "f"));
351
+ }, _Renderer_activeAnimations = function _Renderer_activeAnimations(element) {
352
+ const animations = element.animations();
353
+ if (animations.length === 0) {
354
+ return animations;
355
+ }
356
+ const selection = __classPrivateFieldGet(this, _Renderer_resolver, "f").animation();
357
+ // `true` plays every timeline. A name array plays only the timelines
358
+ // carrying one of those names, so unnamed timelines stay static then.
359
+ return selection === true
360
+ ? animations
361
+ : selection === false
362
+ ? []
363
+ : animations.filter((animation) => animation.name !== undefined &&
364
+ selection.includes(animation.name));
365
+ }, _Renderer_attributesWithoutAnimatedOpacity = function _Renderer_attributesWithoutAnimatedOpacity(attributes, element) {
366
+ if (attributes?.opacity === undefined ||
367
+ element.animations().length === 0) {
368
+ return attributes;
369
+ }
370
+ const active = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_activeAnimations).call(this, element);
371
+ if (!active.some((animation) => animation.tracks.opacity !== undefined)) {
372
+ return attributes;
373
+ }
374
+ const rest = { ...attributes };
375
+ delete rest.opacity;
376
+ return rest;
377
+ }, _Renderer_applyAnimations = function _Renderer_applyAnimations(markup, element) {
378
+ if (markup.length === 0) {
379
+ return markup;
380
+ }
381
+ const active = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_activeAnimations).call(this, element);
382
+ if (active.length === 0) {
383
+ return markup;
384
+ }
385
+ const classes = [];
386
+ let opacityWrapper = -1;
387
+ for (const animation of active) {
388
+ for (const track of __classPrivateFieldGet(_a, _a, "f", _Renderer_trackOrder)) {
389
+ const trackData = animation.tracks[track];
390
+ if (trackData !== undefined) {
391
+ if (track === 'opacity') {
392
+ opacityWrapper = classes.length;
393
+ }
394
+ classes.push(__classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_buildAnimationCss).call(this, animation, track, trackData.keyframes));
395
+ }
396
+ }
397
+ }
398
+ const restingOpacity = element.attributes()?.opacity;
399
+ let result = markup;
400
+ for (let i = classes.length - 1; i >= 0; i--) {
401
+ const opacity = i === opacityWrapper && restingOpacity !== undefined
402
+ ? __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_renderAttributes).call(this, { opacity: restingOpacity })
403
+ : '';
404
+ result = `<g class="${classes[i]}"${opacity}>${result}</g>`;
405
+ }
406
+ return result;
407
+ }, _Renderer_buildAnimationCss = function _Renderer_buildAnimationCss(animation, track, keyframes) {
408
+ var _b, _c, _d, _e;
409
+ const defaultEasing = animation.easing ?? 'linear';
410
+ const body = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_keyframesBody).call(this, track, keyframes, defaultEasing);
411
+ let keyframesName = __classPrivateFieldGet(this, _Renderer_keyframesByContent, "f").get(body);
412
+ if (keyframesName === undefined) {
413
+ keyframesName = `dbk-${__classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_animationHash).call(this)}-${__classPrivateFieldSet(this, _Renderer_keyframesCounter, (_c = __classPrivateFieldGet(this, _Renderer_keyframesCounter, "f"), _b = _c++, _c), "f"), _b}`;
414
+ __classPrivateFieldGet(this, _Renderer_keyframesByContent, "f").set(body, keyframesName);
415
+ __classPrivateFieldGet(this, _Renderer_keyframesCss, "f").push(`@keyframes ${keyframesName}{${body}}`);
416
+ }
417
+ const speed = __classPrivateFieldGet(this, _Renderer_resolver, "f").animationSpeed();
418
+ const duration = Number.format(animation.duration / speed);
419
+ const delay = Number.format((animation.delay ?? 0) / speed);
420
+ const iterations = animation.iterations === undefined || animation.iterations === 'infinite'
421
+ ? 'infinite'
422
+ : Number.format(animation.iterations);
423
+ const direction = __classPrivateFieldGet(_a, _a, "f", _Renderer_directionCss)[animation.direction ?? 'normal'];
424
+ const fill = animation.fill ?? 'none';
425
+ // Only rotate and scale pivot around a point. Translation and opacity
426
+ // need no origin, so their rules skip the transform-box prefix.
427
+ const needsOrigin = track === 'rotate' || track === 'scaleX' || track === 'scaleY';
428
+ const origin = animation.origin ?? { x: 50, y: 50 };
429
+ const originCss = needsOrigin
430
+ ? `transform-box:fill-box;transform-origin:${Number.format(origin.x)}% ${Number.format(origin.y)}%;`
431
+ : '';
432
+ const className = `dba-${__classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_animationHash).call(this)}-${__classPrivateFieldSet(this, _Renderer_animationClassCounter, (_e = __classPrivateFieldGet(this, _Renderer_animationClassCounter, "f"), _d = _e++, _e), "f"), _d}`;
433
+ // The name comes last in the shorthand so it can never be mistaken for a
434
+ // keyword. All seven tokens are always emitted so every port serializes
435
+ // the same string.
436
+ __classPrivateFieldGet(this, _Renderer_animationCss, "f").push(`.${className}{${originCss}animation:${duration}s ${__classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_easingCss).call(this, defaultEasing)} ${delay}s ${iterations} ${direction} ${fill} ${keyframesName}}`);
437
+ return className;
438
+ }, _Renderer_keyframesBody = function _Renderer_keyframesBody(track, keyframes, defaultEasing) {
439
+ const list = [...keyframes];
440
+ if (list[0].at > 0) {
441
+ list.unshift({ at: 0, value: list[0].value });
442
+ }
443
+ const last = list[list.length - 1];
444
+ if (last.at < 100) {
445
+ list.push({ at: 100, value: last.value });
446
+ }
447
+ const defaultCss = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_easingCss).call(this, defaultEasing);
448
+ return list
449
+ .map((keyframe, index) => {
450
+ const easingCss = keyframe.easing !== undefined && index < list.length - 1
451
+ ? __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_easingCss).call(this, keyframe.easing)
452
+ : defaultCss;
453
+ const timing = easingCss !== defaultCss
454
+ ? `;animation-timing-function:${easingCss}`
455
+ : '';
456
+ return `${Number.format(keyframe.at)}%{${__classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_trackDeclaration).call(this, track, keyframe.value)}${timing}}`;
457
+ })
458
+ .join('');
459
+ }, _Renderer_trackDeclaration = function _Renderer_trackDeclaration(track, value) {
460
+ const v = Number.format(value);
461
+ switch (track) {
462
+ case 'translateX':
463
+ return `transform:translateX(${v}px)`;
464
+ case 'translateY':
465
+ return `transform:translateY(${v}px)`;
466
+ case 'rotate':
467
+ return `transform:rotate(${v}deg)`;
468
+ case 'scaleX':
469
+ return `transform:scaleX(${v})`;
470
+ case 'scaleY':
471
+ return `transform:scaleY(${v})`;
472
+ case 'opacity':
473
+ return `opacity:${v}`;
474
+ }
475
+ }, _Renderer_easingCss = function _Renderer_easingCss(easing) {
476
+ if (typeof easing === 'string') {
477
+ return __classPrivateFieldGet(_a, _a, "f", _Renderer_easingKeywordCss)[easing];
478
+ }
479
+ return `cubic-bezier(${Number.format(easing.x1)}, ${Number.format(easing.y1)}, ${Number.format(easing.x2)}, ${Number.format(easing.y2)})`;
480
+ }, _Renderer_registerAnimationStyle = function _Renderer_registerAnimationStyle() {
481
+ if (__classPrivateFieldGet(this, _Renderer_keyframesCss, "f").length === 0) {
482
+ return;
483
+ }
484
+ // An authored def may carry the id `animation-style`, so the CSS takes
485
+ // the next free key rather than replacing that def.
486
+ let key = 'animation-style';
487
+ while (__classPrivateFieldGet(this, _Renderer_defs, "f").has(key)) {
488
+ key += '-';
489
+ }
490
+ __classPrivateFieldGet(this, _Renderer_defs, "f").set(key, `<style>@media (prefers-reduced-motion:no-preference){${__classPrivateFieldGet(this, _Renderer_keyframesCss, "f").join('')}${__classPrivateFieldGet(this, _Renderer_animationCss, "f").join('')}}</style>`);
326
491
  };
492
+ /**
493
+ * The canonical track order, outermost wrapper to innermost. It mirrors the
494
+ * usual translate → rotate → scale transform composition. Every port must
495
+ * wrap in this exact order for the outputs to stay byte-identical.
496
+ */
497
+ _Renderer_trackOrder = { value: [
498
+ 'translateX',
499
+ 'translateY',
500
+ 'rotate',
501
+ 'scaleX',
502
+ 'scaleY',
503
+ 'opacity',
504
+ ] };
505
+ _Renderer_directionCss = { value: {
506
+ normal: 'normal',
507
+ reverse: 'reverse',
508
+ alternate: 'alternate',
509
+ alternateReverse: 'alternate-reverse',
510
+ } };
511
+ _Renderer_easingKeywordCss = { value: {
512
+ linear: 'linear',
513
+ ease: 'ease',
514
+ easeIn: 'ease-in',
515
+ easeOut: 'ease-out',
516
+ easeInOut: 'ease-in-out',
517
+ hold: 'step-end',
518
+ } };
package/lib/Resolver.d.ts CHANGED
@@ -16,6 +16,8 @@ export declare class Resolver<D = unknown> {
16
16
  seed(): string;
17
17
  size(): number | undefined;
18
18
  idRandomization(): boolean;
19
+ animation(): boolean | readonly string[];
20
+ animationSpeed(): number;
19
21
  title(): string | undefined;
20
22
  flip(): StyleOptionsFlipValue;
21
23
  fontFamily(): string;
package/lib/Resolver.js CHANGED
@@ -47,6 +47,15 @@ export class Resolver {
47
47
  idRandomization() {
48
48
  return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, 'idRandomization', () => __classPrivateFieldGet(this, _Resolver_options, "f").idRandomization() ?? false);
49
49
  }
50
+ animation() {
51
+ // Deliberately without PRNG involvement — whether and what animates must
52
+ // not depend on the seed. `true` plays every timeline, a name array only
53
+ // the timelines carrying one of those names.
54
+ return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, 'animation', () => __classPrivateFieldGet(this, _Resolver_options, "f").animation() ?? false);
55
+ }
56
+ animationSpeed() {
57
+ return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memoFloat).call(this, 'animationSpeed', __classPrivateFieldGet(this, _Resolver_options, "f").animationSpeed(), 1);
58
+ }
50
59
  title() {
51
60
  return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, 'title', () => __classPrivateFieldGet(this, _Resolver_options, "f").title());
52
61
  }
@@ -1,4 +1,4 @@
1
- import type { StyleDefinitionElement, StyleDefinitionElementValue, StyleDefinitionElementType, StyleDefinitionAttributes } from '../StyleDefinition.js';
1
+ import type { StyleDefinitionElement, StyleDefinitionElementValue, StyleDefinitionElementType, StyleDefinitionAttributes, StyleDefinitionAnimation } from '../StyleDefinition.js';
2
2
  /**
3
3
  * Read-only view over a single render-tree element from a style definition.
4
4
  *
@@ -27,6 +27,11 @@ export declare class Element {
27
27
  * attributes are defined.
28
28
  */
29
29
  attributes(): StyleDefinitionAttributes | undefined;
30
+ /**
31
+ * Returns the element's declarative animation timelines. Empty for
32
+ * elements without animations.
33
+ */
34
+ animations(): readonly StyleDefinitionAnimation[];
30
35
  /**
31
36
  * Returns the element's children, lazily wrapped as {@link Element}
32
37
  * instances on first access.
@@ -49,6 +49,13 @@ export class Element {
49
49
  attributes() {
50
50
  return __classPrivateFieldGet(this, _Element_data, "f").attributes;
51
51
  }
52
+ /**
53
+ * Returns the element's declarative animation timelines. Empty for
54
+ * elements without animations.
55
+ */
56
+ animations() {
57
+ return __classPrivateFieldGet(this, _Element_data, "f").animations ?? [];
58
+ }
52
59
  /**
53
60
  * Returns the element's children, lazily wrapped as {@link Element}
54
61
  * instances on first access.
package/lib/Style.d.ts CHANGED
@@ -46,6 +46,21 @@ export declare class Style<D = unknown> {
46
46
  * lazily on first access.
47
47
  */
48
48
  components(): ReadonlyMap<string, Component>;
49
+ /**
50
+ * Returns whether any element in the definition carries declarative
51
+ * animations. Computed once and cached. Consumed by the options
52
+ * descriptor to advertise the `animation` options only where they have an
53
+ * effect.
54
+ */
55
+ hasAnimations(): boolean;
56
+ /**
57
+ * Returns the sorted distinct names of the definition's animation
58
+ * timelines. Computed once and cached. Consumed by the options descriptor
59
+ * so tooling can offer the by-name form of the `animation` option. Sorted
60
+ * so every port reports the same order regardless of how it walks the
61
+ * definition.
62
+ */
63
+ animationNames(): readonly string[];
49
64
  /**
50
65
  * Returns a name → {@link Color} map for all defined colors, built lazily
51
66
  * on first access.
package/lib/Style.js CHANGED
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _Style_instances, _a, _Style_data, _Style_meta, _Style_canvas, _Style_components, _Style_colors, _Style_validateAliases, _Style_isAlias;
12
+ var _Style_instances, _a, _Style_data, _Style_meta, _Style_canvas, _Style_components, _Style_colors, _Style_hasAnimations, _Style_animationNames, _Style_validateAliases, _Style_isAlias, _Style_validateAnimations, _Style_visitElements;
13
13
  import { StyleValidator } from './Validator/StyleValidator.js';
14
14
  import { StyleValidationError } from './Error/StyleValidationError.js';
15
15
  import { Meta } from './Style/Meta.js';
@@ -29,9 +29,12 @@ export class Style {
29
29
  _Style_canvas.set(this, void 0);
30
30
  _Style_components.set(this, void 0);
31
31
  _Style_colors.set(this, void 0);
32
+ _Style_hasAnimations.set(this, void 0);
33
+ _Style_animationNames.set(this, void 0);
32
34
  StyleValidator.validate(data);
33
35
  __classPrivateFieldSet(this, _Style_data, structuredClone(data), "f");
34
36
  __classPrivateFieldGet(this, _Style_instances, "m", _Style_validateAliases).call(this);
37
+ __classPrivateFieldGet(this, _Style_instances, "m", _Style_validateAnimations).call(this);
35
38
  }
36
39
  /**
37
40
  * Returns the definition's `$id`, or `undefined` when not set.
@@ -101,6 +104,45 @@ export class Style {
101
104
  __classPrivateFieldSet(this, _Style_components, map, "f");
102
105
  return __classPrivateFieldGet(this, _Style_components, "f");
103
106
  }
107
+ /**
108
+ * Returns whether any element in the definition carries declarative
109
+ * animations. Computed once and cached. Consumed by the options
110
+ * descriptor to advertise the `animation` options only where they have an
111
+ * effect.
112
+ */
113
+ hasAnimations() {
114
+ if (__classPrivateFieldGet(this, _Style_hasAnimations, "f") === undefined) {
115
+ let found = false;
116
+ __classPrivateFieldGet(this, _Style_instances, "m", _Style_visitElements).call(this, (element) => {
117
+ if (element.animations !== undefined && element.animations.length > 0) {
118
+ found = true;
119
+ }
120
+ });
121
+ __classPrivateFieldSet(this, _Style_hasAnimations, found, "f");
122
+ }
123
+ return __classPrivateFieldGet(this, _Style_hasAnimations, "f");
124
+ }
125
+ /**
126
+ * Returns the sorted distinct names of the definition's animation
127
+ * timelines. Computed once and cached. Consumed by the options descriptor
128
+ * so tooling can offer the by-name form of the `animation` option. Sorted
129
+ * so every port reports the same order regardless of how it walks the
130
+ * definition.
131
+ */
132
+ animationNames() {
133
+ if (__classPrivateFieldGet(this, _Style_animationNames, "f") === undefined) {
134
+ const names = new Set();
135
+ __classPrivateFieldGet(this, _Style_instances, "m", _Style_visitElements).call(this, (element) => {
136
+ for (const animation of element.animations ?? []) {
137
+ if (animation.name !== undefined) {
138
+ names.add(animation.name);
139
+ }
140
+ }
141
+ });
142
+ __classPrivateFieldSet(this, _Style_animationNames, Array.from(names).sort(), "f");
143
+ }
144
+ return __classPrivateFieldGet(this, _Style_animationNames, "f");
145
+ }
104
146
  /**
105
147
  * Returns a name → {@link Color} map for all defined colors, built lazily
106
148
  * on first access.
@@ -113,7 +155,7 @@ export class Style {
113
155
  return __classPrivateFieldGet(this, _Style_colors, "f");
114
156
  }
115
157
  }
116
- _a = Style, _Style_data = new WeakMap(), _Style_meta = new WeakMap(), _Style_canvas = new WeakMap(), _Style_components = new WeakMap(), _Style_colors = new WeakMap(), _Style_instances = new WeakSet(), _Style_validateAliases = function _Style_validateAliases() {
158
+ _a = Style, _Style_data = new WeakMap(), _Style_meta = new WeakMap(), _Style_canvas = new WeakMap(), _Style_components = new WeakMap(), _Style_colors = new WeakMap(), _Style_hasAnimations = new WeakMap(), _Style_animationNames = new WeakMap(), _Style_instances = new WeakSet(), _Style_validateAliases = function _Style_validateAliases() {
117
159
  const components = __classPrivateFieldGet(this, _Style_data, "f").components;
118
160
  if (!components) {
119
161
  return;
@@ -144,4 +186,43 @@ _a = Style, _Style_data = new WeakMap(), _Style_meta = new WeakMap(), _Style_can
144
186
  }
145
187
  }, _Style_isAlias = function _Style_isAlias(data) {
146
188
  return 'extends' in data;
189
+ }, _Style_validateAnimations = function _Style_validateAnimations() {
190
+ const errors = [];
191
+ __classPrivateFieldGet(this, _Style_instances, "m", _Style_visitElements).call(this, (element, path) => {
192
+ element.animations?.forEach((animation, animationIndex) => {
193
+ for (const [trackName, track] of Object.entries(animation.tracks)) {
194
+ const keyframes = track.keyframes;
195
+ for (let i = 1; i < keyframes.length; i++) {
196
+ if (keyframes[i].at <= keyframes[i - 1].at) {
197
+ errors.push({
198
+ instancePath: `${path}/animations/${animationIndex}/tracks/${trackName}/keyframes/${i}/at`,
199
+ message: 'must be greater than the previous keyframe',
200
+ });
201
+ }
202
+ }
203
+ }
204
+ });
205
+ });
206
+ if (errors.length > 0) {
207
+ throw new StyleValidationError(errors);
208
+ }
209
+ }, _Style_visitElements = function _Style_visitElements(visit) {
210
+ const walk = (elements, path) => {
211
+ elements.forEach((element, index) => {
212
+ const elementPath = `${path}/${index}`;
213
+ visit(element, elementPath);
214
+ if (element.children) {
215
+ walk(element.children, `${elementPath}/children`);
216
+ }
217
+ });
218
+ };
219
+ walk(__classPrivateFieldGet(this, _Style_data, "f").canvas.elements, '/canvas/elements');
220
+ for (const [name, component] of Object.entries(__classPrivateFieldGet(this, _Style_data, "f").components ?? {})) {
221
+ if (__classPrivateFieldGet(_a, _a, "m", _Style_isAlias).call(_a, component)) {
222
+ continue;
223
+ }
224
+ for (const [variantName, variant] of Object.entries(component.variants)) {
225
+ walk(variant.elements, `/components/${name}/variants/${variantName}/elements`);
226
+ }
227
+ }
147
228
  };
@@ -38,11 +38,67 @@ export interface StyleDefinitionAttributes {
38
38
  readonly 'font-weight'?: string | StyleDefinitionVariableReference;
39
39
  readonly [key: string]: string | StyleDefinitionColorReference | StyleDefinitionVariableReference | undefined;
40
40
  }
41
+ export type StyleDefinitionEasingKeyword = 'linear' | 'ease' | 'easeIn' | 'easeOut' | 'easeInOut' | 'hold';
42
+ /**
43
+ * A cubic bezier easing given by its two control points. `x1`/`x2` stay
44
+ * within 0..1, while `y1`/`y2` may leave that range for overshoot curves.
45
+ */
46
+ export interface StyleDefinitionEasingBezier {
47
+ readonly x1: number;
48
+ readonly y1: number;
49
+ readonly x2: number;
50
+ readonly y2: number;
51
+ }
52
+ export type StyleDefinitionEasing = StyleDefinitionEasingKeyword | StyleDefinitionEasingBezier;
53
+ /**
54
+ * A keyframe on an animation timeline. `at` is a percentage of the
55
+ * animation's duration. `easing` shapes the segment from this keyframe to
56
+ * the next one and falls back to the animation's default easing.
57
+ */
58
+ export interface StyleDefinitionAnimationKeyframe {
59
+ readonly at: number;
60
+ readonly value: number;
61
+ readonly easing?: StyleDefinitionEasing;
62
+ }
63
+ export interface StyleDefinitionAnimationTrack {
64
+ readonly keyframes: readonly StyleDefinitionAnimationKeyframe[];
65
+ }
66
+ export type StyleDefinitionAnimationTrackName = 'translateX' | 'translateY' | 'rotate' | 'scaleX' | 'scaleY' | 'opacity';
67
+ export type StyleDefinitionAnimationDirection = 'normal' | 'reverse' | 'alternate' | 'alternateReverse';
68
+ /**
69
+ * The transform origin for `rotate` and `scale` tracks, as a percentage of
70
+ * the element's bounding box.
71
+ */
72
+ export interface StyleDefinitionAnimationOrigin {
73
+ readonly x: number;
74
+ readonly y: number;
75
+ }
76
+ /**
77
+ * One animation timeline for an element. Rendered to CSS only when the
78
+ * `animation` render option is enabled. Absent fields carry these defaults:
79
+ * `delay` 0, `iterations` `'infinite'`, `direction` `'normal'`, `fill`
80
+ * `'none'`, `easing` `'linear'`, `origin` center (50/50). The optional
81
+ * `name` groups the timeline under a user-selectable animation: the
82
+ * `animation` option accepts these names to play a subset, and a timeline
83
+ * without a name only plays when the option enables all animations.
84
+ */
85
+ export interface StyleDefinitionAnimation {
86
+ readonly name?: string;
87
+ readonly duration: number;
88
+ readonly delay?: number;
89
+ readonly iterations?: 'infinite' | number;
90
+ readonly direction?: StyleDefinitionAnimationDirection;
91
+ readonly fill?: 'none' | 'forwards';
92
+ readonly easing?: StyleDefinitionEasing;
93
+ readonly origin?: StyleDefinitionAnimationOrigin;
94
+ readonly tracks: Readonly<Partial<Record<StyleDefinitionAnimationTrackName, StyleDefinitionAnimationTrack>>>;
95
+ }
41
96
  export interface StyleDefinitionElement {
42
97
  readonly type: StyleDefinitionElementType;
43
98
  readonly name?: string;
44
99
  readonly value?: StyleDefinitionElementValue;
45
100
  readonly attributes?: StyleDefinitionAttributes;
101
+ readonly animations?: readonly StyleDefinitionAnimation[];
46
102
  readonly children?: readonly StyleDefinitionElement[];
47
103
  }
48
104
  export interface StyleDefinitionCanvas {
@@ -43,6 +43,8 @@ export interface StyleOptionsBase {
43
43
  readonly translateX?: number | readonly [number, number];
44
44
  readonly translateY?: number | readonly [number, number];
45
45
  readonly tags?: string | readonly string[];
46
+ readonly animation?: boolean | string | readonly string[];
47
+ readonly animationSpeed?: number | readonly [number, number];
46
48
  }
47
49
  type ComponentVariantOption<D, K extends string> = VariantNames<D, K> | readonly VariantNames<D, K>[] | Readonly<Partial<Record<VariantNames<D, K>, number>>>;
48
50
  type IsAlias<D, C extends string> = D extends {