webqiubai-captcha
Version:
验证码小物件
51 lines (43 loc) • 2.02 kB
JavaScript
/**
* 随机生成SVG验证码
* @returns Object
*/
function create()
{
let str = '0123456789abcdeJKNBVfghiFYTUGIHKkdsfFHGHKJas'
let one = Math.floor( Math.random() * (str.length-1-0+1)+0 )
let two = Math.floor( Math.random() * (str.length-1-0+1)+0 )
let three = Math.floor( Math.random() * (str.length-1-0+1)+0 )
let four = Math.floor( Math.random() * (str.length-1-0+1)+0 )
let text = '' + str[one] + str[two] + str[three] + str[four]
function randomColor() {
var color = ["#"];
arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f"];
for (var i = 0; i < 6; i++) {
var index = Math.round(Math.random() * arr.length);
color.push(arr[index]);
}
var str = color.join("");
return str;
}
function random(max,min) {
return Math.floor(Math.random()*( max - min + 1 ) + min);
}
let j = random(10,0)
for(let i = 0;i<j;i++){
str+=`<line x1="${random(100,0)}" y1="${random(50,0)}" x2="${random(100,0)}" y2="${random(50,0)}" stroke="${randomColor()}" stroke-width="${random(2,1)}"></line>`
}
return {
text,
data: `<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100px" height="50px" style="background:${randomColor()}">
${str}
<text x="10" y="${random(35,25)}" font-size="${random(32,14)}px" fill="${randomColor()}" stroke="black" style="transform:rotate(${random(10,-10)}deg)">${str[one]}</text>
<text x="30" y="${random(35,25)}" font-size="${random(32,14)}px" fill="${randomColor()}" stroke="black" style="transform:rotate(${random(10,-10)}deg)" >${str[two]}</text>
<text x="50" y="${random(35,25)}" font-size="${random(32,14)}px" fill="${randomColor()}" stroke="black" style="transform:rotate(${random(10,-10)}deg)">${str[three]}</text>
<text x="70" y="${random(35,25)}" font-size="${random(32,14)}px" fill="${randomColor()}" stroke="black" style="transform:rotate(${random(10,-10)}deg)">${str[four]}</text>
</svg>`
}
}
module.exports = {
create,
}