@dicebear/core 10.7.0 → 11.0.0-rc.2

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/lib/Resolver.js CHANGED
@@ -47,6 +47,56 @@ 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 an avatar animates must
52
+ // not depend on the seed.
53
+ return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, 'animation', () => __classPrivateFieldGet(this, _Resolver_options, "f").animation() ?? false);
54
+ }
55
+ /**
56
+ * Whether one timeline plays. A named timeline follows its
57
+ * `${name}Animation` switch when the user set one, recorded under that
58
+ * key, and the global `animation` switch otherwise. Unnamed timelines
59
+ * always follow the global switch.
60
+ */
61
+ animationPlays(name) {
62
+ const value = name === undefined ? undefined : __classPrivateFieldGet(this, _Resolver_options, "f").animationFor(name);
63
+ if (name === undefined || value === undefined) {
64
+ return this.animation();
65
+ }
66
+ return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, `${name}Animation`, () => value);
67
+ }
68
+ animationSpeed() {
69
+ return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memoFloat).call(this, 'animationSpeed', __classPrivateFieldGet(this, _Resolver_options, "f").animationSpeed(), 1);
70
+ }
71
+ /**
72
+ * Returns the speed factor of one timeline. A named timeline plays at its
73
+ * `${name}AnimationSpeed` option when the user set one, drawn under that
74
+ * key, and at the global factor otherwise. Unnamed timelines always follow
75
+ * the global factor.
76
+ */
77
+ animationSpeedFor(name) {
78
+ const range = name === undefined ? undefined : __classPrivateFieldGet(this, _Resolver_options, "f").animationSpeedFor(name);
79
+ if (name === undefined || range === undefined) {
80
+ return this.animationSpeed();
81
+ }
82
+ return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memoFloat).call(this, `${name}AnimationSpeed`, range, 1);
83
+ }
84
+ animationDelay() {
85
+ return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memoFloat).call(this, 'animationDelay', __classPrivateFieldGet(this, _Resolver_options, "f").animationDelay(), 0);
86
+ }
87
+ /**
88
+ * Returns the start offset of one timeline in seconds. A named timeline
89
+ * uses its `${name}AnimationDelay` option when the user set one, drawn
90
+ * under that key, and the global offset otherwise. Unnamed timelines
91
+ * always follow the global offset.
92
+ */
93
+ animationDelayFor(name) {
94
+ const range = name === undefined ? undefined : __classPrivateFieldGet(this, _Resolver_options, "f").animationDelayFor(name);
95
+ if (name === undefined || range === undefined) {
96
+ return this.animationDelay();
97
+ }
98
+ return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memoFloat).call(this, `${name}AnimationDelay`, range, 0);
99
+ }
50
100
  title() {
51
101
  return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, 'title', () => __classPrivateFieldGet(this, _Resolver_options, "f").title());
52
102
  }
@@ -223,13 +273,12 @@ _Resolver_style = new WeakMap(), _Resolver_options = new WeakMap(), _Resolver_pr
223
273
  const source = userColors ?? styleColor?.values() ?? [];
224
274
  let candidates = source.map((c) => Color.toHex(c));
225
275
  const fixed = this.colorOrder(name) === COLOR_ORDER_FIXED;
226
- const verbatim = userColors !== undefined && fixed;
227
276
  const fill = this.colorFill(name);
228
277
  const stops = fill === 'solid'
229
278
  ? 1
230
- : __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_colorFillStops).call(this, name, verbatim ? candidates.length : 2);
279
+ : __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_colorFillStops).call(this, name, fixed ? candidates.length : 2);
231
280
  if (!styleColor) {
232
- return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_order).call(this, name, candidates, fixed, verbatim).slice(0, stops);
281
+ return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_order).call(this, name, candidates, fixed).slice(0, stops);
233
282
  }
234
283
  // Detect circular references (e.g. a.contrastTo = b, b.contrastTo = a)
