@dicebear/core 5.0.0-alpha.3 → 5.0.0-alpha.32
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/LICENSE +1 -1
- package/lib/core.d.ts +2 -0
- package/lib/core.js +50 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +11 -0
- package/lib/schema.d.ts +2 -0
- package/lib/schema.js +71 -0
- package/{dist → lib}/types.d.ts +63 -54
- package/lib/types.js +1 -0
- package/lib/utils/escape.d.ts +1 -0
- package/lib/utils/escape.js +8 -0
- package/lib/utils/license.d.ts +3 -0
- package/lib/utils/license.js +60 -0
- package/lib/utils/options.d.ts +3 -0
- package/lib/utils/options.js +34 -0
- package/{dist → lib}/utils/prng.d.ts +2 -2
- package/lib/utils/prng.js +46 -0
- package/lib/utils/svg.d.ts +15 -0
- package/lib/utils/svg.js +71 -0
- package/package.json +19 -21
- package/dist/core.d.ts +0 -3
- package/dist/index.d.ts +0 -12
- package/dist/index.es.js +0 -523
- package/dist/index.js +0 -530
- package/dist/index.umd.js +0 -8
- package/dist/options.d.ts +0 -18
- package/dist/utils/escape.d.ts +0 -1
- package/dist/utils/index.d.ts +0 -5
- package/dist/utils/options.d.ts +0 -2
- package/dist/utils/schema.d.ts +0 -5
- package/dist/utils/svg.d.ts +0 -39
package/LICENSE
CHANGED
package/lib/core.d.ts
ADDED
package/lib/core.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import * as svgUtils from './utils/svg.js';
|
|
2
|
+
import { merge as mergeOptions } from './utils/options.js';
|
|
3
|
+
import { create as createPrng } from './utils/prng.js';
|
|
4
|
+
import * as license from './utils/license.js';
|
|
5
|
+
import { toFormat } from '@dicebear/converter';
|
|
6
|
+
export function createAvatar(style, options = {}) {
|
|
7
|
+
var _a, _b;
|
|
8
|
+
options = mergeOptions(style, options);
|
|
9
|
+
const prng = createPrng(options.seed);
|
|
10
|
+
const result = style.create({ prng: prng, options });
|
|
11
|
+
const backgroundColor = prng.pick((_a = options.backgroundColor) !== null && _a !== void 0 ? _a : []);
|
|
12
|
+
if (options.size) {
|
|
13
|
+
result.attributes.width = options.size.toString();
|
|
14
|
+
result.attributes.height = options.size.toString();
|
|
15
|
+
}
|
|
16
|
+
if (options.scale !== undefined && options.scale !== 100) {
|
|
17
|
+
result.body = svgUtils.addScale(result, options.scale);
|
|
18
|
+
}
|
|
19
|
+
if (options.flip) {
|
|
20
|
+
result.body = svgUtils.addFlip(result);
|
|
21
|
+
}
|
|
22
|
+
if (options.rotate) {
|
|
23
|
+
result.body = svgUtils.addRotate(result, options.rotate);
|
|
24
|
+
}
|
|
25
|
+
if (options.translateX || options.translateY) {
|
|
26
|
+
result.body = svgUtils.addTranslate(result, options.translateX, options.translateY);
|
|
27
|
+
}
|
|
28
|
+
if (backgroundColor && backgroundColor !== 'transparent') {
|
|
29
|
+
result.body = svgUtils.addBackgroundColor(result, backgroundColor);
|
|
30
|
+
}
|
|
31
|
+
if (options.radius || options.clip) {
|
|
32
|
+
result.body = svgUtils.addViewboxMask(result, (_b = options.radius) !== null && _b !== void 0 ? _b : 0);
|
|
33
|
+
}
|
|
34
|
+
// Reduces the occurrence of ID collisions when rendering multiple avatars on one HTML page.
|
|
35
|
+
result.body = svgUtils.randomizeIds(result, prng.seed);
|
|
36
|
+
const attributes = svgUtils.createAttrString(result);
|
|
37
|
+
const metadata = license.xml(style);
|
|
38
|
+
const exif = license.exif(style);
|
|
39
|
+
const avatar = `<svg ${attributes}>${metadata}${result.body}</svg>`;
|
|
40
|
+
return {
|
|
41
|
+
toString: () => avatar,
|
|
42
|
+
...toFormat(avatar, 'svg'),
|
|
43
|
+
png: ({ includeExif = false } = {}) => {
|
|
44
|
+
return toFormat(avatar, 'png', includeExif ? exif : undefined);
|
|
45
|
+
},
|
|
46
|
+
jpeg: ({ includeExif = false } = {}) => {
|
|
47
|
+
return toFormat(avatar, 'jpeg', includeExif ? exif : undefined);
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* DiceBear (@dicebear/core)
|
|
3
|
+
*
|
|
4
|
+
* Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
|
|
5
|
+
* Copyright (c) 2022 Florian Körner
|
|
6
|
+
*/
|
|
7
|
+
export * from './core.js';
|
|
8
|
+
export * from './schema.js';
|
|
9
|
+
export * as license from './utils/license.js';
|
|
10
|
+
export * as escape from './utils/escape.js';
|
|
11
|
+
export * from './types.js';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* DiceBear (@dicebear/core)
|
|
3
|
+
*
|
|
4
|
+
* Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
|
|
5
|
+
* Copyright (c) 2022 Florian Körner
|
|
6
|
+
*/
|
|
7
|
+
export * from './core.js';
|
|
8
|
+
export * from './schema.js';
|
|
9
|
+
export * as license from './utils/license.js';
|
|
10
|
+
export * as escape from './utils/escape.js';
|
|
11
|
+
export * from './types.js';
|
package/lib/schema.d.ts
ADDED
package/lib/schema.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export const schema = {
|
|
2
|
+
type: 'object',
|
|
3
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
4
|
+
title: 'Options',
|
|
5
|
+
properties: {
|
|
6
|
+
seed: {
|
|
7
|
+
type: 'string',
|
|
8
|
+
examples: ['Jane Doe', 'John Doe'],
|
|
9
|
+
},
|
|
10
|
+
flip: {
|
|
11
|
+
type: 'boolean',
|
|
12
|
+
default: false,
|
|
13
|
+
examples: [true, false],
|
|
14
|
+
},
|
|
15
|
+
rotate: {
|
|
16
|
+
type: 'integer',
|
|
17
|
+
minimum: 0,
|
|
18
|
+
maximum: 360,
|
|
19
|
+
default: 0,
|
|
20
|
+
examples: [0, 45, 90, 135, 180, 225, 270, 315],
|
|
21
|
+
},
|
|
22
|
+
scale: {
|
|
23
|
+
type: 'integer',
|
|
24
|
+
minimum: 0,
|
|
25
|
+
maximum: 200,
|
|
26
|
+
default: 100,
|
|
27
|
+
examples: [50, 100, 150],
|
|
28
|
+
},
|
|
29
|
+
radius: {
|
|
30
|
+
type: 'integer',
|
|
31
|
+
minimum: 0,
|
|
32
|
+
maximum: 50,
|
|
33
|
+
default: 0,
|
|
34
|
+
examples: [0, 10, 20, 30, 40, 50],
|
|
35
|
+
},
|
|
36
|
+
size: {
|
|
37
|
+
type: 'integer',
|
|
38
|
+
minimum: 1,
|
|
39
|
+
examples: [128, 256, 512, 1024],
|
|
40
|
+
},
|
|
41
|
+
backgroundColor: {
|
|
42
|
+
type: 'array',
|
|
43
|
+
uniqueItems: true,
|
|
44
|
+
items: {
|
|
45
|
+
type: 'string',
|
|
46
|
+
pattern: '^#([a-fA-F0-9]{3}|[a-fA-F0-9]{4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$',
|
|
47
|
+
},
|
|
48
|
+
examples: ['ffd5dc', 'ffdfbf', 'c0eade', 'b6e3f4', 'd1d4f9'],
|
|
49
|
+
},
|
|
50
|
+
translateX: {
|
|
51
|
+
type: 'integer',
|
|
52
|
+
minimum: -100,
|
|
53
|
+
maximum: 100,
|
|
54
|
+
default: 0,
|
|
55
|
+
examples: [-50, -25, 0, 25, 50],
|
|
56
|
+
},
|
|
57
|
+
translateY: {
|
|
58
|
+
type: 'integer',
|
|
59
|
+
minimum: -100,
|
|
60
|
+
maximum: 100,
|
|
61
|
+
default: 0,
|
|
62
|
+
examples: [-50, -25, 0, 25, 50],
|
|
63
|
+
},
|
|
64
|
+
clip: {
|
|
65
|
+
type: 'boolean',
|
|
66
|
+
default: true,
|
|
67
|
+
examples: [true, false],
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
additionalProperties: false,
|
|
71
|
+
};
|
package/{dist → lib}/types.d.ts
RENAMED
|
@@ -1,54 +1,63 @@
|
|
|
1
|
-
import type { JSONSchema7 } from 'json-schema';
|
|
2
|
-
import type {
|
|
3
|
-
export interface
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export interface
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
export declare type
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
export interface
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
1
|
+
import type { JSONSchema7 } from 'json-schema';
|
|
2
|
+
import type { Result as ConverterResult } from '@dicebear/converter';
|
|
3
|
+
export interface ResultConvertOptions {
|
|
4
|
+
includeExif?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export interface Result extends ConverterResult {
|
|
7
|
+
png(options?: ResultConvertOptions): ConverterResult;
|
|
8
|
+
jpeg(options?: ResultConvertOptions): ConverterResult;
|
|
9
|
+
toString(): string;
|
|
10
|
+
}
|
|
11
|
+
export interface Options {
|
|
12
|
+
seed?: string;
|
|
13
|
+
flip?: boolean;
|
|
14
|
+
rotate?: number;
|
|
15
|
+
scale?: number;
|
|
16
|
+
radius?: number;
|
|
17
|
+
size?: number;
|
|
18
|
+
backgroundColor?: string[];
|
|
19
|
+
translateX?: number;
|
|
20
|
+
translateY?: number;
|
|
21
|
+
clip?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface Exif {
|
|
24
|
+
[key: string]: string;
|
|
25
|
+
}
|
|
26
|
+
export declare type SchemaDefaults = Record<string, unknown>;
|
|
27
|
+
export interface Prng {
|
|
28
|
+
seed: string;
|
|
29
|
+
bool(likelihood?: number): boolean;
|
|
30
|
+
integer(min: number, max: number): number;
|
|
31
|
+
pick<T>(arr: T[]): T | undefined;
|
|
32
|
+
string(length: number, characters?: string): string;
|
|
33
|
+
}
|
|
34
|
+
export declare type StyleSchema = JSONSchema7;
|
|
35
|
+
export declare type StyleOptions<O extends {}> = Partial<O & Options>;
|
|
36
|
+
export interface StyleCreateProps<O> {
|
|
37
|
+
prng: Prng;
|
|
38
|
+
options: StyleOptions<O>;
|
|
39
|
+
}
|
|
40
|
+
export declare type StyleCreate<O extends {}> = (props: StyleCreateProps<O>) => StyleCreateResult;
|
|
41
|
+
export interface StyleCreateResultAttributes {
|
|
42
|
+
viewBox: string;
|
|
43
|
+
[key: string]: string;
|
|
44
|
+
}
|
|
45
|
+
export interface StyleCreateResult {
|
|
46
|
+
attributes: StyleCreateResultAttributes;
|
|
47
|
+
body: string;
|
|
48
|
+
}
|
|
49
|
+
export interface StyleMeta {
|
|
50
|
+
title?: string;
|
|
51
|
+
source?: string;
|
|
52
|
+
creator?: string;
|
|
53
|
+
homepage?: string;
|
|
54
|
+
license?: {
|
|
55
|
+
name: string;
|
|
56
|
+
url: string;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export interface Style<O extends {}> {
|
|
60
|
+
meta: StyleMeta;
|
|
61
|
+
schema: StyleSchema;
|
|
62
|
+
create: StyleCreate<O>;
|
|
63
|
+
}
|
package/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function xml(content: string): string;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as _ from './escape.js';
|
|
2
|
+
export function xml(style) {
|
|
3
|
+
var _a, _b, _c, _d, _e;
|
|
4
|
+
const title = (_a = style.meta.title) !== null && _a !== void 0 ? _a : 'Unnamed';
|
|
5
|
+
const creator = (_b = style.meta.creator) !== null && _b !== void 0 ? _b : 'Unknown';
|
|
6
|
+
let description = `"${title}" by "${creator}"`;
|
|
7
|
+
if ((_c = style.meta.license) === null || _c === void 0 ? void 0 : _c.name) {
|
|
8
|
+
description += `, licensed under "${style.meta.license.name}".`;
|
|
9
|
+
}
|
|
10
|
+
description += ' / Remix of the original. - Created with dicebear.com';
|
|
11
|
+
const xmlTitle = `<dc:title>${_.xml(title)}</dc:title>`;
|
|
12
|
+
const xmlCreator = '<dc:creator>' +
|
|
13
|
+
`<cc:Agent rdf:about="${_.xml((_d = style.meta.homepage) !== null && _d !== void 0 ? _d : '')}">` +
|
|
14
|
+
`<dc:title>${_.xml(creator)}</dc:title>` +
|
|
15
|
+
'</cc:Agent>' +
|
|
16
|
+
'</dc:creator>';
|
|
17
|
+
const xmlSource = style.meta.source
|
|
18
|
+
? `<dc:source>${_.xml(style.meta.source)}</dc:source>`
|
|
19
|
+
: '';
|
|
20
|
+
const xmlLicense = ((_e = style.meta.license) === null || _e === void 0 ? void 0 : _e.url)
|
|
21
|
+
? `<cc:license rdf:resource="${_.xml(style.meta.license.url)}" />`
|
|
22
|
+
: '';
|
|
23
|
+
return (`<desc>${description}</desc>` +
|
|
24
|
+
'<metadata' +
|
|
25
|
+
' xmlns:dc="http://purl.org/dc/elements/1.1/"' +
|
|
26
|
+
' xmlns:cc="http://creativecommons.org/ns#"' +
|
|
27
|
+
' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">' +
|
|
28
|
+
'<rdf:RDF>' +
|
|
29
|
+
'<cc:Work>' +
|
|
30
|
+
xmlTitle +
|
|
31
|
+
xmlCreator +
|
|
32
|
+
xmlSource +
|
|
33
|
+
xmlLicense +
|
|
34
|
+
'</cc:Work>' +
|
|
35
|
+
'</rdf:RDF>' +
|
|
36
|
+
'</metadata>');
|
|
37
|
+
}
|
|
38
|
+
export function exif(style) {
|
|
39
|
+
var _a, _b, _c, _d;
|
|
40
|
+
const title = (_a = style.meta.title) !== null && _a !== void 0 ? _a : 'Unnamed';
|
|
41
|
+
const creator = (_b = style.meta.creator) !== null && _b !== void 0 ? _b : 'Unknown';
|
|
42
|
+
let copyright = `"${title}" by "${creator}"`;
|
|
43
|
+
if ((_c = style.meta.license) === null || _c === void 0 ? void 0 : _c.name) {
|
|
44
|
+
copyright += `, licensed under "${style.meta.license.name}".`;
|
|
45
|
+
}
|
|
46
|
+
copyright += ' / Remix of the original.';
|
|
47
|
+
const exif = {
|
|
48
|
+
ImageDescription: `${copyright} - Created with dicebear.com`,
|
|
49
|
+
Copyright: copyright,
|
|
50
|
+
'XMP-dc:Title': title,
|
|
51
|
+
'XMP-dc:Creator': creator,
|
|
52
|
+
};
|
|
53
|
+
if (style.meta.source) {
|
|
54
|
+
exif['XMP-dc:Source'] = style.meta.source;
|
|
55
|
+
}
|
|
56
|
+
if ((_d = style.meta.license) === null || _d === void 0 ? void 0 : _d.url) {
|
|
57
|
+
exif['XMP-cc:License'] = style.meta.license.url;
|
|
58
|
+
}
|
|
59
|
+
return exif;
|
|
60
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { schema } from '../schema.js';
|
|
2
|
+
export function defaults(schema) {
|
|
3
|
+
var _a;
|
|
4
|
+
let result = {};
|
|
5
|
+
let props = (_a = schema.properties) !== null && _a !== void 0 ? _a : {};
|
|
6
|
+
Object.keys(props).forEach((key) => {
|
|
7
|
+
let val = props[key];
|
|
8
|
+
if (typeof val === 'object' && undefined !== val.default) {
|
|
9
|
+
if (Array.isArray(val.default)) {
|
|
10
|
+
result[key] = [...val.default];
|
|
11
|
+
}
|
|
12
|
+
else if (typeof val.default === 'object') {
|
|
13
|
+
result[key] = { ...val.default };
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
result[key] = val.default;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
export function merge(style, options) {
|
|
23
|
+
let result = {
|
|
24
|
+
...{
|
|
25
|
+
seed: Math.random().toString(),
|
|
26
|
+
},
|
|
27
|
+
...defaults(schema),
|
|
28
|
+
...defaults(style.schema),
|
|
29
|
+
...options,
|
|
30
|
+
};
|
|
31
|
+
// Return a complete copy because the styles could partially customize the
|
|
32
|
+
// options and thus modify nested objects and arrays.
|
|
33
|
+
return JSON.parse(JSON.stringify(result));
|
|
34
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Prng } from '../types';
|
|
2
|
-
export declare function create(seed?: string): Prng;
|
|
1
|
+
import type { Prng } from '../types.js';
|
|
2
|
+
export declare function create(seed?: string): Prng;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const MIN = -2147483648;
|
|
2
|
+
const MAX = 2147483647;
|
|
3
|
+
function xorshift(value) {
|
|
4
|
+
value ^= value << 13;
|
|
5
|
+
value ^= value >> 17;
|
|
6
|
+
value ^= value << 5;
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
function hashSeed(seed) {
|
|
10
|
+
let hash = 0;
|
|
11
|
+
for (let i = 0; i < seed.length; i++) {
|
|
12
|
+
hash = ((hash << 5) - hash + seed.charCodeAt(i)) | 0;
|
|
13
|
+
hash = xorshift(hash);
|
|
14
|
+
}
|
|
15
|
+
return hash;
|
|
16
|
+
}
|
|
17
|
+
function randomSeed() {
|
|
18
|
+
return MIN + Math.floor((MAX - MIN) * Math.random()).toString();
|
|
19
|
+
}
|
|
20
|
+
export function create(seed) {
|
|
21
|
+
seed = seed !== null && seed !== void 0 ? seed : randomSeed();
|
|
22
|
+
let value = hashSeed(seed) || 1;
|
|
23
|
+
const next = () => (value = xorshift(value));
|
|
24
|
+
const integer = (min, max) => {
|
|
25
|
+
return Math.floor(((next() - MIN) / (MAX - MIN)) * (max + 1 - min) + min);
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
seed,
|
|
29
|
+
bool(likelihood = 50) {
|
|
30
|
+
return integer(0, 100) <= likelihood;
|
|
31
|
+
},
|
|
32
|
+
integer(min, max) {
|
|
33
|
+
return integer(min, max);
|
|
34
|
+
},
|
|
35
|
+
pick(arr) {
|
|
36
|
+
return arr.length > 0 ? arr[integer(0, arr.length - 1)] : undefined;
|
|
37
|
+
},
|
|
38
|
+
string(length, characters = 'abcdefghijklmnopqrstuvwxyz1234567890') {
|
|
39
|
+
let str = '';
|
|
40
|
+
for (let i = 0; i < length; i++) {
|
|
41
|
+
str += characters[integer(0, characters.length - 1)];
|
|
42
|
+
}
|
|
43
|
+
return str;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { StyleCreateResult } from '../types.js';
|
|
2
|
+
export declare function getViewBox(result: StyleCreateResult): {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
};
|
|
8
|
+
export declare function addBackgroundColor(result: StyleCreateResult, backgroundColor: string): string;
|
|
9
|
+
export declare function addScale(result: StyleCreateResult, scale: number): string;
|
|
10
|
+
export declare function addTranslate(result: StyleCreateResult, x?: number, y?: number): string;
|
|
11
|
+
export declare function addRotate(result: StyleCreateResult, rotate: number): string;
|
|
12
|
+
export declare function addFlip(result: StyleCreateResult): string;
|
|
13
|
+
export declare function addViewboxMask(result: StyleCreateResult, radius: number): string;
|
|
14
|
+
export declare function createAttrString(result: StyleCreateResult): string;
|
|
15
|
+
export declare function randomizeIds(result: StyleCreateResult, seed: string): string;
|
package/lib/utils/svg.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import * as escape from './escape.js';
|
|
2
|
+
import { create as createPrng } from './prng.js';
|
|
3
|
+
export function getViewBox(result) {
|
|
4
|
+
let viewBox = result.attributes['viewBox'].split(' ');
|
|
5
|
+
let x = parseInt(viewBox[0]);
|
|
6
|
+
let y = parseInt(viewBox[1]);
|
|
7
|
+
let width = parseInt(viewBox[2]);
|
|
8
|
+
let height = parseInt(viewBox[3]);
|
|
9
|
+
return {
|
|
10
|
+
x,
|
|
11
|
+
y,
|
|
12
|
+
width,
|
|
13
|
+
height,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function addBackgroundColor(result, backgroundColor) {
|
|
17
|
+
let { width, height, x, y } = getViewBox(result);
|
|
18
|
+
backgroundColor =
|
|
19
|
+
backgroundColor[0] === '#' ? backgroundColor : `#${backgroundColor}`;
|
|
20
|
+
return `<rect fill="${backgroundColor}" width="${width}" height="${height}" x="${x}" y="${y}" />${result.body}`;
|
|
21
|
+
}
|
|
22
|
+
export function addScale(result, scale) {
|
|
23
|
+
let { width, height, x, y } = getViewBox(result);
|
|
24
|
+
let percent = scale ? (scale - 100) / 100 : 0;
|
|
25
|
+
let translateX = (width / 2 + x) * percent * -1;
|
|
26
|
+
let translateY = (height / 2 + y) * percent * -1;
|
|
27
|
+
return `<g transform="translate(${translateX} ${translateY}) scale(${scale / 100})">${result.body}</g>`;
|
|
28
|
+
}
|
|
29
|
+
export function addTranslate(result, x, y) {
|
|
30
|
+
let viewBox = getViewBox(result);
|
|
31
|
+
let translateX = (viewBox.width + viewBox.x * 2) * ((x !== null && x !== void 0 ? x : 0) / 100);
|
|
32
|
+
let translateY = (viewBox.height + viewBox.y * 2) * ((y !== null && y !== void 0 ? y : 0) / 100);
|
|
33
|
+
return `<g transform="translate(${translateX} ${translateY})">${result.body}</g>`;
|
|
34
|
+
}
|
|
35
|
+
export function addRotate(result, rotate) {
|
|
36
|
+
let { width, height, x, y } = getViewBox(result);
|
|
37
|
+
return `<g transform="rotate(${rotate}, ${width / 2 + x}, ${height / 2 + y})">${result.body}</g>`;
|
|
38
|
+
}
|
|
39
|
+
export function addFlip(result) {
|
|
40
|
+
let { width, x } = getViewBox(result);
|
|
41
|
+
return `<g transform="scale(-1 1) translate(${width * -1 - x * 2} 0)">${result.body}</g>`;
|
|
42
|
+
}
|
|
43
|
+
export function addViewboxMask(result, radius) {
|
|
44
|
+
let { width, height, x, y } = getViewBox(result);
|
|
45
|
+
let rx = radius ? (width * radius) / 100 : 0;
|
|
46
|
+
let ry = radius ? (height * radius) / 100 : 0;
|
|
47
|
+
return (`<mask id="viewboxMask">` +
|
|
48
|
+
`<rect width="${width}" height="${height}" rx="${rx}" ry="${ry}" x="${x}" y="${y}" fill="#fff" />` +
|
|
49
|
+
`</mask>` +
|
|
50
|
+
`<g mask="url(#viewboxMask)">${result.body}</g>`);
|
|
51
|
+
}
|
|
52
|
+
export function createAttrString(result) {
|
|
53
|
+
const attributes = {
|
|
54
|
+
xmlns: 'http://www.w3.org/2000/svg',
|
|
55
|
+
...result.attributes,
|
|
56
|
+
};
|
|
57
|
+
return Object.keys(attributes)
|
|
58
|
+
.map((attr) => `${escape.xml(attr)}="${escape.xml(attributes[attr])}"`)
|
|
59
|
+
.join(' ');
|
|
60
|
+
}
|
|
61
|
+
export function randomizeIds(result, seed) {
|
|
62
|
+
const prng = createPrng(JSON.stringify({
|
|
63
|
+
attributes: result.attributes,
|
|
64
|
+
seed,
|
|
65
|
+
}));
|
|
66
|
+
const ids = {};
|
|
67
|
+
return result.body.replace(/(id="|url\(#)([a-z0-9-_]+)([")])/gi, (match, m1, m2, m3) => {
|
|
68
|
+
ids[m2] = ids[m2] || prng.string(8);
|
|
69
|
+
return `${m1}${ids[m2]}${m3}`;
|
|
70
|
+
});
|
|
71
|
+
}
|
package/package.json
CHANGED
|
@@ -1,52 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dicebear/core",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.32",
|
|
4
4
|
"description": "An avatar library for designers and developers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"avatar",
|
|
7
7
|
"identicon"
|
|
8
8
|
],
|
|
9
|
-
"homepage": "https://
|
|
9
|
+
"homepage": "https://dicebear.com",
|
|
10
10
|
"bugs": {
|
|
11
11
|
"url": "https://github.com/dicebear/dicebear/issues"
|
|
12
12
|
},
|
|
13
13
|
"repository": {
|
|
14
14
|
"type": "git",
|
|
15
|
-
"url": "git
|
|
16
|
-
"directory": "/packages/@dicebear/core"
|
|
15
|
+
"url": "git+https://github.com/dicebear/dicebear.git"
|
|
17
16
|
},
|
|
18
17
|
"license": "MIT",
|
|
19
18
|
"author": "Florian Körner <contact@florian-koerner.com>",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"browser": "dist/index.umd.js",
|
|
24
|
-
"types": "dist/index.d.ts",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"exports": "./lib/index.js",
|
|
21
|
+
"types": "./lib/index.d.ts",
|
|
25
22
|
"files": [
|
|
26
23
|
"LICENSE",
|
|
27
|
-
"
|
|
24
|
+
"lib",
|
|
28
25
|
"README.md"
|
|
29
26
|
],
|
|
30
27
|
"scripts": {
|
|
31
|
-
"
|
|
28
|
+
"prebuild": "del-cli lib",
|
|
29
|
+
"build": "tsc",
|
|
32
30
|
"prepublishOnly": "npm run build",
|
|
33
|
-
"
|
|
34
|
-
"build": "dicebear-project build DiceBear"
|
|
31
|
+
"test": "uvu tests"
|
|
35
32
|
},
|
|
36
33
|
"dependencies": {
|
|
34
|
+
"@dicebear/converter": "^5.0.0-alpha.32",
|
|
37
35
|
"@types/json-schema": "^7.0.7"
|
|
38
36
|
},
|
|
39
37
|
"devDependencies": {
|
|
40
|
-
"@tsconfig/recommended": "^1.0.
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"
|
|
38
|
+
"@tsconfig/recommended": "^1.0.1",
|
|
39
|
+
"del-cli": "^4.0.1",
|
|
40
|
+
"typescript": "^4.6.3",
|
|
41
|
+
"uvu": "^0.5.3"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": "^14.13.1 || >=16.0.0"
|
|
47
45
|
},
|
|
48
46
|
"publishConfig": {
|
|
49
47
|
"access": "public"
|
|
50
48
|
},
|
|
51
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "5a4a06cbb8aca42a5d19da8e704b3a7b3a45d755"
|
|
52
50
|
}
|
package/dist/core.d.ts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { Style, StyleOptions } from './types';
|
|
2
|
-
export declare function createAvatar<O extends {}>(style: Style<O>, options?: StyleOptions<O>): string;
|
|
3
|
-
export declare function createPreview<O extends {}>(style: Style<O>, options: StyleOptions<O>, property: keyof StyleOptions<O>): string | undefined;
|
package/dist/index.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* DiceBear (@dicebear/core)
|
|
3
|
-
*
|
|
4
|
-
* Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
|
|
5
|
-
* Copyright (c) 2021 Florian Körner
|
|
6
|
-
*/
|
|
7
|
-
import { JSONSchema7 } from 'json-schema';
|
|
8
|
-
export declare const schema: JSONSchema7;
|
|
9
|
-
export * from './core';
|
|
10
|
-
export * from './types';
|
|
11
|
-
export * from './options';
|
|
12
|
-
export * as utils from './utils';
|