-
Notifications
You must be signed in to change notification settings - Fork 1
/
刮刮卡.html
151 lines (139 loc) · 5.39 KB
/
刮刮卡.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0;">
<title>橡皮擦效果</title>
<style type="text/css">
* {
padding: 0px;
margin: 0px;
}
.ggkBox {
width: 120px;
height: 40px;
line-height: 40px;
font-size: 14px;
color: #000;
font-weight: bold;
border: 1px solid #999;
text-align: center;
position: relative;
margin: 20px auto;
}
.ggkBox canvas {
position: absolute;
left: 0px;
top: 0px;
}
</style>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
var ggkBox = document.getElementById('ggkBox');//刮刮卡外盒子
var myCanvas = document.getElementById('myCanvas');//canvas对象
var ctx = myCanvas.getContext('2d');
ctx.fillStyle = "#EEE6CD";
var radius = 15;//刮动半径
var x1 = 0;//移动初始点横坐标
var y1 = 0;//移动初始点纵坐标
var x2 = 0;//移动结束点横坐标
var y2 = 0;//移动结束点纵坐标
var ggkTimeout;//刮刮卡展示中奖信息延时
ctx.fillRect(0, 0, myCanvas.width, myCanvas.height);
var touchMethod = function () {
var isTouch = "ontouchstart" in window ? true : false;
return {
start: isTouch ? "touchstart" : "mousedown",
move: isTouch ? "touchmove" : "mousemove",
end: isTouch ? "touchend" : "mouseup"
}
}();
myCanvas.addEventListener(touchMethod.start, function (e) {
e.preventDefault();
clearTimeout(ggkTimeout);
try {
x1 = e.targetTouches[0].pageX - ggkBox.offsetLeft;
y1 = e.targetTouches[0].pageY - ggkBox.offsetTop;
} catch (ex) {
x1 = e.pageX - ggkBox.offsetLeft;
y1 = e.pageY - ggkBox.offsetTop;
}
ctx.save();
ctx.beginPath();
ctx.arc(x1, y1, radius, 0, 2 * Math.PI);
ctx.clip();
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
ctx.restore();
myCanvas.addEventListener(touchMethod.move, moveFn);
document.addEventListener(touchMethod.end, endFn);
});
//TODO 移动函数
function moveFn(e) {
e.preventDefault();
try {
x2 = e.targetTouches[0].pageX - ggkBox.offsetLeft;
y2 = e.targetTouches[0].pageY - ggkBox.offsetTop;
} catch (ex) {
x2 = e.pageX - ggkBox.offsetLeft;
y2 = e.pageY - ggkBox.offsetTop;
}
ctx.save();
ctx.beginPath();
ctx.arc(x1, y1, radius, 0, 2 * Math.PI);
ctx.clip();
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
ctx.restore();
ctx.save();
ctx.beginPath();
var asin = radius * Math.sin(Math.atan((y2 - y1) / (x2 - x1)));
var acos = radius * Math.cos(Math.atan((y2 - y1) / (x2 - x1)));
var x3 = x1 + asin;
var y3 = y1 - acos;
var x4 = x1 - asin;
var y4 = y1 + acos;
var x5 = x2 + asin;
var y5 = y2 - acos;
var x6 = x2 - asin;
var y6 = y2 + acos;
ctx.moveTo(x3, y3);
ctx.lineTo(x5, y5);
ctx.lineTo(x6, y6);
ctx.lineTo(x4, y4);
ctx.closePath();
ctx.clip();
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
ctx.restore();
x1 = x2;
y1 = y2;
}
//TODO 移动结束函数
function endFn(e) {
e.preventDefault();
myCanvas.removeEventListener(touchMethod.move, moveFn);
document.removeEventListener(touchMethod.end, endFn);
ggkTimeout = setTimeout(function () {
var imgData = ctx.getImageData(0, 0, myCanvas.width, myCanvas.height).data;
var dd = 0;
for (var i = 0, dataMax = imgData.length; i < dataMax - 4; i += 4) {
if (imgData[i] + imgData[i + 1] + imgData[i + 2] + imgData[i + 3] > 0) {
dd++;
}
}
if (dd * 4 / dataMax <= 0.7) {
myCanvas.style.display = "none";
setTimeout(function () {
alert('恭喜中奖');
}, 500);
}
}, 100);
}
});
</script>
</head>
<body>
<div class="ggkBox" id="ggkBox">恭喜中奖
<canvas width="120px" height="40px" id="myCanvas"></canvas>
</div>
</body>
</html>