forked from napa3um/node-captcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptcha.js
47 lines (39 loc) · 1.33 KB
/
captcha.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
var Canvas = require('canvas');
module.exports = function(params){
if(typeof params == 'string')
params = { url: params };
params.color = params.color || 'rgb(0,100,100)';
params.background = params.background || 'rgb(255,200,150)';
return function(req, res, next){
if(req.path != params.url)
return next();
var canvas = new Canvas(250, 150);
var ctx = canvas.getContext('2d');
ctx.antialias = 'gray';
ctx.fillStyle = params.background;
ctx.fillRect(0, 0, 250, 150);
ctx.fillStyle = params.color;
ctx.lineWidth = 8;
ctx.strokeStyle = params.color;
ctx.font = '80px sans';
for (var i = 0; i < 2; i++) {
ctx.moveTo(20, Math.random() * 150);
ctx.bezierCurveTo(80, Math.random() * 150, 160, Math.random() * 150, 230, Math.random() * 150);
ctx.stroke();
}
var text = ('' + Math.random()).substr(3, 6);
for (i = 0; i < text.length; i++) {
ctx.setTransform(Math.random() * 0.5 + 1, Math.random() * 0.4, Math.random() * 0.4, Math.random() * 0.5 + 1, 30 * i + 20, 100);
ctx.fillText(text.charAt(i), 0, 0);
}
// ctx.setTransform(1, 0, 0, 1, 0, 0);
// ctx.font = '25px sans';
// ctx.fillStyle = "rgb(255,255,255)";
// ctx.fillText(text, 70, 145);
canvas.toBuffer(function(err, buf) {
if(req.session)
req.session.captcha = text;
res.end(buf);
});
};
};