-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
78 lines (66 loc) · 1.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const Canvas = require('canvas')
const Image = Canvas.Image
const strPool = 'abcdefghijklmnopqrstuvwxyz'
const defaults = {
charPool: ( strPool + strPool.toUpperCase() + '1234567890').split('')
,size: {
width: 100
,height: 32
}
,textPos: {
left: 15
,top: 26
}
,rotate: .01
,charLength: 4
,font: '26px Unifont'
,strokeStyle: '#0088cc'
,bgColor: '#eeeeee'
,confusion: true
,cFont: '30px Arial'
,cStrokeStyle: '#adc'
,cRotate: -.05
}
function getRandomText(pool, len) {
var lenp = pool.length
,i = 0
,res = ''
for(;i < len;i ++) {
res += pool[Math.floor(Math.random() * lenp)]
}
return res
}
module.exports = exports.default = function(opts = {}, callback) {
let defs = Object.assign(defaults, opts)
let canvas = new Canvas(defs.size.width, defs.size.height)
let ctx = canvas.getContext('2d')
let len = defs.charLength
let pool = defs.charPool
let ctext = getRandomText(pool, len)
let text = getRandomText(pool, len)
//begin
//bg
ctx.fillStyle = defs.bgColor
ctx.fillRect(0, 0, defs.size.width, defs.size.height)
//bg text for mangle
if(defs.confusion) {
ctx.beginPath()
ctx.font = defs.cFont
ctx.rotate(defs.cRotate)
ctx.strokeStyle = defs.cStrokeStyle
ctx.strokeText(ctext, defs.textPos.left, defs.textPos.top)
}
//text captcha
ctx.beginPath()
ctx.strokeStyle = defs.strokeStyle
ctx.font = defs.font
ctx.rotate(defs.rotate)
ctx.strokeText(text, defs.textPos.left, defs.textPos.top)
//to buffer
canvas.toBuffer(function(err, buf) {
callback(err, {
captchaStr: text
,captchaImg: buf
})
})
}