|
|
|
@ -1,23 +1,25 @@
|
|
|
|
|
'use strict';
|
|
|
|
|
const chToPath = require('./ch-to-path');
|
|
|
|
|
const random = require('./random');
|
|
|
|
|
const optionMngr = require('./option-manager');
|
|
|
|
|
|
|
|
|
|
const opts = optionMngr.options;
|
|
|
|
|
|
|
|
|
|
const getLineNoise = function (width, height, options) {
|
|
|
|
|
const hasColor = options.color;
|
|
|
|
|
const noiseString = [];
|
|
|
|
|
const noiseLines = [];
|
|
|
|
|
let i = -1;
|
|
|
|
|
|
|
|
|
|
while (++i < options.noise) {
|
|
|
|
|
const start = `${random.int(5, 25)} ${random.int(10, height - 10)}`;
|
|
|
|
|
const end = `${random.int(width - 25, width - 5)} ${random.int(10, height - 10)}`;
|
|
|
|
|
const mid1 = `${random.int((width / 2) - 25, (width / 2) + 25)} ${random.int(10, height - 10)}`;
|
|
|
|
|
const mid2 = `${random.int((width / 2) - 25, (width / 2) + 25)} ${random.int(10, height - 10)}`;
|
|
|
|
|
const start = `${random.int(1, 21)} ${random.int(1, height - 1)}`;
|
|
|
|
|
const end = `${random.int(width - 21, width - 1)} ${random.int(1, height - 1)}`;
|
|
|
|
|
const mid1 = `${random.int((width / 2) - 21, (width / 2) + 21)} ${random.int(1, height - 1)}`;
|
|
|
|
|
const mid2 = `${random.int((width / 2) - 21, (width / 2) + 21)} ${random.int(1, height - 1)}`;
|
|
|
|
|
const color = hasColor ? random.color() : random.greyColor();
|
|
|
|
|
noiseString.push(`<path d="M${start} C${mid1},${mid2},${end}"
|
|
|
|
|
stroke="${color}" fill="none"/>`);
|
|
|
|
|
noiseLines.push(`<path d="M${start} C${mid1},${mid2},${end}" stroke="${color}" fill="none"/>`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return noiseString.join('');
|
|
|
|
|
return noiseLines;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getText = function (text, width, height, options) {
|
|
|
|
@ -29,30 +31,19 @@ const getText = function (text, width, height, options) {
|
|
|
|
|
while (++i < len) {
|
|
|
|
|
const x = spacing * (i + 1);
|
|
|
|
|
const y = height / 2;
|
|
|
|
|
const fontSize = height;
|
|
|
|
|
const charPath = chToPath(
|
|
|
|
|
text[i],
|
|
|
|
|
Object.assign({x, y, fontSize}, options)
|
|
|
|
|
);
|
|
|
|
|
const charPath = chToPath(text[i], Object.assign({x, y}, options));
|
|
|
|
|
|
|
|
|
|
const color = options.color ?
|
|
|
|
|
random.color(options.background) : random.greyColor(0, 4);
|
|
|
|
|
out.push(`<path fill="${color}" d="${charPath}"/>`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return out.join('');
|
|
|
|
|
return out;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const defaultOption = {
|
|
|
|
|
width: 150,
|
|
|
|
|
height: 50,
|
|
|
|
|
noise: 1,
|
|
|
|
|
color: false,
|
|
|
|
|
background: ''
|
|
|
|
|
};
|
|
|
|
|
const createCaptcha = function (text, options) {
|
|
|
|
|
text = text || random.captchaText();
|
|
|
|
|
options = Object.assign({}, defaultOption, options);
|
|
|
|
|
options = Object.assign({}, opts, options);
|
|
|
|
|
const width = options.width;
|
|
|
|
|
const height = options.height;
|
|
|
|
|
const bg = options.background;
|
|
|
|
@ -62,14 +53,15 @@ const createCaptcha = function (text, options) {
|
|
|
|
|
|
|
|
|
|
const bgRect = bg ?
|
|
|
|
|
`<rect width="100%" height="100%" fill="${bg}"/>` : '';
|
|
|
|
|
const lineNoise = getLineNoise(width, height, options);
|
|
|
|
|
const textPath = getText(text, width, height, options);
|
|
|
|
|
const xml = `<svg xmlns="http://www.w3.org/2000/svg"
|
|
|
|
|
width="${width}" height="${height}">
|
|
|
|
|
${bgRect} ${textPath} ${lineNoise}
|
|
|
|
|
</svg>`;
|
|
|
|
|
|
|
|
|
|
return xml.replace(/[\t]/g, '').replace(/\n(\W)/g, '$1');
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const create = function (options) {
|
|
|
|
@ -91,3 +83,5 @@ module.exports = createCaptcha;
|
|
|
|
|
module.exports.randomText = random.captchaText;
|
|
|
|
|
module.exports.create = create;
|
|
|
|
|
module.exports.createMathExpr = createMathExpr;
|
|
|
|
|
module.exports.options = opts;
|
|
|
|
|
module.exports.loadFont = optionMngr.loadFont;
|
|
|
|
|