-
Notifications
You must be signed in to change notification settings - Fork 0
/
24.canvas绘制验证码+随机干扰线.html
72 lines (62 loc) · 1.96 KB
/
24.canvas绘制验证码+随机干扰线.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas绘制验证码</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
canvas{
display: block;
background: #ccc;
margin: 150px auto;
position: relative;
}
</style>
</head>
<body>
<canvas id="canvas" height="50" width="200"></canvas>
<script type="text/javascript">
window.onload = function(){
var canvas = document.getElementById("canvas");
var cv = canvas.getContext('2d');
cv.fillStyle = "yellow";
cv.font= "30px arial";
var words = "QAZWSXEDCRFVTGBYHNUJMIKOLP";
for(var i = 0; i<4; i++){
//随机颜色
var r = Math.floor(Math.random()*256);//结果就是随机0-255之间的整数
var g = Math.floor(Math.random()*256);
var b = Math.floor(Math.random()*256);
cv.fillStyle = 'rgb('+r+','+g+','+b+')';//为啥加上加号?
//随机角度
var d = Math.floor(Math.random()*(5+1-(-5))+(-5));//公式是:生成从x-y之间的一个随机数的公式是:val = Math.floor(Math.random()*((y+1-x)+x))
cv.rotate(d*Math.PI/180);
//随机字母
var n = Math.floor(Math.random()*(words.length-1));
//随机x位置
var x = Math.floor(Math.random()*((i*50+20)+1-i*50)+i*50);//这个公式源公式哪里来的
//随机y位置
var y = Math.floor(Math.random()*(45+1-30)+30);//????
cv.fillText(words[n],x,y);
}
//随机干扰线
var startx = Math.floor(Math.random()*201);
var endx = Math.floor(Math.random()*201);
var starty = Math.floor(Math.random()*51);
var endy = Math.floor(Math.random()*51);
cv.beginPath();
cv.moveTo(startx,starty);
cv.lineTo(endx,endy);
//干扰线的随机颜色
var r = Math.floor(Math.random()*256);//结果就是随机0-255之间的整数
var g = Math.floor(Math.random()*256);
var b = Math.floor(Math.random()*256);
cv.fillStyle = 'rgb('+r+','+g+','+b+')';//为啥加上加号?
cv.stroke();
}
</script>
</body>
</html>