-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
106 lines (99 loc) · 3.98 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<style>
* {
padding: 0;
margin: 0;
}
.hide{
display: block;
}
canvas {
cursor: pointer;
}
</style>
</head>
<body>
<canvas width="400" height="400" id="cas"></canvas>
------
<canvas width="400" height="400" id="cas2"></canvas>
<script>
var canvas2 = document.getElementById('cas2');
var ctx2 = canvas2.getContext('2d');
var canvas = document.getElementById('cas');
var ctx = canvas.getContext('2d');
var img = new Image();
img.widht = 400;
var tf ;
img.onload = function() {
ctx.drawImage(img, 0, 0);
ctx2.drawImage(img, 0, 0);
tf = new transformCanvas(canvas,0,0,canvas.width,canvas.height)
};
img.src = './qq.png';
function transformCanvas (canvas, x, y, w, h, d) {
var D = d || 30,
width = w,
height = h,
ctx = canvas.getContext('2d'),
pielsCopy = ctx.getImageData(x,y,canvas.width,canvas.height);
this.smear = function smear(X,Y) {
var MX = X,
MY = Y,
pixels = ctx.getImageData(x,y,width,height),
data = pixels.data,
cData = pielsCopy.data;
for (var row = 0; row < height; row++) {
var i = row *width*4 + 4;
for (var col = 1; col < width; col++, i+=4) {
var dy = MY - row,
dx = MX - col,
dd = dx * dx + dy * dy,
d = Math.sqrt(dd),
pull = D/d,// D*D/dd D/d //扭曲度可以改这里
mx = col - dx * pull,
my = row - dy * pull,
moveX = parseInt(mx)*4,
moveY = parseInt(my)*width*4+4,
moveNum = moveY + moveX;
// if(true){
// debugger;
// return;
if (mx < width && my < height && mx > 0 && my > 0) {
data[i] = cData[moveNum];
data[i+1] = cData[moveNum+1];
data[i+2] = cData[moveNum+2];
data[i+3] = cData[moveNum+3];
} else {
data[i] = 255;
data[i+1] = 255;
data[i+2] = 255;
data[i+3] = 255;
}
// } else {
// data[i+num] = cData[i+num];
// data[i+num+1] = cData[i+num+1];
// data[i+num+2] = cData[i+num+2];
// data[i+num+3] = cData[i+num+3];
// }
}
}
ctx.putImageData(pixels,x,y);
}
}
canvas.onmousedown = function (e) {
tf.smear(e.pageX, e.pageY);
canvas.onmousemove = function(e) {
requestAnimationFrame(function(){
tf.smear(e.pageX, e.pageY);
});
};
canvas.onmouseup = function() {
canvas.onmousemove = null;
canvas.onmouseup = null;
};
}
</script>
</body>
</html>