/
clip.html
271 lines (231 loc) · 6.07 KB
/
clip.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<!doctype html>
<html lang="en">
<head>
<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">
html,body{
width: 100%;
height: 100%;
margin:0;
padding:0;
}
.view{
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.box{
position: absolute;
left:0;
top:0;
width: 100%;
height: 100%;
background:#000 url("pic1.jpg") no-repeat center center;
background-size: auto 100%;
backface-visibility: hidden;
overflow: hidden;
}
#cas{
opacity: 1;
-webkit-transition:opacity .5s;
-ms-transition:opacity .5s;
-moz-transition:opacity .5s;
}
.noOp{
opacity: 0 !important;
}
</style>
</head>
<body>
<div class="view">
<div class="box" id="bb">
<canvas id="cas" ></canvas>
</div>
</div>
<script charset="utf-8">
var canvas = document.getElementById("cas"), ctx = canvas.getContext("2d");
var x1, y1, a = 30, timeout, totimes = 100, distance = 30;
var saveDot = [];
var canvasBox = document.getElementById("bb");
canvas.width = canvasBox.clientWidth;
canvas.height = canvasBox.clientHeight;
var img = new Image();
img.src = "pic2.jpg";
img.onload = function () {
var w = canvas.height*img.width/img.height;
ctx.drawImage(img, (canvas.width-w)/2, 0, w, canvas.height);
tapClip()
};
function getClipArea(e, hastouch){
var x = hastouch ? e.targetTouches[0].pageX : e.clientX;
var y = hastouch ? e.targetTouches[0].pageY : e.clientY;
var ndom = canvas;
while(ndom.tagName!=="BODY"){
x -= ndom.offsetLeft;
y -= ndom.offsetTop;
ndom = ndom.parentNode;
}
return {
x: x,
y: y
}
}
//通过修改globalCompositeOperation来达到擦除的效果
function tapClip() {
var hastouch = "ontouchstart" in window ? true : false,
tapstart = hastouch ? "touchstart" : "mousedown",
tapmove = hastouch ? "touchmove" : "mousemove",
tapend = hastouch ? "touchend" : "mouseup";
var area;
var x2,y2;
ctx.lineCap = "round";
ctx.lineJoin = "round";
ctx.lineWidth = a * 2;
ctx.globalCompositeOperation = "destination-out";
window.addEventListener(tapstart, function (e) {
clearTimeout(timeout);
e.preventDefault();
area = getClipArea(e, hastouch);
x1 = area.x;
y1 = area.y;
drawLine(x1, y1);
this.addEventListener(tapmove, tapmoveHandler);
this.addEventListener(tapend, function () {
this.removeEventListener(tapmove, tapmoveHandler);
//检测擦除状态
timeout = setTimeout(function () {
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var dd = 0;
for (var x = 0; x < imgData.width; x += distance) {
for (var y = 0; y < imgData.height; y += distance) {
var i = (y * imgData.width + x) * 4;
if (imgData.data[i + 3] > 0) { dd++ }
}
}
if (dd / (imgData.width * imgData.height / (distance * distance)) < 0.4) {
canvas.className = "noOp";
}
}, totimes)
});
function tapmoveHandler(e) {
clearTimeout(timeout);
e.preventDefault();
area = getClipArea(e, hastouch);
x2 = area.x;
y2 = area.y;
drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
})
}
function drawLine(x1, y1, x2, y2){
// ctx.save();
// if(arguments.length==2){
// saveDot = [[x1, y1]];
// ctx.beginPath();
// ctx.arc(x1, y1, a, 0, 2 * Math.PI);
// ctx.fill();
// }else {
// saveDot.push([x2,y2]);
// if(saveDot.length >= 3){
// ctx.beginPath();
// ctx.moveTo(saveDot[0][0], saveDot[0][1]);
// ctx.quadraticCurveTo(saveDot[1][0], saveDot[1][1], saveDot[2][0], saveDot[2][1]);
// ctx.stroke();
// saveDot = [saveDot.pop()];
// }
// }
// ctx.restore();
ctx.save();
ctx.beginPath();
if(arguments.length==2){
ctx.arc(x1, y1, a, 0, 2 * Math.PI);
ctx.fill();
}else {
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
ctx.restore();
}
//使用clip来达到擦除效果
function otherClip() {
var hastouch = "ontouchstart" in window ? true : false,
tapstart = hastouch ? "touchstart" : "mousedown",
tapmove = hastouch ? "touchmove" : "mousemove",
tapend = hastouch ? "touchend" : "mouseup";
var area;
canvas.addEventListener(tapstart, function (e) {
clearTimeout(timeout);
e.preventDefault();
area = getClipArea(e, hastouch);
x1 = area.x;
y1 = area.y;
ctx.save();
ctx.beginPath();
ctx.arc(x1, y1, a, 0, 2 * Math.PI);
ctx.clip();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.restore();
canvas.addEventListener(tapmove, tapmoveHandler);
canvas.addEventListener(tapend, function () {
canvas.removeEventListener(tapmove, tapmoveHandler);
timeout = setTimeout(function () {
var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var dd = 0;
for (var x = 0; x < imgData.width; x += distance) {
for (var y = 0; y < imgData.height; y += distance) {
var i = (y * imgData.width + x) * 4;
if (imgData.data[i + 3] > 0) {
dd++
}
}
}
if (dd / (imgData.width * imgData.height / (distance * distance)) < 0.4) {
canvas.className = "noOp";
}
}, totimes)
});
function tapmoveHandler(e) {
e.preventDefault();
area = getClipArea(e, hastouch);
x2 = area.x;
y2 = area.y;
var asin = a * Math.sin(Math.atan((y2 - y1) / (x2 - x1)));
var acos = a * 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.save();
ctx.beginPath();
ctx.arc(x2, y2, a, 0, 2 * Math.PI);
ctx.clip();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.restore();
ctx.save();
ctx.beginPath();
ctx.moveTo(x3, y3);
ctx.lineTo(x5, y5);
ctx.lineTo(x6, y6);
ctx.lineTo(x4, y4);
ctx.closePath();
ctx.clip();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.restore();
x1 = x2;
y1 = y2;
}
})
}
</script>
</body>
</html>