-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.wavify.js
executable file
·148 lines (121 loc) · 4.25 KB
/
jquery.wavify.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
140
141
142
143
144
145
146
147
148
/*
* Wavify
* Jquery Plugin to make some nice waves
* by peacepostman @ crezeo
*/
(function ( $ ) {
$.fn.wavify = function( options ) {
// Options
//
//
var settings = $.extend({
container: options.container ? options.container : 'body',
// Height of wave
height: 200,
// Amplitude of wave
amplitude: 100,
// Animation speed
speed: .15,
// Total number of articulation in wave
bones: 3,
// Color
color: 'rgba(255,255,255, 0.20)'
}, options );
var wave = this,
width = $(settings.container).width(),
height = $(settings.container).height(),
points = [],
lastUpdate,
totalTime = 0;
// Set color
//
TweenLite.set(wave, {attr:{fill: settings.color}});
function drawPoints(factor) {
var points = [];
for (var i = 0; i <= settings.bones; i++) {
var x = i/settings.bones * width;
var sinSeed = (factor + (i + i % settings.bones)) * settings.speed * 100;
var sinHeight = Math.sin(sinSeed / 100) * settings.amplitude;
var yPos = Math.sin(sinSeed / 100) * sinHeight + settings.height;
points.push({x: x, y: yPos});
}
return points;
}
function drawPath(points) {
var SVGString = 'M ' + points[0].x + ' ' + points[0].y;
var cp0 = {
x: (points[1].x - points[0].x) / 2,
y: (points[1].y - points[0].y) + points[0].y + (points[1].y - points[0].y)
};
SVGString += ' C ' + cp0.x + ' ' + cp0.y + ' ' + cp0.x + ' ' + cp0.y + ' ' + points[1].x + ' ' + points[1].y;
var prevCp = cp0;
var inverted = -1;
for (var i = 1; i < points.length-1; i++) {
var cpLength = Math.sqrt(prevCp.x * prevCp.x + prevCp.y * prevCp.y);
var cp1 = {
x: (points[i].x - prevCp.x) + points[i].x,
y: (points[i].y - prevCp.y) + points[i].y
};
SVGString += ' C ' + cp1.x + ' ' + cp1.y + ' ' + cp1.x + ' ' + cp1.y + ' ' + points[i+1].x + ' ' + points[i+1].y;
prevCp = cp1;
inverted = -inverted;
}
SVGString += ' L ' + width + ' ' + height;
SVGString += ' L 0 ' + height + ' Z';
return SVGString;
}
// Draw function
//
//
function draw() {
var now = window.Date.now();
if (lastUpdate) {
var elapsed = (now-lastUpdate) / 1000;
lastUpdate = now;
totalTime += elapsed;
var factor = totalTime*Math.PI;
TweenMax.to(wave, settings.speed, {
attr:{
d: drawPath(drawPoints(factor))
},
ease: Power1.easeInOut
});
} else {
lastUpdate = now;
}
requestAnimationFrame(draw);
}
// Pure js debounce function to optimize resize method
//
//
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function() {
timeout = null;
if (!immediate) func.apply(context, args);
}, wait);
if (immediate && !timeout) func.apply(context, args);
};
}
// Redraw for resize with debounce
//
var redraw = debounce(function() {
wave.attr('d', '');
points = [];
totalTime = 0;
width = $(settings.container).width();
height = $(settings.container).height();
lastUpdate = false;
setTimeout(function(){
draw();
}, 50);
}, 250);
$(window).on('resize', redraw);
// Execute
//
return draw();
};
}(jQuery));