@dicebear/core 5.0.4 → 5.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/core.js +27 -10
- package/lib/schema.js +21 -0
- package/lib/types.d.ts +10 -0
- package/lib/utils/color.d.ts +6 -0
- package/lib/utils/color.js +24 -0
- package/lib/utils/prng.js +13 -3
- package/lib/utils/svg.d.ts +2 -2
- package/lib/utils/svg.js +15 -2
- package/package.json +3 -3
package/lib/core.js
CHANGED
|
@@ -3,12 +3,15 @@ import { merge as mergeOptions } from './utils/options.js';
|
|
|
3
3
|
import { create as createPrng } from './utils/prng.js';
|
|
4
4
|
import * as license from './utils/license.js';
|
|
5
5
|
import { toFormat } from '@dicebear/converter';
|
|
6
|
+
import { getBackgroundColors } from './utils/color.js';
|
|
6
7
|
export function createAvatar(style, options = {}) {
|
|
7
|
-
var _a, _b;
|
|
8
|
+
var _a, _b, _c, _d;
|
|
8
9
|
options = mergeOptions(style, options);
|
|
9
10
|
const prng = createPrng(options.seed);
|
|
10
11
|
const result = style.create({ prng: prng, options });
|
|
11
|
-
const
|
|
12
|
+
const { primary: primaryBackgroundColor, secondary: secondaryBackgroundColor, } = getBackgroundColors(prng, (_a = options.backgroundColor) !== null && _a !== void 0 ? _a : []);
|
|
13
|
+
const backgroundType = prng.pick((_b = options.backgroundType) !== null && _b !== void 0 ? _b : [], 'solid');
|
|
14
|
+
const backgroundRotation = prng.pick((_c = options.backgroundRotation) !== null && _c !== void 0 ? _c : [], 0);
|
|
12
15
|
if (options.size) {
|
|
13
16
|
result.attributes.width = options.size.toString();
|
|
14
17
|
result.attributes.height = options.size.toString();
|
|
@@ -25,26 +28,40 @@ export function createAvatar(style, options = {}) {
|
|
|
25
28
|
if (options.translateX || options.translateY) {
|
|
26
29
|
result.body = svgUtils.addTranslate(result, options.translateX, options.translateY);
|
|
27
30
|
}
|
|
28
|
-
if (
|
|
29
|
-
|
|
31
|
+
if (primaryBackgroundColor !== 'transparent' &&
|
|
32
|
+
secondaryBackgroundColor !== 'transparent') {
|
|
33
|
+
result.body = svgUtils.addBackground(result, primaryBackgroundColor, secondaryBackgroundColor, backgroundType, backgroundRotation);
|
|
30
34
|
}
|
|
31
35
|
if (options.radius || options.clip) {
|
|
32
|
-
result.body = svgUtils.addViewboxMask(result, (
|
|
36
|
+
result.body = svgUtils.addViewboxMask(result, (_d = options.radius) !== null && _d !== void 0 ? _d : 0);
|
|
33
37
|
}
|
|
34
38
|
// Reduces the occurrence of ID collisions when rendering multiple avatars on one HTML page.
|
|
35
39
|
result.body = svgUtils.randomizeIds(result, prng.seed);
|
|
36
40
|
const attributes = svgUtils.createAttrString(result);
|
|
37
41
|
const metadata = license.xml(style);
|
|
38
42
|
const exif = license.exif(style);
|
|
39
|
-
const
|
|
43
|
+
const svg = `<svg ${attributes}>${metadata}${result.body}</svg>`;
|
|
40
44
|
return {
|
|
41
|
-
toString: () =>
|
|
42
|
-
|
|
45
|
+
toString: () => svg,
|
|
46
|
+
toJson: () => {
|
|
47
|
+
var _a;
|
|
48
|
+
return ({
|
|
49
|
+
svg: svg,
|
|
50
|
+
extra: {
|
|
51
|
+
primaryBackgroundColor,
|
|
52
|
+
secondaryBackgroundColor,
|
|
53
|
+
backgroundType,
|
|
54
|
+
backgroundRotation,
|
|
55
|
+
...(_a = result.extra) === null || _a === void 0 ? void 0 : _a.call(result),
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
...toFormat(svg, 'svg'),
|
|
43
60
|
png: ({ includeExif = false } = {}) => {
|
|
44
|
-
return toFormat(
|
|
61
|
+
return toFormat(svg, 'png', includeExif ? exif : undefined);
|
|
45
62
|
},
|
|
46
63
|
jpeg: ({ includeExif = false } = {}) => {
|
|
47
|
-
return toFormat(
|
|
64
|
+
return toFormat(svg, 'jpeg', includeExif ? exif : undefined);
|
|
48
65
|
},
|
|
49
66
|
};
|
|
50
67
|
}
|
package/lib/schema.js
CHANGED
|
@@ -38,6 +38,27 @@ export const schema = {
|
|
|
38
38
|
pattern: '^(transparent|[a-fA-F0-9]{6})$',
|
|
39
39
|
},
|
|
40
40
|
},
|
|
41
|
+
backgroundType: {
|
|
42
|
+
type: 'array',
|
|
43
|
+
items: {
|
|
44
|
+
type: 'string',
|
|
45
|
+
enum: ['solid', 'gradientLinear'],
|
|
46
|
+
},
|
|
47
|
+
default: ['solid'],
|
|
48
|
+
},
|
|
49
|
+
backgroundRotation: {
|
|
50
|
+
type: 'array',
|
|
51
|
+
items: {
|
|
52
|
+
type: 'integer',
|
|
53
|
+
minimum: 0,
|
|
54
|
+
maximum: 360,
|
|
55
|
+
},
|
|
56
|
+
default: [
|
|
57
|
+
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150,
|
|
58
|
+
160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290,
|
|
59
|
+
300, 310, 320, 330, 340, 350,
|
|
60
|
+
],
|
|
61
|
+
},
|
|
41
62
|
translateX: {
|
|
42
63
|
type: 'integer',
|
|
43
64
|
minimum: -100,
|
package/lib/types.d.ts
CHANGED
|
@@ -7,7 +7,12 @@ export interface Result extends ConverterResult {
|
|
|
7
7
|
png(options?: ResultConvertOptions): ConverterResult;
|
|
8
8
|
jpeg(options?: ResultConvertOptions): ConverterResult;
|
|
9
9
|
toString(): string;
|
|
10
|
+
toJson(): {
|
|
11
|
+
svg: string;
|
|
12
|
+
extra: Record<string, unknown>;
|
|
13
|
+
};
|
|
10
14
|
}
|
|
15
|
+
export type BackgroundType = 'solid' | 'gradientLinear';
|
|
11
16
|
export interface Options {
|
|
12
17
|
seed?: string;
|
|
13
18
|
flip?: boolean;
|
|
@@ -16,6 +21,8 @@ export interface Options {
|
|
|
16
21
|
radius?: number;
|
|
17
22
|
size?: number;
|
|
18
23
|
backgroundColor?: string[];
|
|
24
|
+
backgroundType?: BackgroundType[];
|
|
25
|
+
backgroundRotation?: number[];
|
|
19
26
|
translateX?: number;
|
|
20
27
|
translateY?: number;
|
|
21
28
|
clip?: boolean;
|
|
@@ -29,7 +36,9 @@ export interface Prng {
|
|
|
29
36
|
next(): void;
|
|
30
37
|
bool(likelihood?: number): boolean;
|
|
31
38
|
integer(min: number, max: number): number;
|
|
39
|
+
pick<T>(arr: T[], fallback: T): T;
|
|
32
40
|
pick<T>(arr: T[]): T | undefined;
|
|
41
|
+
shuffle<T>(arr: T[]): T[];
|
|
33
42
|
string(length: number, characters?: string): string;
|
|
34
43
|
}
|
|
35
44
|
export type StyleSchema = JSONSchema7;
|
|
@@ -46,6 +55,7 @@ export interface StyleCreateResultAttributes {
|
|
|
46
55
|
export interface StyleCreateResult {
|
|
47
56
|
attributes: StyleCreateResultAttributes;
|
|
48
57
|
body: string;
|
|
58
|
+
extra?: () => Record<string, unknown>;
|
|
49
59
|
}
|
|
50
60
|
export interface StyleMeta {
|
|
51
61
|
title?: string;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function convertColor(color) {
|
|
2
|
+
return 'transparent' === color ? color : `#${color}`;
|
|
3
|
+
}
|
|
4
|
+
export function getBackgroundColors(prng, backgroundColor) {
|
|
5
|
+
var _a;
|
|
6
|
+
let shuffledBackgroundColors;
|
|
7
|
+
if (backgroundColor.length <= 2) {
|
|
8
|
+
// A function call should in any case make an identical number of calls to the PRNG.
|
|
9
|
+
prng.next();
|
|
10
|
+
shuffledBackgroundColors = backgroundColor;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
shuffledBackgroundColors = prng.shuffle(backgroundColor);
|
|
14
|
+
}
|
|
15
|
+
if (shuffledBackgroundColors.length === 0) {
|
|
16
|
+
shuffledBackgroundColors = ['transparent'];
|
|
17
|
+
}
|
|
18
|
+
const primary = shuffledBackgroundColors[0];
|
|
19
|
+
const secondary = (_a = shuffledBackgroundColors[1]) !== null && _a !== void 0 ? _a : shuffledBackgroundColors[0];
|
|
20
|
+
return {
|
|
21
|
+
primary: convertColor(primary),
|
|
22
|
+
secondary: convertColor(secondary),
|
|
23
|
+
};
|
|
24
|
+
}
|
package/lib/utils/prng.js
CHANGED
|
@@ -31,17 +31,27 @@ export function create(seed = '') {
|
|
|
31
31
|
integer(min, max) {
|
|
32
32
|
return integer(min, max);
|
|
33
33
|
},
|
|
34
|
-
pick(arr) {
|
|
34
|
+
pick(arr, fallback) {
|
|
35
|
+
var _a;
|
|
35
36
|
if (arr.length === 0) {
|
|
36
37
|
next();
|
|
37
38
|
return undefined;
|
|
38
39
|
}
|
|
39
|
-
return arr[integer(0, arr.length - 1)];
|
|
40
|
+
return (_a = arr[integer(0, arr.length - 1)]) !== null && _a !== void 0 ? _a : fallback;
|
|
41
|
+
},
|
|
42
|
+
shuffle(arr) {
|
|
43
|
+
// Each method call should call the `next` function only once.
|
|
44
|
+
// Therefore, we use a separate instance of the PRNG here.
|
|
45
|
+
const internalPrng = create(next().toString());
|
|
46
|
+
return arr.sort(() => internalPrng.integer(-1, 1));
|
|
40
47
|
},
|
|
41
48
|
string(length, characters = 'abcdefghijklmnopqrstuvwxyz1234567890') {
|
|
49
|
+
// Each method call should call the `next` function only once.
|
|
50
|
+
// Therefore, we use a separate instance of the PRNG here.
|
|
51
|
+
const internalPrng = create(next().toString());
|
|
42
52
|
let str = '';
|
|
43
53
|
for (let i = 0; i < length; i++) {
|
|
44
|
-
str += characters[integer(0, characters.length - 1)];
|
|
54
|
+
str += characters[internalPrng.integer(0, characters.length - 1)];
|
|
45
55
|
}
|
|
46
56
|
return str;
|
|
47
57
|
},
|
package/lib/utils/svg.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { StyleCreateResult } from '../types.js';
|
|
1
|
+
import type { BackgroundType, StyleCreateResult } from '../types.js';
|
|
2
2
|
export declare function getViewBox(result: StyleCreateResult): {
|
|
3
3
|
x: number;
|
|
4
4
|
y: number;
|
|
5
5
|
width: number;
|
|
6
6
|
height: number;
|
|
7
7
|
};
|
|
8
|
-
export declare function
|
|
8
|
+
export declare function addBackground(result: StyleCreateResult, primaryColor: string, secondaryColor: string, type: BackgroundType, rotation: number): string;
|
|
9
9
|
export declare function addScale(result: StyleCreateResult, scale: number): string;
|
|
10
10
|
export declare function addTranslate(result: StyleCreateResult, x?: number, y?: number): string;
|
|
11
11
|
export declare function addRotate(result: StyleCreateResult, rotate: number): string;
|
package/lib/utils/svg.js
CHANGED
|
@@ -13,9 +13,22 @@ export function getViewBox(result) {
|
|
|
13
13
|
height,
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
|
-
export function
|
|
16
|
+
export function addBackground(result, primaryColor, secondaryColor, type, rotation) {
|
|
17
17
|
let { width, height, x, y } = getViewBox(result);
|
|
18
|
-
|
|
18
|
+
const solidBackground = `<rect fill="#${primaryColor}" width="${width}" height="${height}" x="${x}" y="${y}" />`;
|
|
19
|
+
switch (type) {
|
|
20
|
+
case 'solid':
|
|
21
|
+
return solidBackground + result.body;
|
|
22
|
+
case 'gradientLinear':
|
|
23
|
+
return (`<rect fill="url(#backgroundLinear)" width="${width}" height="${height}" x="${x}" y="${y}" />` +
|
|
24
|
+
`<defs>` +
|
|
25
|
+
`<linearGradient id="backgroundLinear" gradientTransform="rotate(${rotation} 0.5 0.5)">` +
|
|
26
|
+
`<stop stop-color="${primaryColor}"/>` +
|
|
27
|
+
`<stop offset="1" stop-color="${secondaryColor}"/>` +
|
|
28
|
+
`</linearGradient>` +
|
|
29
|
+
`</defs>` +
|
|
30
|
+
result.body);
|
|
31
|
+
}
|
|
19
32
|
}
|
|
20
33
|
export function addScale(result, scale) {
|
|
21
34
|
let { width, height, x, y } = getViewBox(result);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dicebear/core",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.1.1",
|
|
4
4
|
"description": "An avatar library for designers and developers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"avatar",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"test": "uvu tests"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@dicebear/converter": "^5.
|
|
35
|
+
"@dicebear/converter": "^5.1.1"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@tsconfig/recommended": "^1.0.1",
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"publishConfig": {
|
|
47
47
|
"access": "public"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "0c544971431b02f8783598b4f821a6341b9fff0a"
|
|
50
50
|
}
|