-
Notifications
You must be signed in to change notification settings - Fork 0
/
2015-08-11.html
179 lines (169 loc) · 5.5 KB
/
2015-08-11.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>2015/08/11</title>
<meta name="viewport" content="width=device-width">
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<style type="text/css">
html, body {
margin: 0;
padding: 0;
height: 100%;
}
canvas{
-webkit-user-select:none;
-moz-user-select:none;
}
input{
width:400px;
font-size:18px;
}
.bg {
width: 100%;
height: 50%;
background: tomato;
}
.result {
color: tomato;
margin-top: 10%;
font-size: 20px;
text-align: center;
}
canvas {
-webkit-transition: opacity 1s;
transition: opacity 1s;
z-index: 100;
}
h1 {
position: relative;
font-size: 25px;
color: white;
text-align: center;
margin: 0 auto;
}
.hide {
display: none;
}
.top {
top: 20%;
}
.middle {
top: 30%;
}
.bottom {
top: 40%;
}
.scratch {
position: absolute;
font-size: 25px;
top: 70%;
color: tomato;
text-align: center;
width: 100%;
height: 50px;
}
</style>
</head>
<body>
<div class="bg" id="bg">
<h1 class="top hide">It's lucky to meet you.</h1>
<h1 class="middle hide">Jessica</h1>
<h1 class="bottom hide">Let's explore the world together.</h1>
</div>
<div id="result" class="result"></div>
<div class="scratch">scratch</div>
<script>
function createScratchCard(imgId,condition,callback,isOnce){
var img=document.getElementById(imgId);
document.addEventListener('DOMContentLoaded', function() {
Array.prototype.forEach.call(document.getElementsByClassName('hide'), function(e){
e.style.display = 'block';
});
generate();
})
function generate(){
var cvs=document.createElement('canvas');
cvs.style.position='absolute';
cvs.style.left=img.offsetLeft+'px';
cvs.style.top=img.offsetTop+'px';
cvs.width=img.getClientRects()[0].width;
cvs.height=img.getClientRects()[0].height;
img.parentNode.insertBefore(cvs,img);
var context=cvs.getContext('2d');
context.fillStyle='#bbb';
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.globalCompositeOperation = 'destination-out';
context.strokeStyle="eee";
context.lineJoin = "round";
context.lineWidth = 35;
var offsetParent=cvs,offsetLeft=0,offsetTop=0;
while(offsetParent){
offsetLeft+=offsetParent.offsetLeft;
offsetTop+=offsetParent.offsetTop;
offsetParent=offsetParent.offsetParent;
}
var pathPoints=[];
var x,y;
var start='mousedown',move='mousemove',end='mouseup';
if(document.createTouch){
start="touchstart";
move="touchmove";
end="touchend";
}
cvs.addEventListener(start,onTouchStart);
function onTouchStart(e){
e.preventDefault();
if(e.changedTouches){
e=e.changedTouches[e.changedTouches.length-1];
}
console.log(e.pageX,offsetLeft);
x=e.pageX - offsetLeft;
y=e.pageY - offsetTop;
context.beginPath();
context.arc(x, y, 35/2, 0, Math.PI*2, true);
context.closePath();
context.fill();
document.addEventListener(end,onTouchEnd);
cvs.addEventListener(move,onTouch)
}
function onTouch(e){
if(e.changedTouches){
e=e.changedTouches[e.changedTouches.length-1];
}
context.beginPath();
context.moveTo(x, y);
context.lineTo(e.pageX - offsetLeft, e.pageY- offsetTop);
x=e.pageX - offsetLeft;y=e.pageY - offsetTop;
context.closePath();
context.stroke();
var n=(Math.random()*10000000)|0;
context.canvas.style.color='#'+ n.toString(16);//fix android 4.2 bug force repaint
}
function onTouchEnd(){
cvs.removeEventListener(move,onTouch);
pathPoints=[];
check();
}
function check(){
data=context.getImageData(0,0,cvs.width,cvs.height).data;
var length=data.length,k=0;
for(var i=0;i<length-3;i+=4){
if(data[i]==0&&data[i+1]==0&&data[i+2]==0&&data[i+3]==0){
k++;
}
}
var f=k*100/(cvs.width*cvs.height);
callback(f);
if(f>(condition||90)){
cvs.style.opacity = 0;
}
}
}
}
createScratchCard('bg',50,function (f){
document.getElementById('result').innerHTML='刮开面积:'+f.toFixed(2)+'%';
});
</script>
</body>
</html>