-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.js
38 lines (31 loc) · 1.04 KB
/
example.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
const Captcha = require('./src/index');
const captcha = new Captcha();
const fs = require('fs');
function blobToBase64(blob) {
const reader = new FileReader();
reader.readAsDataURL(blob);
return new Promise((resolve) => {
reader.onloadend = () => {
resolve(reader.result);
};
});
}
(async () => {
await captcha.loadFonts();
for (let i = 0; i < 10; i += 1) {
const { word, image } = captcha.generate();
if (process.browser) {
document.body.appendChild(new Text(word));
if (image.constructor.name === 'OffscreenCanvas') {
const img = document.createElement('img');
img.src = `${await blobToBase64(await image.convertToBlob())}`;
document.body.appendChild(img);
} else {
document.body.appendChild(image);
}
document.body.appendChild(document.createElement('br'));
} else {
fs.writeFileSync(`${word}.png`, image.toBuffer());
}
}
})();