svg-captcha = function (text, options) {
text = text || random.captchaText();
options = Object.assign({}, opts, options);
const width = options.width;
const height = options.height;
const bg = options.background;
if (bg) {
options.color = true;
}
const bgRect = bg ?
`<rect width="100%" height="100%" fill="${bg}"/>` : '';
const paths =
[].concat(getLineNoise(width, height, options))
.concat(getText(text, width, height, options))
.sort(() => Math.random() - 0.5)
.join('');
const start = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">`;
const xml = `${start}${bgRect}${paths}</svg>`;
return xml;
}n/a
create = function (options) {
const text = random.captchaText(options);
const data = createCaptcha(text, options);
return {text, data};
}...
## install
> npm install --save svg-captcha
## usage
```Javascript
var svgCaptcha = require('svg-captcha');
var captcha = svgCaptcha.create();
console.log(c);
// {data: '<svg.../svg>', text: 'abcd'}
```
with express
```Javascript
var svgCaptcha = require('svg-captcha');
...createMathExpr = function (options) {
const expr = random.mathExpr();
const text = expr.text;
const data = createCaptcha(expr.equation, options);
return {text, data};
}...
* `color`: true // characters will have distinct colors instead of grey, true if background option is set
* `background`: '#cc9966' // background color of the svg image
This function returns an object that has the following property:
* `data`: string // svg path data
* `text`: string // captcha text
#### `svgCaptcha.createMathExpr(options)`
Similar to create api, you have the same options and return value.
The difference is that data is a svg will be an math equation on screen
and text will be the result of that equation in string, otherwise the usage
is the same as above.
#### `svgCaptcha.loadFont(url)`
Load your own font and override the default font.
...filepath => {
const font = opentype.loadSync(filepath);
options.font = font;
options.ascender = font.ascender;
options.descender = font.descender;
}...
#### `svgCaptcha.createMathExpr(options)`
Similar to create api, you have the same options and return value.
The difference is that data is a svg will be an math equation on screen
and text will be the result of that equation in string, otherwise the usage
is the same as above.
#### `svgCaptcha.loadFont(url)`
Load your own font and override the default font.
* `url`: string // path to your font
This api is a wrapper around loadFont api of opentype.js.
Your may need experiment around various options to make your own font accessible.
See the following api.
#### `svgCaptcha.options`
...randomText = function (options) {
if (typeof options === 'number') {
options = {size: options};
}
options = options || {};
const size = options.size || 4;
const ignoreChars = options.ignoreChars || '';
let i = -1;
let out = '';
let chars = opts.charPreset;
if (ignoreChars) {
chars = stripCharsFromString(chars, ignoreChars);
}
const len = chars.length - 1;
while (++i < size) {
out += chars[randomInt(0, len)];
}
return out;
}...
In addition to size, noise, color, and background, you can also set the following property:
* `width`: number // width of captcha
* `height`: number // height of captcha
* `fontSize`: number // captcha text size
* `charPreset`: string // random character preset
#### `svgCaptcha.randomText([size|options])`
return a random string.
#### `svgCaptcha(text, options)`
return a svg captcha based on text provided.
In pre 1.1.0 version you have to call these two functions,
now you can call create() to save some key strokes ;).
...filepath => {
const font = opentype.loadSync(filepath);
options.font = font;
options.ascender = font.ascender;
options.descender = font.descender;
}...
#### `svgCaptcha.createMathExpr(options)`
Similar to create api, you have the same options and return value.
The difference is that data is a svg will be an math equation on screen
and text will be the result of that equation in string, otherwise the usage
is the same as above.
#### `svgCaptcha.loadFont(url)`
Load your own font and override the default font.
* `url`: string // path to your font
This api is a wrapper around loadFont api of opentype.js.
Your may need experiment around various options to make your own font accessible.
See the following api.
#### `svgCaptcha.options`
...captchaText = function (options) {
if (typeof options === 'number') {
options = {size: options};
}
options = options || {};
const size = options.size || 4;
const ignoreChars = options.ignoreChars || '';
let i = -1;
let out = '';
let chars = opts.charPreset;
if (ignoreChars) {
chars = stripCharsFromString(chars, ignoreChars);
}
const len = chars.length - 1;
while (++i < size) {
out += chars[randomInt(0, len)];
}
return out;
}n/a
color = function (bgColor) {
// Random 24 colors
// or based on step
const hue = randomInt(0, 24) / 24;
const saturation = randomInt(60, 80) / 100;
const bgLightness = bgColor ? getLightness(bgColor) : 1.0;
let minLightness;
let maxLightness;
if (bgLightness >= 0.5) {
minLightness = Math.round(bgLightness * 100) - 45;
maxLightness = Math.round(bgLightness * 100) - 25;
} else {
minLightness = Math.round(bgLightness * 100) + 25;
maxLightness = Math.round(bgLightness * 100) + 45;
}
const lightness = randomInt(minLightness, maxLightness) / 100;
const q = lightness < 0.5 ?
lightness * (lightness + saturation) :
lightness + saturation - (lightness * saturation);
const p = (2 * lightness) - q;
const r = Math.floor(hue2rgb(p, q, hue + (1 / 3)) * 255);
const g = Math.floor(hue2rgb(p, q, hue) * 255);
const b = Math.floor(hue2rgb(p, q, hue - (1 / 3)) * 255);
/* eslint-disable no-mixed-operators */
const c = ((b | g << 8 | r << 16) | 1 << 24).toString(16).slice(1);
return '#' + c;
}n/a
greyColor = function (min, max) {
min = min || 1;
max = max || 9;
const int = randomInt(min, max).toString(16);
return `#${int}${int}${int}`;
}n/a
int = function (min, max) {
return Math.round(min + (Math.random() * (max - min)));
}n/a
mathExpr = function () {
const left = randomInt(1, 9);
const right = randomInt(1, 9);
const text = (left + right).toString();
const equation = left + '+' + right;
return {text, equation};
}n/a