@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/README.md +19 -4
- package/lib/Avatar.d.ts +4 -6
- package/lib/Avatar.js +5 -13
- package/lib/Options.d.ts +18 -0
- package/lib/Options.js +30 -2
- package/lib/OptionsDescriptor.js +18 -0
- package/lib/Renderer.js +219 -19
- package/lib/Resolver.d.ts +24 -0
- package/lib/Resolver.js +56 -16
- package/lib/Style/Element.d.ts +6 -1
- package/lib/Style/Element.js +7 -0
- package/lib/Style.d.ts +15 -0
- package/lib/Style.js +83 -4
- package/lib/StyleDefinition.d.ts +57 -0
- package/lib/StyleOptions.d.ts +9 -1
- package/lib/Validator/OptionsValidator.js +761 -508
- package/lib/Validator/StyleValidator.js +1078 -282
- 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
|
@@ -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/
|
|
14
|
+
[Documentation](https://www.dicebear.com/integrations/javascript/)
|
|
15
15
|
|
|
16
16
|
## Installation
|
|
17
17
|
|
|
@@ -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,6 +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;
|
|
29
|
+
/**
|
|
30
|
+
* Returns the `${name}Animation` switch for one animation name, or
|
|
31
|
+
* `undefined` when unset.
|
|
32
|
+
*/
|
|
33
|
+
animationFor(name: string): boolean | undefined;
|
|
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;
|
|
28
46
|
/**
|
|
29
47
|
* Returns the global `tags` filter as parsed tokens, or an empty array when
|
|
30
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,6 +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
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns the `${name}Animation` switch for one animation name, or
|
|
71
|
+
* `undefined` when unset.
|
|
72
|
+
*/
|
|
73
|
+
animationFor(name) {
|
|
74
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_dynamic).call(this, `${name}Animation`);
|
|
75
|
+
}
|
|
76
|
+
animationSpeed() {
|
|
77
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_toRange).call(this, __classPrivateFieldGet(this, _Options_data, "f").animationSpeed);
|
|
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
|
+
}
|
|
68
96
|
/**
|
|
69
97
|
* Returns the global `tags` filter as parsed tokens, or an empty array when
|
|
70
98
|
* unset. Each raw token (`category` / `category:value`, optionally
|
package/lib/OptionsDescriptor.js
CHANGED
|
@@ -107,6 +107,24 @@ _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 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.
|
|
114
|
+
if (__classPrivateFieldGet(this, _OptionsDescriptor_style, "f").hasAnimations()) {
|
|
115
|
+
result.animation = { type: 'boolean' };
|
|
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
|
+
}
|
|
127
|
+
}
|
|
110
128
|
return result;
|
|
111
129
|
};
|
|
112
130
|
_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
|
-
},
|
|
141
|
-
|
|
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,21 +188,26 @@ _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());
|
|
182
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));
|
|
183
207
|
if (children.length === 0) {
|
|
184
|
-
|
|
185
|
-
// component came up empty, has no content left to group. It draws
|
|
186
|
-
// nothing either way, but a masked group without content has an empty
|
|
187
|
-
// bounding box, and strict SVG parsers reject the whole document over
|
|
188
|
-
// it. Wrappers that carry an id stay, so references keep resolving.
|
|
189
|
-
if (element.children().length > 0 &&
|
|
190
|
-
element.attributes()?.id === undefined) {
|
|
191
|
-
return '';
|
|
192
|
-
}
|
|
193
|
-
return `<${name}${attrs}/>`;
|
|
208
|
+
return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<${name}${attrs}/>`, element, active);
|
|
194
209
|
}
|
|
195
|
-
return `<${name}${attrs}>${children}</${name}
|
|
210
|
+
return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<${name}${attrs}>${children}</${name}>`, element, active);
|
|
196
211
|
}, _Renderer_renderTextElement = function _Renderer_renderTextElement(element) {
|
|
197
212
|
const value = element.value();
|
|
198
213
|
return value !== undefined ? Xml.escape(__classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_resolveValue).call(this, value)) : '';
|
|
@@ -219,7 +234,8 @@ _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_d
|
|
|
219
234
|
__classPrivateFieldGet(this, _Renderer_defs, "f").set(id, `<g id="${id}">${body}</g>`);
|
|
220
235
|
}
|
|
221
236
|
const transforms = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_buildTransforms).call(this, component);
|
|
222
|
-
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);
|
|
223
239
|
let mergedAttributes = userAttributes;
|
|
224
240
|
if (transforms.length > 0) {
|
|
225
241
|
const userTransform = userAttributes?.transform;
|
|
@@ -229,7 +245,7 @@ _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_d
|
|
|
229
245
|
mergedAttributes = { ...userAttributes, transform: allParts.join(' ') };
|
|
230
246
|
}
|
|
231
247
|
const attrs = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_renderAttributes).call(this, mergedAttributes);
|
|
232
|
-
return `<use${attrs} href="#${id}"
|
|
248
|
+
return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_applyAnimations).call(this, `<use${attrs} href="#${id}"/>`, element, active);
|
|
233
249
|
}, _Renderer_buildTransforms = function _Renderer_buildTransforms(component) {
|
|
234
250
|
const { rotate, translateX, translateY, scale } = __classPrivateFieldGet(this, _Renderer_resolver, "f").componentTransform(component.name());
|
|
235
251
|
if (translateX === 0 && translateY === 0 && rotate === 0 && scale === 1) {
|
|
@@ -323,4 +339,188 @@ _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_d
|
|
|
323
339
|
return (__classPrivateFieldSet(this, _Renderer_cachedInitials, __classPrivateFieldGet(this, _Renderer_cachedInitials, "f") ?? Initials.fromSeed(__classPrivateFieldGet(this, _Renderer_resolver, "f").seed()), "f"));
|
|
324
340
|
}, _Renderer_hashSeed = function _Renderer_hashSeed() {
|
|
325
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"));
|
|
342
|
+
}, _Renderer_animationHash = function _Renderer_animationHash() {
|
|
343
|
+
if (__classPrivateFieldGet(this, _Renderer_cachedAnimationHash, "f") !== undefined) {
|
|
344
|
+
return __classPrivateFieldGet(this, _Renderer_cachedAnimationHash, "f");
|
|
345
|
+
}
|
|
346
|
+
const random = __classPrivateFieldGet(this, _Renderer_resolver, "f").idRandomization()
|
|
347
|
+
? ':' + __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_randomSuffix).call(this)
|
|
348
|
+
: '';
|
|
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() ?? '') +
|
|
360
|
+
':' +
|
|
361
|
+
__classPrivateFieldGet(this, _Renderer_resolver, "f").seed() +
|
|
362
|
+
':' +
|
|
363
|
+
Number.format(__classPrivateFieldGet(this, _Renderer_resolver, "f").animationSpeed()) +
|
|
364
|
+
':' +
|
|
365
|
+
Number.format(__classPrivateFieldGet(this, _Renderer_resolver, "f").animationDelay()) +
|
|
366
|
+
named +
|
|
367
|
+
random), "f"));
|
|
368
|
+
}, _Renderer_activeAnimations = function _Renderer_activeAnimations(element) {
|
|
369
|
+
const animations = element.animations();
|
|
370
|
+
if (animations.length === 0) {
|
|
371
|
+
return animations;
|
|
372
|
+
}
|
|
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) {
|
|
379
|
+
if (attributes?.opacity === undefined ||
|
|
380
|
+
!active.some((animation) => animation.tracks.opacity !== undefined)) {
|
|
381
|
+
return attributes;
|
|
382
|
+
}
|
|
383
|
+
const rest = { ...attributes };
|
|
384
|
+
delete rest.opacity;
|
|
385
|
+
return rest;
|
|
386
|
+
}, _Renderer_applyAnimations = function _Renderer_applyAnimations(markup, element, active) {
|
|
387
|
+
if (markup.length === 0 || active.length === 0) {
|
|
388
|
+
return markup;
|
|
389
|
+
}
|
|
390
|
+
const classes = [];
|
|
391
|
+
let opacityWrapper = -1;
|
|
392
|
+
for (const animation of active) {
|
|
393
|
+
for (const track of __classPrivateFieldGet(_a, _a, "f", _Renderer_trackOrder)) {
|
|
394
|
+
const trackData = animation.tracks[track];
|
|
395
|
+
if (trackData !== undefined) {
|
|
396
|
+
if (track === 'opacity') {
|
|
397
|
+
opacityWrapper = classes.length;
|
|
398
|
+
}
|
|
399
|
+
classes.push(__classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_buildAnimationCss).call(this, animation, track, trackData.keyframes));
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
const restingOpacity = element.attributes()?.opacity;
|
|
404
|
+
let result = markup;
|
|
405
|
+
for (let i = classes.length - 1; i >= 0; i--) {
|
|
406
|
+
const opacity = i === opacityWrapper && restingOpacity !== undefined
|
|
407
|
+
? __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_renderAttributes).call(this, { opacity: restingOpacity })
|
|
408
|
+
: '';
|
|
409
|
+
result = `<g class="${classes[i]}"${opacity}>${result}</g>`;
|
|
410
|
+
}
|
|
411
|
+
return result;
|
|
412
|
+
}, _Renderer_buildAnimationCss = function _Renderer_buildAnimationCss(animation, track, keyframes) {
|
|
413
|
+
var _b, _c, _d, _e;
|
|
414
|
+
const defaultEasing = animation.easing ?? 'linear';
|
|
415
|
+
const body = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_keyframesBody).call(this, track, keyframes, defaultEasing);
|
|
416
|
+
let keyframesName = __classPrivateFieldGet(this, _Renderer_keyframesByContent, "f").get(body);
|
|
417
|
+
if (keyframesName === undefined) {
|
|
418
|
+
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}`;
|
|
419
|
+
__classPrivateFieldGet(this, _Renderer_keyframesByContent, "f").set(body, keyframesName);
|
|
420
|
+
__classPrivateFieldGet(this, _Renderer_keyframesCss, "f").push(`@keyframes ${keyframesName}{${body}}`);
|
|
421
|
+
}
|
|
422
|
+
const speed = __classPrivateFieldGet(this, _Renderer_resolver, "f").animationSpeedFor(animation.name);
|
|
423
|
+
const duration = Number.format(animation.duration / speed);
|
|
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));
|
|
428
|
+
const iterations = animation.iterations === undefined || animation.iterations === 'infinite'
|
|
429
|
+
? 'infinite'
|
|
430
|
+
: Number.format(animation.iterations);
|
|
431
|
+
const direction = __classPrivateFieldGet(_a, _a, "f", _Renderer_directionCss)[animation.direction ?? 'normal'];
|
|
432
|
+
const fill = animation.fill ?? 'none';
|
|
433
|
+
// Only rotate and scale pivot around a point. Translation and opacity
|
|
434
|
+
// need no origin, so their rules skip the transform-box prefix.
|
|
435
|
+
const needsOrigin = track === 'rotate' || track === 'scaleX' || track === 'scaleY';
|
|
436
|
+
const origin = animation.origin ?? { x: 50, y: 50 };
|
|
437
|
+
const originCss = needsOrigin
|
|
438
|
+
? `transform-box:fill-box;transform-origin:${Number.format(origin.x)}% ${Number.format(origin.y)}%;`
|
|
439
|
+
: '';
|
|
440
|
+
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}`;
|
|
441
|
+
// The name comes last in the shorthand so it can never be mistaken for a
|
|
442
|
+
// keyword. All seven tokens are always emitted so every port serializes
|
|
443
|
+
// the same string.
|
|
444
|
+
__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}}`);
|
|
445
|
+
return className;
|
|
446
|
+
}, _Renderer_keyframesBody = function _Renderer_keyframesBody(track, keyframes, defaultEasing) {
|
|
447
|
+
const list = [...keyframes];
|
|
448
|
+
if (list[0].at > 0) {
|
|
449
|
+
list.unshift({ at: 0, value: list[0].value });
|
|
450
|
+
}
|
|
451
|
+
const last = list[list.length - 1];
|
|
452
|
+
if (last.at < 100) {
|
|
453
|
+
list.push({ at: 100, value: last.value });
|
|
454
|
+
}
|
|
455
|
+
const defaultCss = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_easingCss).call(this, defaultEasing);
|
|
456
|
+
return list
|
|
457
|
+
.map((keyframe, index) => {
|
|
458
|
+
const easingCss = keyframe.easing !== undefined && index < list.length - 1
|
|
459
|
+
? __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_easingCss).call(this, keyframe.easing)
|
|
460
|
+
: defaultCss;
|
|
461
|
+
const timing = easingCss !== defaultCss
|
|
462
|
+
? `;animation-timing-function:${easingCss}`
|
|
463
|
+
: '';
|
|
464
|
+
return `${Number.format(keyframe.at)}%{${__classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_trackDeclaration).call(this, track, keyframe.value)}${timing}}`;
|
|
465
|
+
})
|
|
466
|
+
.join('');
|
|
467
|
+
}, _Renderer_trackDeclaration = function _Renderer_trackDeclaration(track, value) {
|
|
468
|
+
const v = Number.format(value);
|
|
469
|
+
switch (track) {
|
|
470
|
+
case 'translateX':
|
|
471
|
+
return `transform:translateX(${v}px)`;
|
|
472
|
+
case 'translateY':
|
|
473
|
+
return `transform:translateY(${v}px)`;
|
|
474
|
+
case 'rotate':
|
|
475
|
+
return `transform:rotate(${v}deg)`;
|
|
476
|
+
case 'scaleX':
|
|
477
|
+
return `transform:scaleX(${v})`;
|
|
478
|
+
case 'scaleY':
|
|
479
|
+
return `transform:scaleY(${v})`;
|
|
480
|
+
case 'opacity':
|
|
481
|
+
return `opacity:${v}`;
|
|
482
|
+
}
|
|
483
|
+
}, _Renderer_easingCss = function _Renderer_easingCss(easing) {
|
|
484
|
+
if (typeof easing === 'string') {
|
|
485
|
+
return __classPrivateFieldGet(_a, _a, "f", _Renderer_easingKeywordCss)[easing];
|
|
486
|
+
}
|
|
487
|
+
return `cubic-bezier(${Number.format(easing.x1)}, ${Number.format(easing.y1)}, ${Number.format(easing.x2)}, ${Number.format(easing.y2)})`;
|
|
488
|
+
}, _Renderer_registerAnimationStyle = function _Renderer_registerAnimationStyle() {
|
|
489
|
+
if (__classPrivateFieldGet(this, _Renderer_keyframesCss, "f").length === 0) {
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
// An authored def may carry the id `animation-style`, so the CSS takes
|
|
493
|
+
// the next free key rather than replacing that def.
|
|
494
|
+
let key = 'animation-style';
|
|
495
|
+
while (__classPrivateFieldGet(this, _Renderer_defs, "f").has(key)) {
|
|
496
|
+
key += '-';
|
|
497
|
+
}
|
|
498
|
+
__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
499
|
};
|
|
500
|
+
/**
|
|
501
|
+
* The canonical track order, outermost wrapper to innermost. It mirrors the
|
|
502
|
+
* usual translate → rotate → scale transform composition. Every port must
|
|
503
|
+
* wrap in this exact order for the outputs to stay byte-identical.
|
|
504
|
+
*/
|
|
505
|
+
_Renderer_trackOrder = { value: [
|
|
506
|
+
'translateX',
|
|
507
|
+
'translateY',
|
|
508
|
+
'rotate',
|
|
509
|
+
'scaleX',
|
|
510
|
+
'scaleY',
|
|
511
|
+
'opacity',
|
|
512
|
+
] };
|
|
513
|
+
_Renderer_directionCss = { value: {
|
|
514
|
+
normal: 'normal',
|
|
515
|
+
reverse: 'reverse',
|
|
516
|
+
alternate: 'alternate',
|
|
517
|
+
alternateReverse: 'alternate-reverse',
|
|
518
|
+
} };
|
|
519
|
+
_Renderer_easingKeywordCss = { value: {
|
|
520
|
+
linear: 'linear',
|
|
521
|
+
ease: 'ease',
|
|
522
|
+
easeIn: 'ease-in',
|
|
523
|
+
easeOut: 'ease-out',
|
|
524
|
+
easeInOut: 'ease-in-out',
|
|
525
|
+
hold: 'step-end',
|
|
526
|
+
} };
|
package/lib/Resolver.d.ts
CHANGED
|
@@ -16,6 +16,30 @@ export declare class Resolver<D = unknown> {
|
|
|
16
16
|
seed(): string;
|
|
17
17
|
size(): number | undefined;
|
|
18
18
|
idRandomization(): 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;
|
|
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;
|
|
19
43
|
title(): string | undefined;
|
|
20
44
|
flip(): StyleOptionsFlipValue;
|
|
21
45
|
fontFamily(): string;
|