-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (89 loc) · 2.52 KB
/
index.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
require('bindpolyfill');
var Bezier = require('bezier').Bezier;
var presets = {
'ease' : [0.25,0.1,0.25,1],
'ease-in' : [0.42,0,1,1],
'ease-out' : [0,0,0.58,1],
'ease-in-out' : [0.42,0,0.58,1],
'linear' : [0,0,1,1],
'ease-in-out-back' : [0.45,-0.42, 0.595,1.34],
'ease-out-back' : [0.62,1.255, 0.665,1.095],
'ease-in-back' : [0.33,-0.305, 0.715,-0.155],
'ease-out-expo' : [0.015,0.745,0.225,0.985],
'ease-in-expo' : [0.775,0, 0.975,0.075],
'ease-in-cubic' : [0.6,0.02 ,0.95,0.295],
'ease-out-cubic' : [0.075,0.61, 0.36,0.93]
};
var preComputed = {};
var Ease = function(){
return this;
};
Ease.prototype = {
using : function( preset ){
var p;
if (preComputed[preset]){
this.curve = preComputed[preset];
return this.curve.findYAtX.bind(this.curve);
} else if (p = presets[preset]){
preComputed[preset] = new Bezier({
c1 : [0,0],
c4 : [1,1],
c2 : [p[0], p[1]],
c3 : [p[2], p[3]]
})
.buildLookup();
this.curve = preComputed[preset];
return this.curve.findYAtX.bind(this.curve);
} else {
throw new Error('No such preset!');
}
},
usingCustomCurve : function( curve ){
this.curve = new Bezier( curve );
return this.curve.yAtTime.bind(this.curve);
},
usingCSS3Curve : function( c2x, c2y, c3x, c3y){
var id = c2x + '.' + c2y + '.' + c3x + '.' + c3y;
if (!preComputed[id]){
preComputed[id] = (
new Bezier({
c1 : [0,0],
c2 : [c2x, c2y],
c3 : [c3x, c3y],
c4 : [1,1]
})
).buildLookup();
}
this.curve = preComputed[id];
return this.curve.findYAtX.bind(this.curve);
},
usingCustomEaser : function ( easer ){
this.curve = easer;
return this.curve;
}
};
module.exports.generateAllSamples = function(){
var p;
for (var i in presets){
if (presets.hasOwnProperty(i)){
p = presets[i];
preComputed[i] = new Bezier({c1 : [0,0], c4 : [1,1], c2 : [p[0], p[1]], c3 : [p[2], p[3]] }).buildLookup();
}
}
};
module.exports.Easer = Ease;
module.exports.isPreset = function( val ){
if (presets[val]){
return true;
}
return false;
};
module.exports.presets = (function(){
var result = [];
for(var i in presets){
if(presets.hasOwnProperty(i)){
result.push(i);
}
}
return result;
}());