|
|
|
@ -6,6 +6,31 @@ const optionMngr = require('./option-manager');
|
|
|
|
|
|
|
|
|
|
const opts = optionMngr.options;
|
|
|
|
|
|
|
|
|
|
function shuffleArray(array) {
|
|
|
|
|
let count = array.length,
|
|
|
|
|
randomnumber,
|
|
|
|
|
temp;
|
|
|
|
|
while( count ){
|
|
|
|
|
randomnumber = Math.random() * count-- | 0;
|
|
|
|
|
temp = array[count];
|
|
|
|
|
array[count] = array[randomnumber];
|
|
|
|
|
array[randomnumber] = temp
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mergePaths(paths) {
|
|
|
|
|
if (!paths.length) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
const out = paths[0];
|
|
|
|
|
for (let i = 1; i < paths.length; i += 1) {
|
|
|
|
|
out.commands = out.commands.concat(
|
|
|
|
|
paths[i].commands,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getTextPath = function (text, width, height, options) {
|
|
|
|
|
const len = text.length;
|
|
|
|
|
const spacing = (width - 2) / (len + 1);
|
|
|
|
@ -22,22 +47,7 @@ const getTextPath = function (text, width, height, options) {
|
|
|
|
|
return out;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function mergePaths(paths) {
|
|
|
|
|
if (!paths.length) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
const out = paths[0];
|
|
|
|
|
for (let i = 1; i < paths.length; i += 1) {
|
|
|
|
|
out.commands = out.commands.concat(
|
|
|
|
|
paths[i].commands,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const createCaptcha = function (text, options) {
|
|
|
|
|
text = text || random.captchaText();
|
|
|
|
|
options = Object.assign({}, opts, options);
|
|
|
|
|
const width = options.width;
|
|
|
|
|
const height = options.height;
|
|
|
|
|
const bg = options.background;
|
|
|
|
@ -46,9 +56,8 @@ const createCaptcha = function (text, options) {
|
|
|
|
|
`<rect width="100%" height="100%" fill="${bg}"/>` : '';
|
|
|
|
|
|
|
|
|
|
/* Create character paths and order them randomly */
|
|
|
|
|
let path =
|
|
|
|
|
[].concat(getTextPath(text, width, height, options))
|
|
|
|
|
.sort(() => Math.random() - 0.5);
|
|
|
|
|
let path = [].concat(getTextPath(text, width, height, options));
|
|
|
|
|
shuffleArray(path);
|
|
|
|
|
/* Join paths together to one */
|
|
|
|
|
path = mergePaths(path);
|
|
|
|
|
/* Randomize nodes and randomly split them */
|
|
|
|
@ -65,24 +74,27 @@ const createCaptcha = function (text, options) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const create = function (options) {
|
|
|
|
|
options = Object.assign({}, opts, options);
|
|
|
|
|
|
|
|
|
|
const text = random.captchaText(options);
|
|
|
|
|
const data = createCaptcha(text, options);
|
|
|
|
|
|
|
|
|
|
return {text, data};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const createMathExpr = function (options) {
|
|
|
|
|
options = Object.assign({}, opts, options);
|
|
|
|
|
|
|
|
|
|
const expr = random.mathExpr(options.mathMin, options.mathMax, options.mathOperator);
|
|
|
|
|
const text = expr.text;
|
|
|
|
|
const data = createCaptcha(expr.equation, options);
|
|
|
|
|
|
|
|
|
|
return {text, data};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = createCaptcha;
|
|
|
|
|
module.exports.randomText = random.captchaText;
|
|
|
|
|
module.exports.create = create;
|
|
|
|
|
|
|
|
|
|
module.exports.createMathExpr = createMathExpr;
|
|
|
|
|
module.exports.createCaptcha = function (text, options) {
|
|
|
|
|
options = Object.assign({}, opts, options);
|
|
|
|
|
createCaptcha(text, options);
|
|
|
|
|
}
|
|
|
|
|
module.exports.options = opts;
|
|
|
|
|
module.exports.loadFont = optionMngr.loadFont;
|
|
|
|
|