forked from lisnote/image-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (88 loc) · 2.86 KB
/
index.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
const sharp = require("sharp");
const { generate } = require("generate-passphrase");
const sharpDecorator = (width, height, textOptions) => {
return sharp(bufferColorful(width, height, textOptions));
};
const bufferColorful = (width, height, textOptions = {}) => {
const {
fontSize = 128,
wrapBreak = 6,
passPhraseLength = 3,
text = [],
xStart = 32,
yStart = 128
} = textOptions;
let lastPadding = yStart;
const passPhrase =
text === null || text.length === 0
? generate({
length: passPhraseLength,
separator: "-",
uppercase: true,
numbers: false
}).split("-")
: text;
let svg = `<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">
<style>
.text { font: bold ${fontSize}px sans-serif; }
</style>
<radialGradient id="radialGradient1" cx="${randomNumber(
width
)}" cy="${randomNumber(height)}" r="2000" gradientUnits="userSpaceOnUse">
<stop offset="0.1" style="stop-color:${randomHSL(0, 100, 30, 360, 100, 85)}" />
<stop offset="0.8" style="stop-color:${randomHSL(0, 100, 30, 360, 100, 85)};" />
</radialGradient>
<radialGradient id="radialGradient2" cx="${randomNumber(
width
)}" cy="${randomNumber(height)}" r="2000" gradientUnits="userSpaceOnUse">
<stop offset="0.1" style="stop-color:${randomHSL(0, 100, 30, 360, 100, 85)}" />
<stop offset="0.8" style="stop-color:${randomHSL(
0,
100,
30,
360,
100,
85
)};stop-opacity:0" />
</radialGradient>
<rect width="100%" height="100%" fill="url(#radialGradient1)" />
<rect width="100%" height="100%" fill="url(#radialGradient2)" />
${passPhrase.map((phrase, index) => {
let svgText = `<text x="${xStart}" y="${lastPadding}" class="text" fill="white">${phrase}</text>`;
if (phrase.length > wrapBreak) {
svgText = `<text x="${xStart}" y="${lastPadding}" class="text" fill="white">${phrase.substring(
0,
wrapBreak
)}</text>
<text x="${xStart}" y="${lastPadding +
fontSize * 0.85}" class="text" fill="white">${phrase.substring(
wrapBreak,
phrase.length
)}</text>`;
lastPadding += fontSize * 0.85;
}
lastPadding += fontSize;
return svgText;
})}
</svg>`;
return Buffer.from(svg);
};
const randomHSL = (minH, minS, minL, maxH, maxS, maxL) => {
minH = minH || 0;
maxH = maxH || 360;
minS = minS || 0;
maxS = maxS || 100;
minL = minL || 0;
maxL = maxL || 100;
let HSL, H, S, L;
H = Math.random() * (maxH - minH) + minH;
S = Math.random() * (maxS - minS) + minS;
L = Math.random() * (maxL - minL) + minL;
HSL = "hsl(" + H + "," + S + "%," + L + "%)";
return HSL;
};
const randomNumber = (num = 1) => {
return Math.random() * num;
};
module.exports = sharpDecorator;