-
Notifications
You must be signed in to change notification settings - Fork 1
/
ani.js
330 lines (285 loc) · 7.71 KB
/
ani.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// under development. This code currently is not functioning.
(function(){
var defaults = {
dur:500
};
var JsAni = {
increment:30,
reversed:null,
isForward:false,
isReversed:true,
init: function(){
var o = this.options;
//console.warn("JS ANIID:", this.id, this.shyid)
this.to = {};
this.from = {};
//console.warn("CSS ANI", o.props);
for(var nm in o.props){
var u = o.props[nm].units;
u = u===undefined?"px":u;
if(nm == "opacity" || nm == "color") u = "";
o.props[nm].units = u; // fix it if undefined
this.to[nm] = o.props[nm].end + u;
this.from[nm] = o.props[nm].beg + u;
}
//b.style(this.node, this.from);
var ease = this.ease();
var rease = this.reverseEase();
var ms;
this.handle //= b.timer(this, function(){
this._currTime = new Date().getTime();
ms = this.reversed ? this._endTime - this._currTime : this._currTime - this._begTime;
var p = o.beg>o.end ? 1-(ms / this.duration) : ms / this.duration;
p = b.minMax(p, 0, 1);
this.update(this.reversed ? rease(p) : ease(p), ms);
if(this._currTime >= this._endTime) {
//log(o.props.bottom.beg, o.props.bottom.end, this)
this.stop();
b.style(this.node, this.reversed ? this.from : this.to); // ensure animated all the way\
this.onEnd();
o.onEnd && o.onEnd();
}
}, 10000, this.increment);
log("JsAni", this)
this.handle.pause();
},
update: function(p, ms){
var o = this.options, num, s, e;
obj = {};
for(var nm in o.props){
num = b.minMax(o.props[nm].beg+((o.props[nm].end-o.props[nm].beg)*p), o.props[nm].beg, o.props[nm].end);
obj[nm] = num + "" + o.props[nm].units;
//log("Ani.update:", num, "/", obj[nm]);
}
b.style(o.node, obj);
},
onEnd: function(){},
play: function(){
var handle = {
then: function(cb){
cb && cb();
}
};
b.once(this, 'onEnd', handle, 'then');
if(!this.playing){
this.isReversed = false;
this.isForward = false;
this._begTime = new Date().getTime();
this._endTime = this.duration+this._begTime;
this.handle.resume();
}
return handle;
},
stop: function(){
this.handle.pause();
this.isReversed = this.reversed;
this.isForward = !this.reversed;
log("stop: rev:", this.isReversed, "for", this.isForward, "reveresed:", this.reversed)
this.playing = false;
},
forward: function(){
log("forward:", this.isForward)
if(this.isForward) return null;
this.reversed = false;
return this.play();
},
reverse: function(){
log("reverse:", this.isReversed);
if(this.isReversed) return null;
this.reversed = true;
return this.play();
},
toggle: function(){
this.reversed = !this.reversed;
this.play();
/*
UNTESTED!
return;
if(this.reversed === null){
this.reversed = false;
if(!this.playing) this.handle.resume();
return;
}
this.reversed = !this.reversed;
var c = this._currTime;
var s = this._begTime;
var e = this._endTime;
this._begTime = c - (e-c);
this._endTime = c + (c-s);
if(!this.playing) this.play();
*/
},
easeTypes: {
linear: function(n){
return n;
},
easeIn: function(n){
return Math.pow(n, 5);
},
easeOut:function(n){
return n * (n - 5) * -1;
},
easeInOut: function(n){
n = n * 2;
if(n < 1){ return Math.pow(n, 3) / 2; };
n -= 2;
return (Math.pow(n, 3) + 2) / 2;
}
},
easeType:"easeInOut",
ease: function(t){
this.easeType = t || "easeInOut";
return this.easeTypes[this.easeType];
},
reverseEase: function(){
switch(this.easeType){
case "easeInOut": return this.easeTypes["easeInOut"];
case "easeIn": return this.easeTypes["easeOut"];
case "easeOut": return this.easeTypes["easeIn"];
}
return false;
}
};
var CssAni = {
init: function(){
var o = this.options;
//console.warn("CSS ANIID:", this.id, this.shyid)
var transName = b.supports.transition();
var properties = [];
this.to = {};
this.from = {};
for(var nm in o.props){
var u = o.props[nm].units;
u = u===undefined?"px":u;
if(nm == "opacity" || nm == "color") u = "";
properties.push(nm);
this.to[nm] = o.props[nm].end + u;
this.from[nm] = o.props[nm].beg + u;
}
b.style(this.node, transName+"Property", properties.join(","));
b.style(this.node, transName+"Duration", this.duration+"ms");
b.on(this.node, b.supports.transitionevent(), this, function(){
//console.warn("ON-ENDED!");
this.playing = false;
this.onEnd();
o.onEnd && o.onEnd();
});
log("CssAni trans:", transName, b.supports.transitionevent(), this);
},
onEnd: function(){
},
stop: function(){
// TODO!
b.style(this.node, b.supports.transition()+"Duration", "0ms");
b.style(this.node, 'left', b.style(this.node, 'left') + 'px');
},
play: function(delay, propObj){
clearTimeout(this._delayHandle);
if(delay && !this.playing){
this._delayHandle = setTimeout(b.bind(this, function(){
b.style(this.node, propObj);
}), delay);
}else{
//console.log('set prop:', propObj)
b.style(this.node, propObj);
}
this.playing = true;
log("SET ANI STYLE", propObj);
var dur = this.duration;
var handle = {
then: function(callback){
if(callback){
b.timer(callback, dur);
}
}
};
b.once(this, 'onEnd', handle, 'then');
return handle;
},
forward: function(delay){
log("forward");
return this.play(delay || this._toDelay, this.to);
},
reverse: function(delay){
log("reverse");
return this.play(delay || this._fromDelay, this.from);
}
};
var Sub = b.supports.transition() ? CssAni : JsAni;
b.Ani = b.def({
shyid:"",
handle:0,
_delayHandle:0,
_endTime:0,
_begTime:0,
_currTime:0,
_fromDelay:0,
_toDelay:0,
playing: false,
duration:500,
Ani: function(options){
var o = this.options = options;//b.mix(defaults, options, true); <- yikes! mixin messed with IE and caused new instance overwrites! why??
this.id = b.uid("ani");
var nm;
// deleting props! May mess up the non-copy!
this.node = o.node = b.byId(o.node);
if(options.duration){
this.duration = options.duration;
delete options.duration;
}
if(options.to && options.to.delay){
this._toDelay = options.to.delay;
delete options.to.delay;
}
if(options.from && options.from.delay){
this._fromDelay = options.from.delay;
delete options.from.delay;
}
// Careful!
// Webkit might not derive the "from" style itself - you may need to set it.
if(options.to){
o.props = {};
for(nm in options.to){
if(nm == "units") continue;
if(nm == "delay") continue;
o.props[nm] = {};
o.props[nm].end = options.to[nm];
if(options.from && options.from[nm] !== undefined){
o.props[nm].beg = options.from[nm];
}else{
o.props[nm].beg = b.style(o.node, nm);
}
}
}else{
for(nm in o.props){
if(o.props[nm].beg === undefined){
o.props[nm].beg = b.style(o.node, nm);
}
}
}
log("Ani.props:", o.props); // opacity units is good here
this.init();
}
}, Sub);
bv.mix({
ani: function(o){
return new b.Ani(o);
}
});
/*
USAGE
this.ani = b.ani({
node:this.node,
dur:300
});
this.ani.to({
onEnd:onEnd,
init:this.initialize,
left:x,
top: listHeight - getImageContainer().h - options.bottom,
width:this.cBox.w*p,
height:this.cBox.h*p,
marginTop:(this.cBox.h-(this.cBox.h*p))
});
*/
})();