235
284
  if (__classPrivateFieldGet(this, _Resolver_colorResolving, "f").includes(name)) {
@@ -239,7 +288,7 @@ _Resolver_style = new WeakMap(), _Resolver_options = new WeakMap(), _Resolver_pr
239
288
  const contrastTo = styleColor.contrastTo();
240
289
  const notEqualTo = styleColor.notEqualTo();
241
290
  try {
242
- if (contrastTo && !verbatim) {
291
+ if (contrastTo && !fixed) {
243
292
  const refColor = this.color(contrastTo)[0];
244
293
  if (refColor) {
245
294
  candidates = Color.sortByContrast(candidates, refColor);
@@ -261,19 +310,10 @@ _Resolver_style = new WeakMap(), _Resolver_options = new WeakMap(), _Resolver_pr
261
310
  // Skip shuffle when sorted by contrast to preserve the ordering
262
311
  const ordered = contrastTo
263
312
  ? candidates
264
- : __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_order).call(this, name, candidates, fixed, verbatim);
313
+ : __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_order).call(this, name, candidates, fixed);
265
314
  return ordered.slice(0, stops);
266
- }, _Resolver_order = function _Resolver_order(name, candidates, fixed, verbatim) {
267
- if (!fixed) {
268
- return __classPrivateFieldGet(this, _Resolver_prng, "f").shuffle(`${name}Color`, candidates);
269
- }
270
- if (verbatim) {
271
- return candidates;
272
- }
273
- // Deprecated: DiceBear 11 will take the palette in its definition order
274
- // here, the same verbatim rule as user-supplied colors, and drop this
275
- // sort (see CHANGELOG.md, "Deprecated").
276
- return Array.from(new Set(candidates)).sort();
315
+ }, _Resolver_order = function _Resolver_order(name, candidates, fixed) {
316
+ return fixed ? candidates : __classPrivateFieldGet(this, _Resolver_prng, "f").shuffle(`${name}Color`, candidates);
277
317
  }, _Resolver_colorFillStops = function _Resolver_colorFillStops(name, fallback) {
278
318
  const range = __classPrivateFieldGet(this, _Resolver_options, "f").colorFillStops(name);
279
319
  return range
@@ -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 a switch, a speed and a delay per animation name.
60
+ * Sorted so every port reports the same order regardless of how it walks
61
+ * the 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,8 +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;
13
- import { StyleValidator } from './Validator/StyleValidator.js';
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;
14
13
  import { StyleValidationError } from './Error/StyleValidationError.js';
15
14
  import { Meta } from './Style/Meta.js';
16
15
  import { Canvas } from './Style/Canvas.js';
@@ -29,9 +28,11 @@ export class Style {
29
28
  _Style_canvas.set(this, void 0);
30
29
  _Style_components.set(this, void 0);
31
30
  _Style_colors.set(this, void 0);
32
- StyleValidator.validate(data);
31
+ _Style_hasAnimations.set(this, void 0);
32
+ _Style_animationNames.set(this, void 0);
33
33
  __classPrivateFieldSet(this, _Style_data, structuredClone(data), "f");
34
34
  __classPrivateFieldGet(this, _Style_instances, "m", _Style_validateAliases).call(this);
35
+ __classPrivateFieldGet(this, _Style_instances, "m", _Style_validateAnimations).call(this);
35
36
  }
36
37
  /**
37
38
  * Returns the definition's `$id`, or `undefined` when not set.
@@ -101,6 +102,45 @@ export class Style {
101
102
  __classPrivateFieldSet(this, _Style_components, map, "f");
102
103
  return __classPrivateFieldGet(this, _Style_components, "f");
103
104
  }
105
+ /**
106
+ * Returns whether any element in the definition carries declarative
107
+ * animations. Computed once and cached. Consumed by the options
108
+ * descriptor to advertise the `animation` options only where they have an
109
+ * effect.
110
+ */
111
+ hasAnimations() {
112
+ if (__classPrivateFieldGet(this, _Style_hasAnimations, "f") === undefined) {
113
+ let found = false;
114
+ __classPrivateFieldGet(this, _Style_instances, "m", _Style_visitElements).call(this, (element) => {
115
+ if (element.animations !== undefined && element.animations.length > 0) {
116
+ found = true;
117
+ }
118
+ });
119
+ __classPrivateFieldSet(this, _Style_hasAnimations, found, "f");
120
+ }
121
+ return __classPrivateFieldGet(this, _Style_hasAnimations, "f");
122
+ }
123
+ /**
124
+ * Returns the sorted distinct names of the definition's animation
125
+ * timelines. Computed once and cached. Consumed by the options descriptor
126
+ * so tooling can offer a switch, a speed and a delay per animation name.
127
+ * Sorted so every port reports the same order regardless of how it walks
128
+ * the definition.
129
+ */
130
+ animationNames() {
131
+ if (__classPrivateFieldGet(this, _Style_animationNames, "f") === undefined) {
132
+ const names = new Set();
133
+ __classPrivateFieldGet(this, _Style_instances, "m", _Style_visitElements).call(this, (element) => {
134
+ for (const animation of element.animations ?? []) {
135
+ if (animation.name !== undefined) {
136
+ names.add(animation.name);
137
+ }
138
+ }
139
+ });
140
+ __classPrivateFieldSet(this, _Style_animationNames, Array.from(names).sort(), "f");
141
+ }
142
+ return __classPrivateFieldGet(this, _Style_animationNames, "f");
143
+ }
104
144
  /**
105
145
  * Returns a name → {@link Color} map for all defined colors, built lazily
106
146
  * on first access.
@@ -113,7 +153,7 @@ export class Style {
113
153
  return __classPrivateFieldGet(this, _Style_colors, "f");
114
154
  }
115
155
  }
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() {
156
+ _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
157
  const components = __classPrivateFieldGet(this, _Style_data, "f").components;
118
158
  if (!components) {
119
159
  return;
@@ -144,4 +184,43 @@ _a = Style, _Style_data = new WeakMap(), _Style_meta = new WeakMap(), _Style_can
144
184
  }
145
185
  }, _Style_isAlias = function _Style_isAlias(data) {
146
186
  return 'extends' in data;
187
+ }, _Style_validateAnimations = function _Style_validateAnimations() {
188
+ const errors = [];
189
+ __classPrivateFieldGet(this, _Style_instances, "m", _Style_visitElements).call(this, (element, path) => {
190
+ element.animations?.forEach((animation, animationIndex) => {
191
+ for (const [trackName, track] of Object.entries(animation.tracks)) {
192
+ const keyframes = track.keyframes;
193
+ for (let i = 1; i < keyframes.length; i++) {
194
+ if (keyframes[i].at <= keyframes[i - 1].at) {
195
+ errors.push({
196
+ instancePath: `${path}/animations/${animationIndex}/tracks/${trackName}/keyframes/${i}/at`,
197
+ message: 'must be greater than the previous keyframe',
198
+ });
199
+ }
200
+ }
201
+ }
202
+ });
203
+ });
204
+ if (errors.length > 0) {
205
+ throw new StyleValidationError(errors);
206
+ }
207
+ }, _Style_visitElements = function _Style_visitElements(visit) {
208
+ const walk = (elements, path) => {
209
+ elements.forEach((element, index) => {
210
+ const elementPath = `${path}/${index}`;
211
+ visit(element, elementPath);
212
+ if (element.children) {
213
+ walk(element.children, `${elementPath}/children`);
214
+ }
215
+ });
216
+ };
217
+ walk(__classPrivateFieldGet(this, _Style_data, "f").canvas.elements, '/canvas/elements');
218
+ for (const [name, component] of Object.entries(__classPrivateFieldGet(this, _Style_data, "f").components ?? {})) {
219
+ if (__classPrivateFieldGet(_a, _a, "m", _Style_isAlias).call(_a, component)) {
220
+ continue;
221
+ }
222
+ for (const [variantName, variant] of Object.entries(component.variants)) {
223
+ walk(variant.elements, `/components/${name}/variants/${variantName}/elements`);
224
+ }
225
+ }
147
226
  };
@@ -38,11 +38,68 @@ 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: a named
82
+ * timeline follows its `${name}Animation`, `${name}AnimationSpeed` and
83
+ * `${name}AnimationDelay` options when set, and a timeline without a name
84
+ * always follows the global `animation` switch.
85
+ */
86
+ export interface StyleDefinitionAnimation {
87
+ readonly name?: string;
88
+ readonly duration: number;
89
+ readonly delay?: number;
90
+ readonly iterations?: 'infinite' | number;
91
+ readonly direction?: StyleDefinitionAnimationDirection;
92
+ readonly fill?: 'none' | 'forwards';
93
+ readonly easing?: StyleDefinitionEasing;
94
+ readonly origin?: StyleDefinitionAnimationOrigin;
95
+ readonly tracks: Readonly<Partial<Record<StyleDefinitionAnimationTrackName, StyleDefinitionAnimationTrack>>>;
96
+ }
41
97
  export interface StyleDefinitionElement {
42
98
  readonly type: StyleDefinitionElementType;
43
99
  readonly name?: string;
44
100
  readonly value?: StyleDefinitionElementValue;
45
101
  readonly attributes?: StyleDefinitionAttributes;
102
+ readonly animations?: readonly StyleDefinitionAnimation[];
46
103
  readonly children?: readonly StyleDefinitionElement[];
47
104
  }
48
105
  export interface StyleDefinitionCanvas {
@@ -43,6 +43,9 @@ 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;
47
+ readonly animationSpeed?: number | readonly [number, number];
48
+ readonly animationDelay?: number | readonly [number, number];
46
49
  }
47
50
  type ComponentVariantOption<D, K extends string> = VariantNames<D, K> | readonly VariantNames<D, K>[] | Readonly<Partial<Record<VariantNames<D, K>, number>>>;
48
51
  type IsAlias<D, C extends string> = D extends {
@@ -66,7 +69,12 @@ type ColorOptions<C extends string> = [C] extends [never] ? unknown : {
66
69
  } & {
67
70
  readonly [K in C as `${K}ColorOrder`]?: StyleOptionsColorOrderValue;
68
71
  };
69
- export type StyleOptions<D = unknown> = StyleOptionsBase & ComponentOptions<D, ComponentNames<D>> & ColorOptions<AllColorNames<D>> & (HasSpecificKeys<D> extends true ? unknown : {
72
+ type AnimationOptions = {
73
+ readonly [key: `${string}Animation`]: boolean | undefined;
74
+ readonly [key: `${string}AnimationSpeed`]: number | readonly [number, number] | undefined;
75
+ readonly [key: `${string}AnimationDelay`]: number | readonly [number, number] | undefined;
76
+ };
77
+ export type StyleOptions<D = unknown> = StyleOptionsBase & ComponentOptions<D, ComponentNames<D>> & ColorOptions<AllColorNames<D>> & (HasSpecificKeys<D> extends true ? AnimationOptions : {
70
78
  readonly [key: string]: unknown;
71
79
  });
72
80
  export {};