A highly customizable CAPTCHA generator using canvas in TypeScript.
npm install @ddededodediamante/captcha-generatorimport Captcha from '@ddededodediamante/captcha-generator';
import fs from 'fs';
const captcha = new Captcha({
width: 250,
height: 100,
backgroundColor: '#f0f0f0',
textColor: '#333',
font: 'Arial',
fontSize: 40
});
const { text, buffer } = captcha.generate("image/png");
console.log('CAPTCHA text:', text);
fs.writeFileSync('captcha.png', buffer);Creates a CAPTCHA generator with optional settings:
| Option | Type | Default | Description |
|---|---|---|---|
width |
number |
200 |
Canvas width in pixels |
height |
number |
80 |
Canvas height in pixels |
backgroundColor |
string |
#ffffff |
Background color |
textColor |
string |
#000000 |
Color of the CAPTCHA text |
font |
string |
Sans |
Font family |
fontSize |
number |
32 |
Font size in pixels |
characters |
string |
ABCDEFGHI... |
Characters used in the CAPTCHA text |
charLength |
number |
6 |
Number of characters in the CAPTCHA |
noiseLines |
number |
4 |
Number of random noise lines |
noiseDots |
number |
100 |
Number of random noise dots |
noiseColor |
string |
#888888 |
Color for noise lines and dots |
Generates the CAPTCHA, returns { text: string; buffer: Buffer }.