-
Notifications
You must be signed in to change notification settings - Fork 7
/
sample.js
129 lines (92 loc) · 5.37 KB
/
sample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const Avatar = require('./lib/index').default;
const {createCanvas} = require('canvas');
const fs = require('fs');
if (!fs.existsSync('./tmp')) {
fs.mkdirSync('./tmp', {recursive: true});
}
// General
const generalAvatar = Avatar.builder(Avatar.Image.margin(Avatar.Image.circleMask(Avatar.Image.identicon())), 128, 128);
generalAvatar.create('gabriel').then(buffer => fs.writeFileSync('tmp/avatar-gabriel.png', buffer));
generalAvatar.create('allaigre').then(buffer => fs.writeFileSync('tmp/avatar-allaigre.png', buffer));
// Cat example
const catAvatar = Avatar.catBuilder(256);
catAvatar.create('sample2-1').then(buffer => fs.writeFileSync('tmp/sample2-1.png', buffer));
catAvatar.create('sample2-2').then(buffer => fs.writeFileSync('tmp/sample2-2.png', buffer));
Avatar.builder(Avatar.Image.compose(Avatar.Image.fillStyle('#FAFAFA'), Avatar.Image.cat()), 256, 256).create('sample2-3').then(buffer => fs.writeFileSync('tmp/sample2-3.png', buffer));
// Github example
Avatar.githubBuilder(128).create('sample3').then(buffer => fs.writeFileSync('tmp/sample3-1.png', buffer));
Avatar.builder(Avatar.Image.compose(Avatar.Image.fillStyle('#FAFAFA'), Avatar.Image.github()), 128, 128).create('sample3').then(buffer => fs.writeFileSync('tmp/sample3-2.png', buffer));
// Square example
Avatar.squareBuilder(128).create('sample5-1').then(buffer => fs.writeFileSync('tmp/sample5-1.png', buffer));
Avatar.squareBuilder(128, 6).create('sample5-2').then(buffer => fs.writeFileSync('tmp/sample5-2.png', buffer));
// Custome image builder
Avatar.builder(Avatar.Image.compose(Avatar.Image.fillStyle('#FAFAFA'), Avatar.Image.grid({
buildImage: function (avatarBuilder, random, width, height) {
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
const nb = random.nextInt(10) + 10;
for (let i = 0; i < nb; i++) {
const red = random.nextInt(256);
const green = random.nextInt(256);
const blue = random.nextInt(256);
ctx.fillStyle = `rgb(${red},${green},${blue})`;
const x = random.nextInt(width);
const y = random.nextInt(height);
ctx.fillRect(x, y, random.nextInt(width - x), random.nextInt(height - y));
}
return Promise.resolve(canvas);
}
}, 2, 2)), 256, 256).create('sample3').then(buffer => fs.writeFileSync('tmp/sample4.png', buffer));
// Big example
const width = 1024;
const height = 512;
const size = 64;
const avatar = Avatar.builder(Avatar.Image.grid(Avatar.Image.random(
Avatar.Image.compose(Avatar.Image.fillStyle('#FAFAFA'), Avatar.Image.cat()),
Avatar.Image.compose(Avatar.Image.fillStyle('#FAFAFA'), Avatar.Image.longShadow(Avatar.Image.margin(Avatar.Image.cat()))),
Avatar.Image.compose(
Avatar.Image.fillStyle('#FFFFFF'),
Avatar.Image.margin(Avatar.Image.circleMask(Avatar.Image.compose(
Avatar.Image.randomFillStyle(),
Avatar.Image.longShadow(Avatar.Image.margin(Avatar.Image.cat()))
)
), 5)),
Avatar.Image.compose(
Avatar.Image.fillStyle('#FFFFFF'),
Avatar.Image.margin(Avatar.Image.roundedRectMask(Avatar.Image.scoreShadow(Avatar.Image.compose(
Avatar.Image.randomFillStyle(),
Avatar.Image.margin(Avatar.Image.cat())
)
)), 5)
),
Avatar.Image.female8bit(),
Avatar.Image.compose(Avatar.Image.fillStyle('#FFFFFF'), Avatar.Image.circleMask(Avatar.Image.female8bit())),
Avatar.Image.male8bit(),
Avatar.Image.compose(Avatar.Image.fillStyle('#FFFFFF'), Avatar.Image.roundedRectMask(Avatar.Image.male8bit())),
Avatar.Image.compose(Avatar.Image.fillStyle('#FFFFFF'), Avatar.Image.github()),
Avatar.Image.compose(Avatar.Image.fillStyle('#FFFFFF'), Avatar.Image.triangle()),
Avatar.Image.identicon(),
Avatar.Image.square()
), width / size, height / size), width, height, {
cache: Avatar.Cache.compose(Avatar.Cache.lru(), Avatar.Cache.folder())
});
avatar.create('sample1').then(buffer => fs.writeFileSync('tmp/sample1.png', buffer));
// Triangle
Avatar.builder(Avatar.Image.grid(Avatar.Image.triangle(), 4, 1), 512, 128).create('gabriel').then(buffer => fs.writeFileSync('tmp/triangle1.png', buffer));
// Square
Avatar.builder(Avatar.Image.grid(Avatar.Image.square(), 4, 1), 512, 128).create('gabriel').then(buffer => fs.writeFileSync('tmp/square1.png', buffer));
// Identicon
Avatar.builder(Avatar.Image.grid(Avatar.Image.identicon(), 4, 1), 512, 128).create('gabriel').then(buffer => fs.writeFileSync('tmp/identicon1.png', buffer));
// Github
Avatar.builder(Avatar.Image.grid(Avatar.Image.github(), 4, 1), 512, 128).create('gabriel').then(buffer => fs.writeFileSync('tmp/github1.png', buffer));
// Cat
Avatar.builder(Avatar.Image.grid(Avatar.Image.cat(), 4, 1), 512, 128).create('gabriel').then(buffer => fs.writeFileSync('tmp/cat1.png', buffer));
Avatar.builder(Avatar.Image.grid(
Avatar.Image.margin(Avatar.Image.roundedRectMask(Avatar.Image.compose(
Avatar.Image.randomFillStyle(),
Avatar.Image.shadow(Avatar.Image.margin(Avatar.Image.cat(), 8), {blur: 5, offsetX: 2.5, offsetY: -2.5, color: 'rgba(0,0,0,0.75)'})
), 32), 8), 4, 1), 512, 128).create('gabriel').then(buffer => fs.writeFileSync('tmp/cat2.png', buffer));
// Male 8bit
Avatar.builder(Avatar.Image.grid(Avatar.Image.male8bit(), 4, 1), 512, 128).create('gabriel').then(buffer => fs.writeFileSync('tmp/8bitmale.png', buffer));
// Female 8bit
Avatar.builder(Avatar.Image.grid(Avatar.Image.female8bit(), 4, 1), 512, 128).create('gabriel').then(buffer => fs.writeFileSync('tmp/8bitfemale.png', buffer));