@dicebear/core 11.0.0-rc.1 → 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/README.md +18 -3
- package/lib/Avatar.d.ts +4 -6
- package/lib/Avatar.js +5 -13
- package/lib/Options.d.ts +15 -3
- package/lib/Options.js +24 -10
- package/lib/OptionsDescriptor.d.ts +1 -5
- package/lib/OptionsDescriptor.js +14 -5
- package/lib/Renderer.js +51 -43
- package/lib/Resolver.d.ts +23 -1
- package/lib/Resolver.js +50 -19
- package/lib/Style.d.ts +3 -3
- package/lib/Style.js +3 -5
- package/lib/StyleDefinition.d.ts +4 -3
- package/lib/StyleOptions.d.ts +8 -2
- package/lib/Validator/OptionsValidator.js +762 -704
- package/lib/Validator/StyleValidator.js +226 -137
- package/lib/index.d.ts +20 -2
- package/lib/index.js +26 -2
- package/lib/lite.d.ts +5 -0
- package/lib/lite.js +9 -0
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -24,13 +24,13 @@ Requires Node.js 22+ or any modern browser.
|
|
|
24
24
|
## Usage
|
|
25
25
|
|
|
26
26
|
```js
|
|
27
|
-
import { Avatar } from '@dicebear/core';
|
|
27
|
+
import { Avatar, Style } from '@dicebear/core';
|
|
28
28
|
|
|
29
29
|
// From a style definition (here from the @dicebear/styles package)
|
|
30
30
|
import definition from '@dicebear/styles/lorelei.json' with { type: 'json' };
|
|
31
31
|
|
|
32
|
-
const avatar = new Avatar(definition, {
|
|
33
|
-
seed: 'John
|
|
32
|
+
const avatar = new Avatar(new Style(definition), {
|
|
33
|
+
seed: 'John',
|
|
34
34
|
size: 128,
|
|
35
35
|
});
|
|
36
36
|
|
|
@@ -50,6 +50,21 @@ const avatar1 = new Avatar(style, { seed: 'Alice' });
|
|
|
50
50
|
const avatar2 = new Avatar(style, { seed: 'Bob' });
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
+
## Smaller bundle without validation
|
|
54
|
+
|
|
55
|
+
About half of the package is the two schema validators. `@dicebear/core/lite`
|
|
56
|
+
exports the same API without them, about 14 kB instead of 30 kB gzipped.
|
|
57
|
+
|
|
58
|
+
> **Warning:** The validation is what keeps unsafe content such as scripts,
|
|
59
|
+
> event handlers and external references out of the SVG, and what turns a wrong
|
|
60
|
+
> option into an error instead of odd output. The lite entry renders whatever it
|
|
61
|
+
> gets. Use it only for definitions and options you wrote or generated yourself,
|
|
62
|
+
> never for anything from an upload, a URL or another source you do not control.
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
import { Style, Avatar } from '@dicebear/core/lite';
|
|
66
|
+
```
|
|
67
|
+
|
|
53
68
|
## Sponsors
|
|
54
69
|
|
|
55
70
|
Advertisement: Many thanks to our sponsors who provide us with free or
|
package/lib/Avatar.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { Style } from './Style.js';
|
|
2
2
|
import type { StyleOptions } from './StyleOptions.js';
|
|
3
|
-
type UnwrapStyle<D> = D extends Style<infer S> ? S : D;
|
|
4
3
|
interface AvatarJson<D = unknown> {
|
|
5
4
|
readonly svg: string;
|
|
6
|
-
readonly options: StyleOptions<
|
|
5
|
+
readonly options: StyleOptions<D>;
|
|
7
6
|
}
|
|
8
7
|
/**
|
|
9
8
|
* Top-level entry point for rendering an avatar from a style and options.
|
|
@@ -14,11 +13,10 @@ interface AvatarJson<D = unknown> {
|
|
|
14
13
|
export declare class Avatar<D = unknown> {
|
|
15
14
|
#private;
|
|
16
15
|
/**
|
|
17
|
-
* Pass a {@link Style} instance
|
|
18
|
-
*
|
|
19
|
-
* reuse the instance across avatars.
|
|
16
|
+
* Pass a {@link Style} instance and reuse it across avatars. The parsed
|
|
17
|
+
* style carries the validation, so nothing is re-checked per render.
|
|
20
18
|
*/
|
|
21
|
-
constructor(
|
|
19
|
+
constructor(style: Style<D>, optionsInput?: StyleOptions<D>);
|
|
22
20
|
/**
|
|
23
21
|
* Returns the rendered SVG markup.
|
|
24
22
|
*/
|
package/lib/Avatar.js
CHANGED
|
@@ -14,10 +14,6 @@ import { Style } from './Style.js';
|
|
|
14
14
|
import { Options } from './Options.js';
|
|
15
15
|
import { Resolver } from './Resolver.js';
|
|
16
16
|
import { Renderer } from './Renderer.js';
|
|
17
|
-
// Emitted at most once per process: passing a raw definition is deprecated, but
|
|
18
|
-
// it was the documented usage for a long time, so a per-call warning would be
|
|
19
|
-
// noisy.
|
|
20
|
-
let definitionInputWarned = false;
|
|
21
17
|
/**
|
|
22
18
|
* Top-level entry point for rendering an avatar from a style and options.
|
|
23
19
|
*
|
|
@@ -26,20 +22,16 @@ let definitionInputWarned = false;
|
|
|
26
22
|
*/
|
|
27
23
|
export class Avatar {
|
|
28
24
|
/**
|
|
29
|
-
* Pass a {@link Style} instance
|
|
30
|
-
*
|
|
31
|
-
* reuse the instance across avatars.
|
|
25
|
+
* Pass a {@link Style} instance and reuse it across avatars. The parsed
|
|
26
|
+
* style carries the validation, so nothing is re-checked per render.
|
|
32
27
|
*/
|
|
33
|
-
constructor(
|
|
28
|
+
constructor(style, optionsInput) {
|
|
34
29
|
_Avatar_svg.set(this, void 0);
|
|
35
30
|
_Avatar_resolvedOptions.set(this, void 0);
|
|
36
|
-
if (!(
|
|
37
|
-
|
|
38
|
-
console.warn('[DiceBear] Passing a style definition to `new Avatar()` is deprecated ' +
|
|
39
|
-
'and will be removed in v11. Wrap it in a Style first: ' +
|
|
31
|
+
if (!(style instanceof Style)) {
|
|
32
|
+
throw new TypeError('[DiceBear] `new Avatar()` expects a Style. Wrap the definition first: ' +
|
|
40
33
|
'`new Avatar(new Style(definition), options)`.');
|
|
41
34
|
}
|
|
42
|
-
const style = styleInput instanceof Style ? styleInput : new Style(styleInput);
|
|
43
35
|
const options = new Options(optionsInput);
|
|
44
36
|
const resolver = new Resolver(style, options);
|
|
45
37
|
__classPrivateFieldSet(this, _Avatar_svg, new Renderer(style, resolver).render(), "f");
|
package/lib/Options.d.ts
CHANGED
|
@@ -25,12 +25,24 @@ export declare class Options<D = unknown> {
|
|
|
25
25
|
rotate(): Range | undefined;
|
|
26
26
|
translateX(): Range | undefined;
|
|
27
27
|
translateY(): Range | undefined;
|
|
28
|
+
animation(): boolean | undefined;
|
|
28
29
|
/**
|
|
29
|
-
* Returns the
|
|
30
|
-
*
|
|
30
|
+
* Returns the `${name}Animation` switch for one animation name, or
|
|
31
|
+
* `undefined` when unset.
|
|
31
32
|
*/
|
|
32
|
-
|
|
33
|
+
animationFor(name: string): boolean | undefined;
|
|
33
34
|
animationSpeed(): Range | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the `${name}AnimationSpeed` option for one animation name as a
|
|
37
|
+
* range, or `undefined` when unset.
|
|
38
|
+
*/
|
|
39
|
+
animationSpeedFor(name: string): Range | undefined;
|
|
40
|
+
animationDelay(): Range | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Returns the `${name}AnimationDelay` option for one animation name as a
|
|
43
|
+
* range, or `undefined` when unset.
|
|
44
|
+
*/
|
|
45
|
+
animationDelayFor(name: string): Range | undefined;
|
|
34
46
|
/**
|
|
35
47
|
* Returns the global `tags` filter as parsed tokens, or an empty array when
|
|
36
48
|
* unset. Each raw token (`category` / `category:value`, optionally
|
package/lib/Options.js
CHANGED
|
@@ -10,7 +10,6 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _Options_instances, _Options_data, _Options_tags, _Options_dynamic, _Options_asArray, _Options_toRange;
|
|
13
|
-
import { OptionsValidator } from './Validator/OptionsValidator.js';
|
|
14
13
|
/**
|
|
15
14
|
* Validates the raw user-supplied options and exposes them through typed
|
|
16
15
|
* accessors. Each accessor returns the user's input in a normalized form
|
|
@@ -26,7 +25,6 @@ export class Options {
|
|
|
26
25
|
_Options_instances.add(this);
|
|
27
26
|
_Options_data.set(this, void 0);
|
|
28
27
|
_Options_tags.set(this, void 0);
|
|
29
|
-
OptionsValidator.validate(data);
|
|
30
28
|
__classPrivateFieldSet(this, _Options_data, structuredClone(data), "f");
|
|
31
29
|
}
|
|
32
30
|
seed() {
|
|
@@ -65,20 +63,36 @@ export class Options {
|
|
|
65
63
|
translateY() {
|
|
66
64
|
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_toRange).call(this, __classPrivateFieldGet(this, _Options_data, "f").translateY);
|
|
67
65
|
}
|
|
66
|
+
animation() {
|
|
67
|
+
return __classPrivateFieldGet(this, _Options_data, "f").animation;
|
|
68
|
+
}
|
|
68
69
|
/**
|
|
69
|
-
* Returns the
|
|
70
|
-
*
|
|
70
|
+
* Returns the `${name}Animation` switch for one animation name, or
|
|
71
|
+
* `undefined` when unset.
|
|
71
72
|
*/
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (raw === undefined || typeof raw === 'boolean') {
|
|
75
|
-
return raw;
|
|
76
|
-
}
|
|
77
|
-
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_asArray).call(this, raw);
|
|
73
|
+
animationFor(name) {
|
|
74
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_dynamic).call(this, `${name}Animation`);
|
|
78
75
|
}
|
|
79
76
|
animationSpeed() {
|
|
80
77
|
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_toRange).call(this, __classPrivateFieldGet(this, _Options_data, "f").animationSpeed);
|
|
81
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Returns the `${name}AnimationSpeed` option for one animation name as a
|
|
81
|
+
* range, or `undefined` when unset.
|
|
82
|
+
*/
|
|
83
|
+
animationSpeedFor(name) {
|
|
84
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_toRange).call(this, __classPrivateFieldGet(this, _Options_instances, "m", _Options_dynamic).call(this, `${name}AnimationSpeed`));
|
|
85
|
+
}
|
|
86
|
+
animationDelay() {
|
|
87
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_toRange).call(this, __classPrivateFieldGet(this, _Options_data, "f").animationDelay);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Returns the `${name}AnimationDelay` option for one animation name as a
|
|
91
|
+
* range, or `undefined` when unset.
|
|
92
|
+
*/
|
|
93
|
+
animationDelayFor(name) {
|
|
94
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_toRange).call(this, __classPrivateFieldGet(this, _Options_instances, "m", _Options_dynamic).call(this, `${name}AnimationDelay`));
|
|
95
|
+
}
|
|
82
96
|
/**
|
|
83
97
|
* Returns the global `tags` filter as parsed tokens, or an empty array when
|
|
84
98
|
* unset. Each raw token (`category` / `category:value`, optionally
|
|
@@ -30,11 +30,7 @@ interface RangeField {
|
|
|
30
30
|
readonly min?: number;
|
|
31
31
|
readonly max?: number;
|
|
32
32
|
}
|
|
33
|
-
|
|
34
|
-
readonly type: 'animation';
|
|
35
|
-
readonly values: readonly string[];
|
|
36
|
-
}
|
|
37
|
-
export type FieldDescriptor = StringField | NumberField | BooleanField | EnumField | ColorField | RangeField | AnimationField;
|
|
33
|
+
export type FieldDescriptor = StringField | NumberField | BooleanField | EnumField | ColorField | RangeField;
|
|
38
34
|
export type Descriptor = Record<string, FieldDescriptor>;
|
|
39
35
|
/**
|
|
40
36
|
* Builds a descriptor of every option a given style accepts. Tooling such as
|
package/lib/OptionsDescriptor.js
CHANGED
|
@@ -108,13 +108,22 @@ _a = OptionsDescriptor, _OptionsDescriptor_descriptor = new WeakMap(), _OptionsD
|
|
|
108
108
|
};
|
|
109
109
|
}
|
|
110
110
|
// Only advertise the animation options when the style carries declarative
|
|
111
|
-
// animations
|
|
111
|
+
// animations. On a static style they are accepted but have no effect.
|
|
112
|
+
// Every animation name gets its own switch, speed and delay field, in the
|
|
113
|
+
// order of the name list.
|
|
112
114
|
if (__classPrivateFieldGet(this, _OptionsDescriptor_style, "f").hasAnimations()) {
|
|
113
|
-
result.animation = {
|
|
114
|
-
type: 'animation',
|
|
115
|
-
values: __classPrivateFieldGet(this, _OptionsDescriptor_style, "f").animationNames(),
|
|
116
|
-
};
|
|
115
|
+
result.animation = { type: 'boolean' };
|
|
117
116
|
result.animationSpeed = { type: 'range', min: 0.1, max: 10 };
|
|
117
|
+
result.animationDelay = { type: 'range', min: -3600, max: 3600 };
|
|
118
|
+
for (const name of __classPrivateFieldGet(this, _OptionsDescriptor_style, "f").animationNames()) {
|
|
119
|
+
result[`${name}Animation`] = { type: 'boolean' };
|
|
120
|
+
result[`${name}AnimationSpeed`] = { type: 'range', min: 0.1, max: 10 };
|
|
121
|
+
result[`${name}AnimationDelay`] = {
|
|
122
|
+
type: 'range',
|
|
123
|
+
min: -3600,
|
|
124
|
+
max: 3600,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
118
127
|
}
|
|
119
128
|
return result;
|
|
120
129
|
};
|
package/lib/Renderer.js
CHANGED
|
@@ -188,21 +188,26 @@ _a = Renderer, _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap
|
|
|
188
188
|
}
|
|
189
189
|
return '';
|
|
190
190
|
}
|
|
191
|
-
const attrs = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_renderAttributes).call(this, __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_attributesWithoutAnimatedOpacity).call(this, element.attributes(), element));
|
|
192
191
|
const children = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_renderElements).call(this, element.children());
|
|
192
|
+
// A wrapper whose children all rendered to nothing, because an optional
|
|
193
|
+
// component came up empty, has no content left to group. It draws
|
|
194
|
+
// nothing either way, but a masked group without content has an empty
|
|
195
|
+
// bounding box, and strict SVG parsers reject the whole document over
|
|
196
|
+
// it. Wrappers that carry an id stay, so references keep resolving.
|
|
197
|
+
if (children.length === 0 &&
|
|
198
|
+
element.children().length > 0 &&
|
|
199
|
+
element.attributes()?.id === undefined) {
|
|
200
|
+
return '';
|
|
201
|
+
}
|
|
202
|
+
// The animation switches are read once the element is known to render,
|
|
203
|
+
// after its children, so the resolved options record them in one order
|
|
204
|
+
// regardless of the attributes the element carries.
|
|
205
|
+
const active = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_activeAnimations).call(this, element);
|
|
206
|
+
const attrs = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_renderAttributes).call(this, __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_attributesWithoutAnimatedOpacity).call(this, element.attributes(), active));
|
|
193
207
|
if (children.length === 0) {
|
|
194
|
-
|
|
195
|
-
// component came up empty, has no content left to group. It draws
|
|
196
|
-
// nothing either way, but a masked group without content has an empty
|
|
197
|
-
// bounding box, and strict SVG parsers reject the whole document over
|
|
198
|
-
// it. Wrappers that carry an id stay, so references keep resolving.
|
|
199
|
-
if (element.children().length > 0 &&
|
|
200
|
-
element.attributes()?.id === undefined) {
|
|
201
|
-
return '';
|
|
202
|
-
}
|
|
203
|
-
return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<${name}${attrs}/>`, element);
|
|
208
|
+
return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<${name}${attrs}/>`, element, active);
|
|
204
209
|
}
|
|
205
|
-
return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<${name}${attrs}>${children}</${name}>`, element);
|
|
210
|
+
return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<${name}${attrs}>${children}</${name}>`, element, active);
|
|
206
211
|
}, _Renderer_renderTextElement = function _Renderer_renderTextElement(element) {
|
|
207
212
|
const value = element.value();
|
|
208
213
|
return value !== undefined ? Xml.escape(__classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_resolveValue).call(this, value)) : '';
|
|
@@ -229,7 +234,8 @@ _a = Renderer, _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap
|
|
|
229
234
|
__classPrivateFieldGet(this, _Renderer_defs, "f").set(id, `<g id="${id}">${body}</g>`);
|
|
230
235
|
}
|
|
231
236
|
const transforms = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_buildTransforms).call(this, component);
|
|
232
|
-
const
|
|
237
|
+
const active = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_activeAnimations).call(this, element);
|
|
238
|
+
const userAttributes = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_attributesWithoutAnimatedOpacity).call(this, element.attributes(), active);
|
|
233
239
|
let mergedAttributes = userAttributes;
|
|
234
240
|
if (transforms.length > 0) {
|
|
235
241
|
const userTransform = userAttributes?.transform;
|
|
@@ -239,7 +245,7 @@ _a = Renderer, _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap
|
|
|
239
245
|
mergedAttributes = { ...userAttributes, transform: allParts.join(' ') };
|
|
240
246
|
}
|
|
241
247
|
const attrs = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_renderAttributes).call(this, mergedAttributes);
|
|
242
|
-
return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<use${attrs} href="#${id}"/>`, element);
|
|
248
|
+
return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<use${attrs} href="#${id}"/>`, element, active);
|
|
243
249
|
}, _Renderer_buildTransforms = function _Renderer_buildTransforms(component) {
|
|
244
250
|
const { rotate, translateX, translateY, scale } = __classPrivateFieldGet(this, _Renderer_resolver, "f").componentTransform(component.name());
|
|
245
251
|
if (translateX === 0 && translateY === 0 && rotate === 0 && scale === 1) {
|
|
@@ -334,52 +340,51 @@ _a = Renderer, _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap
|
|
|
334
340
|
}, _Renderer_hashSeed = function _Renderer_hashSeed() {
|
|
335
341
|
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
342
|
}, _Renderer_animationHash = function _Renderer_animationHash() {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
: '';
|
|
343
|
+
if (__classPrivateFieldGet(this, _Renderer_cachedAnimationHash, "f") !== undefined) {
|
|
344
|
+
return __classPrivateFieldGet(this, _Renderer_cachedAnimationHash, "f");
|
|
345
|
+
}
|
|
341
346
|
const random = __classPrivateFieldGet(this, _Renderer_resolver, "f").idRandomization()
|
|
342
347
|
? ':' + __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_randomSuffix).call(this)
|
|
343
348
|
: '';
|
|
344
|
-
|
|
349
|
+
// Every named timeline the style carries joins the hash with its state,
|
|
350
|
+
// `off` or the factor and offset it plays at, so two renders that differ
|
|
351
|
+
// only in a per-name animation option never share their rules. The style
|
|
352
|
+
// lists its names in byte order already.
|
|
353
|
+
const states = __classPrivateFieldGet(this, _Renderer_style, "f")
|
|
354
|
+
.animationNames()
|
|
355
|
+
.map((name) => __classPrivateFieldGet(this, _Renderer_resolver, "f").animationPlays(name)
|
|
356
|
+
? `${name}:${Number.format(__classPrivateFieldGet(this, _Renderer_resolver, "f").animationSpeedFor(name))}:${Number.format(__classPrivateFieldGet(this, _Renderer_resolver, "f").animationDelayFor(name))}`
|
|
357
|
+
: `${name}:off`);
|
|
358
|
+
const named = states.length > 0 ? ':' + states.join(',') : '';
|
|
359
|
+
return (__classPrivateFieldSet(this, _Renderer_cachedAnimationHash, Fnv1a.hex((__classPrivateFieldGet(this, _Renderer_style, "f").meta().source().name() ?? '') +
|
|
345
360
|
':' +
|
|
346
361
|
__classPrivateFieldGet(this, _Renderer_resolver, "f").seed() +
|
|
347
362
|
':' +
|
|
348
363
|
Number.format(__classPrivateFieldGet(this, _Renderer_resolver, "f").animationSpeed()) +
|
|
349
|
-
|
|
364
|
+
':' +
|
|
365
|
+
Number.format(__classPrivateFieldGet(this, _Renderer_resolver, "f").animationDelay()) +
|
|
366
|
+
named +
|
|
350
367
|
random), "f"));
|
|
351
368
|
}, _Renderer_activeAnimations = function _Renderer_activeAnimations(element) {
|
|
352
369
|
const animations = element.animations();
|
|
353
370
|
if (animations.length === 0) {
|
|
354
371
|
return animations;
|
|
355
372
|
}
|
|
356
|
-
|
|
357
|
-
//
|
|
358
|
-
//
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
? []
|
|
363
|
-
: animations.filter((animation) => animation.name !== undefined &&
|
|
364
|
-
selection.includes(animation.name));
|
|
365
|
-
}, _Renderer_attributesWithoutAnimatedOpacity = function _Renderer_attributesWithoutAnimatedOpacity(attributes, element) {
|
|
373
|
+
// The global switch is read first so it lands in the resolved options
|
|
374
|
+
// whenever a node carries animations, on or off. Named timelines then
|
|
375
|
+
// follow their own switch when the user set one.
|
|
376
|
+
__classPrivateFieldGet(this, _Renderer_resolver, "f").animation();
|
|
377
|
+
return animations.filter((animation) => __classPrivateFieldGet(this, _Renderer_resolver, "f").animationPlays(animation.name));
|
|
378
|
+
}, _Renderer_attributesWithoutAnimatedOpacity = function _Renderer_attributesWithoutAnimatedOpacity(attributes, active) {
|
|
366
379
|
if (attributes?.opacity === undefined ||
|
|
367
|
-
|
|
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)) {
|
|
380
|
+
!active.some((animation) => animation.tracks.opacity !== undefined)) {
|
|
372
381
|
return attributes;
|
|
373
382
|
}
|
|
374
383
|
const rest = { ...attributes };
|
|
375
384
|
delete rest.opacity;
|
|
376
385
|
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) {
|
|
386
|
+
}, _Renderer_applyAnimations = function _Renderer_applyAnimations(markup, element, active) {
|
|
387
|
+
if (markup.length === 0 || active.length === 0) {
|
|
383
388
|
return markup;
|
|
384
389
|
}
|
|
385
390
|
const classes = [];
|
|
@@ -414,9 +419,12 @@ _a = Renderer, _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap
|
|
|
414
419
|
__classPrivateFieldGet(this, _Renderer_keyframesByContent, "f").set(body, keyframesName);
|
|
415
420
|
__classPrivateFieldGet(this, _Renderer_keyframesCss, "f").push(`@keyframes ${keyframesName}{${body}}`);
|
|
416
421
|
}
|
|
417
|
-
const speed = __classPrivateFieldGet(this, _Renderer_resolver, "f").
|
|
422
|
+
const speed = __classPrivateFieldGet(this, _Renderer_resolver, "f").animationSpeedFor(animation.name);
|
|
418
423
|
const duration = Number.format(animation.duration / speed);
|
|
419
|
-
|
|
424
|
+
// The user's offset is in seconds of playback, so it is added after the
|
|
425
|
+
// speed has scaled the authored delay.
|
|
426
|
+
const delay = Number.format((animation.delay ?? 0) / speed +
|
|
427
|
+
__classPrivateFieldGet(this, _Renderer_resolver, "f").animationDelayFor(animation.name));
|
|
420
428
|
const iterations = animation.iterations === undefined || animation.iterations === 'infinite'
|
|
421
429
|
? 'infinite'
|
|
422
430
|
: Number.format(animation.iterations);
|
package/lib/Resolver.d.ts
CHANGED
|
@@ -16,8 +16,30 @@ export declare class Resolver<D = unknown> {
|
|
|
16
16
|
seed(): string;
|
|
17
17
|
size(): number | undefined;
|
|
18
18
|
idRandomization(): boolean;
|
|
19
|
-
animation(): boolean
|
|
19
|
+
animation(): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Whether one timeline plays. A named timeline follows its
|
|
22
|
+
* `${name}Animation` switch when the user set one, recorded under that
|
|
23
|
+
* key, and the global `animation` switch otherwise. Unnamed timelines
|
|
24
|
+
* always follow the global switch.
|
|
25
|
+
*/
|
|
26
|
+
animationPlays(name: string | undefined): boolean;
|
|
20
27
|
animationSpeed(): number;
|
|
28
|
+
/**
|
|
29
|
+
* Returns the speed factor of one timeline. A named timeline plays at its
|
|
30
|
+
* `${name}AnimationSpeed` option when the user set one, drawn under that
|
|
31
|
+
* key, and at the global factor otherwise. Unnamed timelines always follow
|
|
32
|
+
* the global factor.
|
|
33
|
+
*/
|
|
34
|
+
animationSpeedFor(name: string | undefined): number;
|
|
35
|
+
animationDelay(): number;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the start offset of one timeline in seconds. A named timeline
|
|
38
|
+
* uses its `${name}AnimationDelay` option when the user set one, drawn
|
|
39
|
+
* under that key, and the global offset otherwise. Unnamed timelines
|
|
40
|
+
* always follow the global offset.
|
|
41
|
+
*/
|
|
42
|
+
animationDelayFor(name: string | undefined): number;
|
|
21
43
|
title(): string | undefined;
|
|
22
44
|
flip(): StyleOptionsFlipValue;
|
|
23
45
|
fontFamily(): string;
|
package/lib/Resolver.js
CHANGED
|
@@ -48,14 +48,55 @@ export class Resolver {
|
|
|
48
48
|
return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, 'idRandomization', () => __classPrivateFieldGet(this, _Resolver_options, "f").idRandomization() ?? false);
|
|
49
49
|
}
|
|
50
50
|
animation() {
|
|
51
|
-
// Deliberately without PRNG involvement
|
|
52
|
-
// not depend on the seed.
|
|
53
|
-
// the timelines carrying one of those names.
|
|
51
|
+
// Deliberately without PRNG involvement: whether an avatar animates must
|
|
52
|
+
// not depend on the seed.
|
|
54
53
|
return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, 'animation', () => __classPrivateFieldGet(this, _Resolver_options, "f").animation() ?? false);
|
|
55
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
|
+
}
|
|
56
68
|
animationSpeed() {
|
|
57
69
|
return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memoFloat).call(this, 'animationSpeed', __classPrivateFieldGet(this, _Resolver_options, "f").animationSpeed(), 1);
|
|
58
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
|
+
}
|
|
59
100
|
title() {
|
|
60
101
|
return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, 'title', () => __classPrivateFieldGet(this, _Resolver_options, "f").title());
|
|
61
102
|
}
|
|
@@ -232,13 +273,12 @@ _Resolver_style = new WeakMap(), _Resolver_options = new WeakMap(), _Resolver_pr
|
|
|
232
273
|
const source = userColors ?? styleColor?.values() ?? [];
|
|
233
274
|
let candidates = source.map((c) => Color.toHex(c));
|
|
234
275
|
const fixed = this.colorOrder(name) === COLOR_ORDER_FIXED;
|
|
235
|
-
const verbatim = userColors !== undefined && fixed;
|
|
236
276
|
const fill = this.colorFill(name);
|
|
237
277
|
const stops = fill === 'solid'
|
|
238
278
|
? 1
|
|
239
|
-
: __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_colorFillStops).call(this, name,
|
|
279
|
+
: __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_colorFillStops).call(this, name, fixed ? candidates.length : 2);
|
|
240
280
|
if (!styleColor) {
|
|
241
|
-
return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_order).call(this, name, candidates, fixed
|
|
281
|
+
return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_order).call(this, name, candidates, fixed).slice(0, stops);
|
|
242
282
|
}
|
|
243
283
|
// Detect circular references (e.g. a.contrastTo = b, b.contrastTo = a)
|
|
244
284
|
if (__classPrivateFieldGet(this, _Resolver_colorResolving, "f").includes(name)) {
|
|
@@ -248,7 +288,7 @@ _Resolver_style = new WeakMap(), _Resolver_options = new WeakMap(), _Resolver_pr
|
|
|
248
288
|
const contrastTo = styleColor.contrastTo();
|
|
249
289
|
const notEqualTo = styleColor.notEqualTo();
|
|
250
290
|
try {
|
|
251
|
-
if (contrastTo && !
|
|
291
|
+
if (contrastTo && !fixed) {
|
|
252
292
|
const refColor = this.color(contrastTo)[0];
|
|
253
293
|
if (refColor) {
|
|
254
294
|
candidates = Color.sortByContrast(candidates, refColor);
|
|
@@ -270,19 +310,10 @@ _Resolver_style = new WeakMap(), _Resolver_options = new WeakMap(), _Resolver_pr
|
|
|
270
310
|
// Skip shuffle when sorted by contrast to preserve the ordering
|
|
271
311
|
const ordered = contrastTo
|
|
272
312
|
? candidates
|
|
273
|
-
: __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_order).call(this, name, candidates, fixed
|
|
313
|
+
: __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_order).call(this, name, candidates, fixed);
|
|
274
314
|
return ordered.slice(0, stops);
|
|
275
|
-
}, _Resolver_order = function _Resolver_order(name, candidates, fixed
|
|
276
|
-
|
|
277
|
-
return __classPrivateFieldGet(this, _Resolver_prng, "f").shuffle(`${name}Color`, candidates);
|
|
278
|
-
}
|
|
279
|
-
if (verbatim) {
|
|
280
|
-
return candidates;
|
|
281
|
-
}
|
|
282
|
-
// Deprecated: DiceBear 11 will take the palette in its definition order
|
|
283
|
-
// here, the same verbatim rule as user-supplied colors, and drop this
|
|
284
|
-
// sort (see CHANGELOG.md, "Deprecated").
|
|
285
|
-
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);
|
|
286
317
|
}, _Resolver_colorFillStops = function _Resolver_colorFillStops(name, fallback) {
|
|
287
318
|
const range = __classPrivateFieldGet(this, _Resolver_options, "f").colorFillStops(name);
|
|
288
319
|
return range
|
package/lib/Style.d.ts
CHANGED
|
@@ -56,9 +56,9 @@ export declare class Style<D = unknown> {
|
|
|
56
56
|
/**
|
|
57
57
|
* Returns the sorted distinct names of the definition's animation
|
|
58
58
|
* timelines. Computed once and cached. Consumed by the options descriptor
|
|
59
|
-
* so tooling can offer
|
|
60
|
-
* so every port reports the same order regardless of how it walks
|
|
61
|
-
* definition.
|
|
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
62
|
*/
|
|
63
63
|
animationNames(): readonly string[];
|
|
64
64
|
/**
|
package/lib/Style.js
CHANGED
|
@@ -10,7 +10,6 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
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
|
-
import { StyleValidator } from './Validator/StyleValidator.js';
|
|
14
13
|
import { StyleValidationError } from './Error/StyleValidationError.js';
|
|
15
14
|
import { Meta } from './Style/Meta.js';
|
|
16
15
|
import { Canvas } from './Style/Canvas.js';
|
|
@@ -31,7 +30,6 @@ export class Style {
|
|
|
31
30
|
_Style_colors.set(this, void 0);
|
|
32
31
|
_Style_hasAnimations.set(this, void 0);
|
|
33
32
|
_Style_animationNames.set(this, void 0);
|
|
34
|
-
StyleValidator.validate(data);
|
|
35
33
|
__classPrivateFieldSet(this, _Style_data, structuredClone(data), "f");
|
|
36
34
|
__classPrivateFieldGet(this, _Style_instances, "m", _Style_validateAliases).call(this);
|
|
37
35
|
__classPrivateFieldGet(this, _Style_instances, "m", _Style_validateAnimations).call(this);
|
|
@@ -125,9 +123,9 @@ export class Style {
|
|
|
125
123
|
/**
|
|
126
124
|
* Returns the sorted distinct names of the definition's animation
|
|
127
125
|
* timelines. Computed once and cached. Consumed by the options descriptor
|
|
128
|
-
* so tooling can offer
|
|
129
|
-
* so every port reports the same order regardless of how it walks
|
|
130
|
-
* definition.
|
|
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.
|
|
131
129
|
*/
|
|
132
130
|
animationNames() {
|
|
133
131
|
if (__classPrivateFieldGet(this, _Style_animationNames, "f") === undefined) {
|
package/lib/StyleDefinition.d.ts
CHANGED
|
@@ -78,9 +78,10 @@ export interface StyleDefinitionAnimationOrigin {
|
|
|
78
78
|
* `animation` render option is enabled. Absent fields carry these defaults:
|
|
79
79
|
* `delay` 0, `iterations` `'infinite'`, `direction` `'normal'`, `fill`
|
|
80
80
|
* `'none'`, `easing` `'linear'`, `origin` center (50/50). The optional
|
|
81
|
-
* `name` groups the timeline under a user-selectable animation:
|
|
82
|
-
*
|
|
83
|
-
*
|
|
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.
|
|
84
85
|
*/
|
|
85
86
|
export interface StyleDefinitionAnimation {
|
|
86
87
|
readonly name?: string;
|
package/lib/StyleOptions.d.ts
CHANGED
|
@@ -43,8 +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
|
|
46
|
+
readonly animation?: boolean;
|
|
47
47
|
readonly animationSpeed?: number | readonly [number, number];
|
|
48
|
+
readonly animationDelay?: number | readonly [number, number];
|
|
48
49
|
}
|
|
49
50
|
type ComponentVariantOption<D, K extends string> = VariantNames<D, K> | readonly VariantNames<D, K>[] | Readonly<Partial<Record<VariantNames<D, K>, number>>>;
|
|
50
51
|
type IsAlias<D, C extends string> = D extends {
|
|
@@ -68,7 +69,12 @@ type ColorOptions<C extends string> = [C] extends [never] ? unknown : {
|
|
|
68
69
|
} & {
|
|
69
70
|
readonly [K in C as `${K}ColorOrder`]?: StyleOptionsColorOrderValue;
|
|
70
71
|
};
|
|
71
|
-
|
|
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 : {
|
|
72
78
|
readonly [key: string]: unknown;
|
|
73
79
|
});
|
|
74
80
|
export {};
|