-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate.js
179 lines (143 loc) · 3.72 KB
/
animate.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
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
/*
* AnimateJS
*/
Animate = function (cfg) {
this.duration = cfg.duration;
this.draw = cfg.draw;
this.statics = cfg.statics;
this.state = "stoped";
this.frame = 0;
// events
this.eventHandler = {
play: function(){},
stop: function(){},
pause: function(){},
end: function(){}
};
// Frames per Second (fps)
let fps = cfg.fps;
if (typeof fps == "number") {
this.requestAnimationFrame = function (callback) {
setTimeout(function(){
callback(performance.now());
}, 1000 / fps);
};
this.fps = fps;
}
else this.fps = "auto";
// timing
let timing = cfg.timing || Animate.LINEAR;
switch (cfg.erase) {
// eraseOut
case "out":
this.timing = function (n) {
return 1 - timing(1 - n);
};
break;
// eraseInOut
case "both": case "in-out":
this.timing = function (n) {
if (n <= 0.5) return timing(2 * n) / 2;
else return (2 - timing(2 * (1 - n))) / 2;
};
break;
// eraseIn
default: this.timing = timing;
}
};
// RENDER FRAME (DRAW)
Animate.prototype.renderFrame = function (time) {
let frame = (time - this.timeStart) / this.duration;
if (frame >= 1) frame = 1;
this.frame = frame;
this.draw(this.timing(frame), this);
if (frame < 1) {
if (this.state == "playing") this.requestAnimationFrame(this.renderFrame.bind(this));
}
else {
this.state = "stoped";
this.eventHandler.end(this);
}
};
// actions
// CLONE ANIMATION
Animate.prototype.clone = function () {
return new Animate({
duration: this.duration,
fps: this.fps,
statics: this.statics,
timing: this.timing,
draw: this.draw
});
}
// DESTROY ANIMATION
Animate.prototype.destroy = function () {
this
.on("stop", function(){})
.stop();
// eliminar propiedades
let props = Object.keys(this);
for (let i = 0; i < props.length; i++) delete this[props[i]];
// eliminar prototipo
Object.setPrototypeOf(this, null);
}
// PLAY ANIMATION
Animate.prototype.play = function () {
let timeStart = performance.now();
if (this.state == "paused") timeStart -= this.duration * this.frame;
this.timeStart = timeStart;
this.state = "playing";
this.eventHandler.play(this);
this.renderFrame(timeStart);
return this;
};
// STOP ANIMATION
Animate.prototype.stop = function () {
this.state = "stoped";
this.eventHandler.stop(this);
return this;
};
// PAUSE ANIMATION
Animate.prototype.pause = function () {
if (this.state == "playing") {
this.state = "paused";
this.eventHandler.pause(this);
}
return this;
}
// MOVE ANIMATION
Animate.prototype.toFrame = function (frame) {
let time = performance.now();
this.state = "paused";
this.timeStart = time - this.duration * frame;
this.frame = frame;
this.renderFrame(time);
return this;
}
// EVENTS
Animate.prototype.on = function (event, callback) {
this.eventHandler[event] = callback;
return this;
};
// TIMING
Animate.prototype.requestAnimationFrame = function(fn){window.requestAnimationFrame(fn)};
Animate.LINEAR = function (n) { return n };
Animate.REVERSE = function (n) { return 1 - n };
Animate.QUAD = function (n) { return Math.pow(n, 2) };
Animate.QUBIC = function (n) { return Math.pow(n, 3) };
Animate.CIRC = function (n) { return 1 - Math.sin(Math.acos(n)) };
Animate.ARROW = function (n) {
let x = 1.5;
return Math.pow(n, 2) * ((x + 1) * n - x)
};
Animate.BOUNCE = function (n) {
for (let a = 0, b = 1; 1; a += b, b /= 2) {
if (n >= (7 - 4 * a) / 11) {
return - Math.pow((11 - 6 * a - 11 * n) / 4, 2) + Math.pow(b, 2)
}
}
};
Animate.ELASTIC = function (n) {
let x = 1.5;
return Math.pow(2, 10 * (n - 1)) * Math.cos(20 * Math.PI * x / 3 * n)
}