-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlake.js
139 lines (118 loc) · 4.45 KB
/
lake.js
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
(function($) {
jQuery.fn.lake = function(options) {
var settings = $.extend({
'speed': 1,
'scale': 1,
'waves': 10,
'image': true
}, options);
var waves = settings['waves'];
var speed = settings['speed']/4;
var scale = settings['scale']/2;
var ca = document.createElement('canvas');
var c = ca.getContext('2d');
var img = this.get(0);
var img_loaded = false;
img.parentNode.insertBefore(ca, img);
var w, h, dw, dh;
var offset = 0;
var frame = 0;
var max_frames = 0;
var frames = [];
img.onload = function() {
c.save();
c.canvas.width = this.width;
c.canvas.height = this.height*2;
c.drawImage(this, 0, 0);
c.scale(1, -1);
c.drawImage(this, 0, -this.height*2);
img_loaded = true;
c.restore();
w = c.canvas.width;
h = c.canvas.height;
dw = w;
dh = h/2;
var id = c.getImageData(0, h/2, w, h).data;
var end = false;
// precalc frames
// image displacement
c.save();
while (!end) {
// var odd = c.createImageData(dw, dh);
var odd = c.getImageData(0, h/2, w, h);
var od = odd.data;
// var pixel = (w*4) * 5;
var pixel = 0;
for (var y = 0; y < dh; y++) {
for (var x = 0; x < dw; x++) {
// var displacement = (scale * dd[pixel]) | 0;
var displacement = (scale * 10 * (Math.sin((dh/(y/waves)) + (-offset)))) | 0;
var j = ((displacement + y) * w + x + displacement)*4;
// horizon flickering fix
if (j < 0) {
pixel += 4;
continue;
}
// edge wrapping fix
var m = j % (w*4);
var n = scale * 10 * (y/waves);
if (m < n || m > (w*4)-n) {
var sign = y < w/2 ? 1 : -1;
od[pixel] = od[pixel + 4 * sign];
od[++pixel] = od[pixel + 4 * sign];
od[++pixel] = od[pixel + 4 * sign];
od[++pixel] = od[pixel + 4 * sign];
++pixel;
continue;
}
if (id[j+3] != 0) {
od[pixel] = id[j];
od[++pixel] = id[++j];
od[++pixel] = id[++j];
od[++pixel] = id[++j];
++pixel;
} else {
od[pixel] = od[pixel - w*4];
od[++pixel] = od[pixel - w*4];
od[++pixel] = od[pixel - w*4];
od[++pixel] = od[pixel - w*4];
++pixel;
// pixel += 4;
}
}
}
if (offset > speed * (6/speed)) {
offset = 0;
max_frames = frame - 1;
// frames.pop();
frame = 0;
end = true;
} else {
offset += speed;
frame++;
}
frames.push(odd);
}
c.restore();
if (!settings.image) {
c.height = c.height/2;
}
};
setInterval(function() {
if (img_loaded) {
if (!settings.image) {
c.putImageData(frames[frame], 0, 0);
} else {
c.putImageData(frames[frame], 0, h/2);
}
// c.putImageData(frames[frame], 0, h/2);
if (frame < max_frames) {
frame++;
} else {
frame = 0;
}
}
}, 33);
return this;
}
})(jQuery);