@dicebear/core 10.1.0 → 10.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -24
- package/lib/Renderer.js +7 -2
- package/lib/Utils/Color.js +98 -6
- package/lib/Utils/Initials.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,29 +1,51 @@
|
|
|
1
|
-
<h1><img src="https://www.dicebear.com/logo-readme.svg" width="28" /> DiceBear</h1>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
<img src="https://api.dicebear.com/6.x/initials/svg?seed=..&backgroundColor=ade8f4&radius=10" width="64" />
|
|
10
|
-
</p>
|
|
11
|
-
|
|
12
|
-
With DiceBear you can create awesome avatars for your project in no time.
|
|
13
|
-
Whether you are looking for abstract shapes or lovingly designed characters, you
|
|
14
|
-
will find something suitable among our avatar styles. And no matter how and for
|
|
15
|
-
what you want to use the avatars, DiceBear offers the right solution!
|
|
16
|
-
|
|
17
|
-
In addition to purely random avatars, you can also create deterministic avatars
|
|
18
|
-
for user identities. With the built-in PRNG you create the same avatar over and
|
|
19
|
-
over again based on a seed. But also individual avatars are possible! Just use
|
|
20
|
-
the countless options that each avatar style provides.
|
|
21
|
-
|
|
22
|
-
And thanks to the JavaScript library, HTTP API, CLI, Figma plugin and
|
|
23
|
-
Playground, your next avatar is always just a stone's throw away!
|
|
1
|
+
<h1><img src="https://www.dicebear.com/logo-readme.svg" width="28" /> DiceBear Core (JavaScript)</h1>
|
|
2
|
+
|
|
3
|
+
JavaScript implementation of the DiceBear avatar library. Generates deterministic
|
|
4
|
+
SVG avatars from style definitions and a seed string.
|
|
5
|
+
|
|
6
|
+
DiceBear is available for multiple languages. All implementations share the same
|
|
7
|
+
PRNG and rendering pipeline, producing identical SVG output for the same seed,
|
|
8
|
+
style, and options — regardless of the language used.
|
|
24
9
|
|
|
25
10
|
[Playground](https://www.dicebear.com/playground) |
|
|
26
|
-
[Documentation](https://www.dicebear.com/
|
|
11
|
+
[Documentation](https://www.dicebear.com/how-to-use/js-library/)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @dicebear/core
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Requires Node.js 22+ or any modern browser.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import { Avatar } from '@dicebear/core';
|
|
25
|
+
|
|
26
|
+
// From a style definition (here from the @dicebear/styles package)
|
|
27
|
+
import definition from '@dicebear/styles/lorelei.json' with { type: 'json' };
|
|
28
|
+
|
|
29
|
+
const avatar = new Avatar(definition, {
|
|
30
|
+
seed: 'John Doe',
|
|
31
|
+
size: 128,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
avatar.toString(); // SVG string
|
|
35
|
+
avatar.toDataUri(); // data:image/svg+xml;charset=utf-8,...
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Using the Style class
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
import { Style, Avatar } from '@dicebear/core';
|
|
42
|
+
|
|
43
|
+
const style = new Style(definition);
|
|
44
|
+
|
|
45
|
+
// Create multiple avatars from the same style
|
|
46
|
+
const avatar1 = new Avatar(style, { seed: 'Alice' });
|
|
47
|
+
const avatar2 = new Avatar(style, { seed: 'Bob' });
|
|
48
|
+
```
|
|
27
49
|
|
|
28
50
|
## Sponsors
|
|
29
51
|
|
package/lib/Renderer.js
CHANGED
|
@@ -296,8 +296,13 @@ _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap(), _Renderer_d
|
|
|
296
296
|
return '';
|
|
297
297
|
}, _Renderer_resolveVariable = function _Renderer_resolveVariable(name) {
|
|
298
298
|
switch (name) {
|
|
299
|
-
case 'initial':
|
|
300
|
-
|
|
299
|
+
case 'initial': {
|
|
300
|
+
// charAt(0) would return a lone surrogate (ill-formed XML) for
|
|
301
|
+
// supplementary-plane initials; take the full first code point
|
|
302
|
+
// instead, like the PHP/Python/Rust/Go ports.
|
|
303
|
+
const first = __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_initials).call(this).codePointAt(0);
|
|
304
|
+
return first !== undefined ? String.fromCodePoint(first) : '';
|
|
305
|
+
}
|
|
301
306
|
case 'initials':
|
|
302
307
|
return __classPrivateFieldGet(this, _Renderer_instances, "m", _Renderer_initials).call(this);
|
|
303
308
|
case 'fontWeight':
|
package/lib/Utils/Color.js
CHANGED
|
@@ -3,7 +3,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
3
3
|
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");
|
|
4
4
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
5
|
};
|
|
6
|
-
var _a, _Color_linearize;
|
|
6
|
+
var _a, _Color_LINEARIZED, _Color_linearize;
|
|
7
7
|
/**
|
|
8
8
|
* Color helpers used by the renderer and option resolver.
|
|
9
9
|
*/
|
|
@@ -79,9 +79,101 @@ export class Color {
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
_a = Color, _Color_linearize = function _Color_linearize(channel) {
|
|
82
|
-
|
|
83
|
-
if (s <= 0.04045) {
|
|
84
|
-
return s / 12.92;
|
|
85
|
-
}
|
|
86
|
-
return ((s + 0.055) / 1.055) ** 2.4;
|
|
82
|
+
return __classPrivateFieldGet(_a, _a, "f", _Color_LINEARIZED)[channel];
|
|
87
83
|
};
|
|
84
|
+
/**
|
|
85
|
+
* Linear-light value for every 8-bit sRGB channel value, precomputed from
|
|
86
|
+
* `s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4` with
|
|
87
|
+
* `s = channel / 255`.
|
|
88
|
+
*
|
|
89
|
+
* A lookup table because `**` (`Math.pow`) is not required to be correctly
|
|
90
|
+
* rounded: results differ between JS engines and the math libraries used by
|
|
91
|
+
* the other language ports, which would break cross-language byte parity.
|
|
92
|
+
*/
|
|
93
|
+
_Color_LINEARIZED = { value: [
|
|
94
|
+
0.0, 0.0003035269835488375, 0.000607053967097675, 0.0009105809506465125,
|
|
95
|
+
0.00121410793419535, 0.0015176349177441874, 0.001821161901293025,
|
|
96
|
+
0.0021246888848418626, 0.0024282158683907, 0.0027317428519395373,
|
|
97
|
+
0.003035269835488375, 0.003346535763899161, 0.003676507324047436,
|
|
98
|
+
0.004024717018496307, 0.004391442037410293, 0.004776953480693729,
|
|
99
|
+
0.005181516702338386, 0.005605391624202723, 0.006048833022857055,
|
|
100
|
+
0.006512090792594474, 0.006995410187265387, 0.007499032043226175,
|
|
101
|
+
0.008023192985384994, 0.008568125618069307, 0.009134058702220787,
|
|
102
|
+
0.009721217320237847, 0.010329823029626938, 0.010960094006488246,
|
|
103
|
+
0.011612245179743887, 0.012286488356915872, 0.012983032342173012,
|
|
104
|
+
0.013702083047289686, 0.014443843596092545, 0.01520851442291271,
|
|
105
|
+
0.01599629336550963, 0.016807375752887384, 0.017641954488384078,
|
|
106
|
+
0.018500220128379697, 0.019382360956935723, 0.0202885630566524,
|
|
107
|
+
0.021219010376003558, 0.02217388479338738, 0.02315336617811041,
|
|
108
|
+
0.024157632448504756, 0.025186859627361627, 0.026241221894849898,
|
|
109
|
+
0.027320891639074897, 0.028426039504420793, 0.0295568344378088,
|
|
110
|
+
0.030713443732993635, 0.03189603307301153, 0.033104766570885055,
|
|
111
|
+
0.03433980680868217, 0.03560131487502034, 0.03688945040110004,
|
|
112
|
+
0.0382043715953465, 0.03954623527673283, 0.04091519690685319,
|
|
113
|
+
0.042311410620809675, 0.043735029256973465, 0.04518620438567554,
|
|
114
|
+
0.0466650863368801, 0.048171824226889426, 0.04970656598412723,
|
|
115
|
+
0.05126945837404324, 0.052860647023180246, 0.05448027644244237,
|
|
116
|
+
0.05612849004960009, 0.05780543019106723, 0.0595112381629812,
|
|
117
|
+
0.06124605423161761, 0.06301001765316767, 0.06480326669290577,
|
|
118
|
+
0.06662593864377289, 0.06847816984440017, 0.07036009569659588,
|
|
119
|
+
0.07227185068231748, 0.07421356838014963, 0.07618538148130785,
|
|
120
|
+
0.07818742180518633, 0.08021982031446831, 0.0822827071298148,
|
|
121
|
+
0.08437621154414882, 0.08650046203654976, 0.08865558628577294,
|
|
122
|
+
0.09084171118340767, 0.09305896284668747, 0.0953074666309647,
|
|
123
|
+
0.09758734714186246, 0.09989872824711389, 0.1022417330881013,
|
|
124
|
+
0.10461648409110419, 0.10702310297826761, 0.10946171077829933,
|
|
125
|
+
0.1119324278369056, 0.11443537382697373, 0.11697066775851084,
|
|
126
|
+
0.11953842798834562, 0.12213877222960187, 0.12477181756095049,
|
|
127
|
+
0.12743768043564743, 0.1301364766903643, 0.13286832155381798,
|
|
128
|
+
0.13563332965520566, 0.13843161503245183, 0.14126329114027164,
|
|
129
|
+
0.14412847085805777, 0.14702726649759498, 0.14995978981060856,
|
|
130
|
+
0.15292615199615017, 0.1559264637078274, 0.1589608350608804,
|
|
131
|
+
0.16202937563911096, 0.1651321945016676, 0.16826940018969075,
|
|
132
|
+
0.1714411007328226, 0.17464740365558504, 0.17788841598362914,
|
|
133
|
+
0.18116424424986022, 0.184474994500441, 0.18782077230067787,
|
|
134
|
+
0.1912016827407914, 0.19461783044157582, 0.19806931955994886,
|
|
135
|
+
0.20155625379439707, 0.20507873639031693, 0.20863687014525575,
|
|
136
|
+
0.21223075741405523, 0.21586050011389923, 0.21952619972926923,
|
|
137
|
+
0.2232279573168085, 0.22696587351009836, 0.23074004852434915,
|
|
138
|
+
0.23455058216100522, 0.238397573812271, 0.24228112246555486,
|
|
139
|
+
0.24620132670783548, 0.25015828472995344, 0.25415209433082675,
|
|
140
|
+
0.2581828529215958, 0.26225065752969623, 0.26635560480286247,
|
|
141
|
+
0.2704977910130658, 0.27467731206038465, 0.2788942634768104,
|
|
142
|
+
0.2831487404299921, 0.2874408377269175, 0.29177064981753587,
|
|
143
|
+
0.2961382707983211, 0.3005437944157765, 0.3049873140698863,
|
|
144
|
+
0.30946892281750854, 0.31398871337571754, 0.31854677812509186,
|
|
145
|
+
0.32314320911295075, 0.3277780980565422, 0.33245153634617935,
|
|
146
|
+
0.33716361504833037, 0.341914424908661, 0.3467040563550296,
|
|
147
|
+
0.35153259950043936, 0.3564001441459435, 0.3613067797835095,
|
|
148
|
+
0.3662525955988395, 0.3712376804741491, 0.37626212299090644,
|
|
149
|
+
0.3813260114325301, 0.386429433787049, 0.39157247774972326,
|
|
150
|
+
0.39675523072562685, 0.40197777983219574, 0.4072402119017367,
|
|
151
|
+
0.41254261348390375, 0.4178850708481375, 0.4232676699860717,
|
|
152
|
+
0.4286904966139067, 0.4341536361747489, 0.4396571738409188,
|
|
153
|
+
0.44520119451622786, 0.45078578283822346, 0.45641102318040466,
|
|
154
|
+
0.4620769996544071, 0.467783796112159, 0.47353149614800955,
|
|
155
|
+
0.4793201831008268, 0.4851499400560704, 0.4910208498478356,
|
|
156
|
+
0.4969329950608704, 0.5028864580325687, 0.5088813208549338,
|
|
157
|
+
0.5149176653765214, 0.5209955732043543, 0.5271151257058131,
|
|
158
|
+
0.5332764040105052, 0.5394794890121071, 0.5457244613701866,
|
|
159
|
+
0.5520114015120001, 0.5583403896342679, 0.5647115057049292,
|
|
160
|
+
0.5711248294648731, 0.5775804404296506, 0.5840784178911641,
|
|
161
|
+
0.5906188409193369, 0.5972017883637634, 0.6038273388553378,
|
|
162
|
+
0.6104955708078648, 0.6172065624196511, 0.6239603916750761,
|
|
163
|
+
0.6307571363461468, 0.6375968739940326, 0.6444796819705821,
|
|
164
|
+
0.6514056374198242, 0.6583748172794486, 0.665387298282272,
|
|
165
|
+
0.6724431569576875, 0.6795424696330938, 0.6866853124353134,
|
|
166
|
+
0.6938717612919899, 0.7011018919329731, 0.7083757798916868,
|
|
167
|
+
0.7156935005064807, 0.7230551289219693, 0.7304607400903537,
|
|
168
|
+
0.7379104087727308, 0.7454042095403874, 0.7529422167760779,
|
|
169
|
+
0.7605245046752924, 0.7681511472475071, 0.7758222183174236,
|
|
170
|
+
0.7835377915261935, 0.7912979403326302, 0.799102738014409,
|
|
171
|
+
0.8069522576692516, 0.8148465722161012, 0.8227857543962835,
|
|
172
|
+
0.8307698767746546, 0.83879901174074, 0.846873231509858, 0.8549926081242338,
|
|
173
|
+
0.8631572134541023, 0.8713671191987973, 0.8796223968878317,
|
|
174
|
+
0.8879231178819663, 0.8962693533742664, 0.9046611743911496,
|
|
175
|
+
0.9130986517934192, 0.9215818562772946, 0.9301108583754237,
|
|
176
|
+
0.938685728457888, 0.9473065367331999, 0.9559733532492861,
|
|
177
|
+
0.9646862478944651, 0.9734452903984125, 0.9822505503331171,
|
|
178
|
+
0.9911020971138298, 1,
|
|
179
|
+
] };
|
package/lib/Utils/Initials.js
CHANGED
|
@@ -12,7 +12,8 @@ export class Initials {
|
|
|
12
12
|
static fromSeed(seed, discardAtSymbol = true) {
|
|
13
13
|
let input = seed;
|
|
14
14
|
if (discardAtSymbol) {
|
|
15
|
-
|
|
15
|
+
// Strip the whole @ suffix, including any line terminators (dotall `s`).
|
|
16
|
+
input = seed.replace(/@.*/s, '');
|
|
16
17
|
}
|
|
17
18
|
input = input.replace(/[`´'ʼ]/g, '');
|
|
18
19
|
const matches = input.match(/(\p{L}[\p{L}\p{M}]*)/gu);
|