-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
486 lines (379 loc) · 10.9 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* jshint node: true */
'use strict';
/**
# Bedazzle
Bedazzle is a JS animation library that allows you to do some pretty
tricky stuff without much effort. It's quite similar to
[move.js](https://github.com/visionmedia/move.js) but uses a frames rather
than a `then` function.
Additionally, bedazzle is more or less a pure CSS manipulation library
and doesn't support the additional easing functions that move does. It does
work in partnership with your CSS though and aligns animation frames with
your transition delays in the CSS (which is pretty neat).
## Example Usage
Consider the following example, which animates a series of divs. First
we have some stripped down html:
<<< examples/simple.html
Then some css which is used across most of the examples:
<<< examples/demos.css
Finally, a little bit of browserifiable css to make it all work:
<<< examples/simple.js
## Running the Examples
Running the examples is probably easiest done with
[bde](https://github.com/DamonOehlman/bde) given custom HTML is used in
the demos. Install `bde` using the following command:
```
npm install -g bde
```
You can then run bde using the following command:
```
bde examples
```
If all is well, you should be able to fire up some of the
[examples](https://github.com/DamonOehlman/bedazzle/tree/master/examples)
in your browser (although I'm still converting somee over). If you are
running bde using the default port, you should be able to access the simple
example from above at the following url:
<http://localhost:8080/simple.html>
## Reference
**/
var ratchet = require('ratchet');
var stylar = require('stylar');
var _ = require('underscore');
var extend = _.extend;
// define the property map
var transformProps = [
'x',
'y',
'z',
'rotate',
'rx',
'ry',
'rz',
'scale'
];
var percentageProps = [
'opacity'
];
var standardProps = [
'height',
'width'
];
var reStripValue = /^\-?\d+/;
/**
### bedazzle(elements, opts?)
Create a new `Bedazzler` instance which is used to orchestrate the
animation of the supplied `elements` (or those elements matched by the
selector referred to by elements if it is a string).
**/
function Bedazzler(elements, opts) {
if (! (this instanceof Bedazzler)) {
return new Bedazzler(elements, opts);
}
// if we have been given a string, then find the elements
if (typeof elements == 'string' || (elements instanceof String)) {
elements = ((opts || {}).scope || document).querySelectorAll(elements);
}
this.elements = [].slice.call(elements);
this.rtid = 0;
this.state = {};
this.done = [];
this.queued = [];
// set some configurable options
this._opts = {
frameDelay: 1000 / 60,
immediate: false
};
}
module.exports = Bedazzler;
var p = Bedazzler.prototype;
/**
### Bedazzler#frame(action?)
Define a new frame in the animation loop. If an action function is
supplied, then it will be called (with `this` bound to the Bedazzler) once
the frame becomes active.
**/
p.frame = function(action) {
var ii;
var bedazzler = this;
var props = {
elements: [],
manualHelpers: [],
callbacks: []
};
// currentTransform = stylar(this.elements[0]).get('transform', true);
props.transform = new ratchet.Transform(); // ratchet(currentTransform);
// if we have current properties, then clone the values
if (this.props) {
// copy each of the elements
for (ii = this.elements.length; ii--; ) {
props.elements[ii] = _.clone(this.props.elements[ii]) || {};
}
}
// otherwise create the new properties
else {
for (ii = this.elements.length; ii--; ) {
props.elements[ii] = {};
}
}
// queue the action
this.queued.push(function() {
bedazzler.props = props;
// if we have the action, then run it
if (typeof action == 'function') {
action.call(bedazzler, bedazzler.elements);
}
// return the props after having being tweaked by this frame
return props;
});
// queue it up
if (this._opts.immediate) {
this._applyChanges();
}
else {
clearTimeout(this.rtid);
this.rtid = setTimeout(function() {
bedazzler._applyChanges();
}, this._opts.frameDelay || 0);
}
return this;
};
/**
### Bedazzler#end(callback)
Once the current frame has completed, trigger the supplied function.
**/
p.end = function(callback) {
this.props.callbacks.push(callback);
return this;
};
/**
### Bedazzler#loop()
When all of the defined frames have completed, restart from the beginning.
An example of using loop can be found in the 'loopy' example, shown below:
<<< examples/loopy.js
**/
p.loop = function() {
var lastFrame = this.queued[this.queued.length - 1];
// if we have a last frame mark it as looping
if (lastFrame) {
lastFrame.loop = true;
}
return this;
};
/**
### Bedazzler#manual(helper)
TBC
**/
p.manual = function(helper) {
this.props.manualHelpers.push(helper);
return this;
};
/**
### Bedazzler#opts(opts)
TBC
**/
p.opts = function(opts) {
extend(this._opts, opts);
return this;
};
/**
### Bedazzler#set(name, value)
TBC
**/
p.set = function(name, value) {
var props = this.props;
if (arguments.length === 1) {
this.update(name, true);
}
else if (_.include(transformProps, name)) {
this[name].call(this, value, true);
}
else if (name == 'transform') {
props.transform = ratchet(value);
}
else {
// iterate through the elements and set the values for the named property
for (var ii = this.elements.length; ii--; ) {
props.elements[ii][name] = value;
}
}
return this;
};
/**
### Bedazzler#update(props, absolute?)
TBC
**/
p.update = function(props, absolute) {
// update the state of each of the properties
if (props) {
for (var key in props) {
if (this[key]) {
this[key](parseInt(props[key], 10) || props[key], absolute);
}
}
}
return this;
};
// create the prototype methods for each of the identified
// transform functions
transformProps.forEach(function(key) {
var targetKeys = [key];
var targetSection = 'translate';
var reRotate = /^r(\w)?(?:otate)?$/;
var reScale = /^scale(X|Y|Z)?$/;
if (reRotate.test(key)) {
targetKeys = [RegExp.$1 || 'z'];
targetSection = 'rotate';
}
if (reScale.test(key)) {
if (RegExp.$1) {
targetKeys = RegExp.$1.toLowerCase();
}
else {
targetKeys = ['x', 'y'];
}
targetSection = 'scale';
}
p[key] = function(value, absolute) {
var xyz = this.props.transform[targetSection], currentVal;
targetKeys.forEach(function(targetKey) {
currentVal = xyz[targetKey].value || 0;
// if we are applying an absolute value, then
if (absolute) {
xyz[targetKey].value = value - currentVal;
}
else {
xyz[targetKey].value = currentVal + value;
}
});
return this;
};
});
// implement the non-transform percentage (opacity, etc) properties
_.each(percentageProps, function(key) {
p[key] = function(value) {
for (var ii = this.elements.length; ii--; ) {
var targetProp = this.props.elements[ii];
// get the current value, if not defined default to 1 / 100%
var currentVal = targetProp[key] || stylar(this.elements[ii], key) || 1;
// multiply the current value by the new value
targetProp[key] = currentVal * value;
}
return this;
};
});
// implement prototype method for the standard properties
_.each(standardProps, function(key) {
p[key] = function(value) {
for (var ii = this.elements.length; ii--; ) {
var targetProp = this.props.elements[ii];
var currentVal = targetProp[key] ||
stylar(this.elements[ii], key) ||
'0px';
var actualValue = parseFloat(currentVal) || 0;
var units = value.toString().replace(reStripValue, '') ||
currentVal.toString().replace(reStripValue, '') ||
'px';
targetProp[key] = (actualValue + parseFloat(value)) + units;
}
return this;
};
});
/**
## Internal Methods
**/
/**
### _applyChanges()
**/
p._applyChanges = function() {
var ii;
var element;
var props;
var frame = this.queued.shift();
var bedazzler = this;
var transitionDuration;
var transitioners = [];
var timeout = 0;
var currentTransform;
var newTransform;
// debugger;
// if we don't have properties to apply, return
if (typeof frame != 'function') {
return;
}
// goto the next frame
props = frame();
// if we have manual helpers, then run then now
props.manualHelpers.forEach(function(helper) {
helper.call(bedazzler, bedazzler.elements);
});
// iterate through the elements and update
for (ii = this.elements.length; ii--; ) {
// get a stylar reference for the target element
element = stylar(this.elements[ii]);
// determine whether the current element has a transition on it
transitionDuration = parseFloat(element.get('transition-duration'));
// determine whether we have a transition on the element or not
if (transitionDuration) {
timeout = Math.max(timeout, transitionDuration * 1000);
transitioners.push(this.elements[ii]);
}
// if we have a general transform apply that
if (props.transform) {
// read the transform
currentTransform = element.get('transform', true);
newTransform = currentTransform ?
ratchet(currentTransform).add(props.transform) :
props.transform;
// update the transform taking into account the current transform
element.set('transform', newTransform.toString({ all: true }));
}
// iterate through the values and apply then to the element
element.set(props.elements[ii]);
}
// complete the frame
this.rtid = 0;
this.done.push(frame);
// if the frame is looping, then copy done to queued
if (frame.loop) {
this.queued = this.done.splice(0);
console.log(transitioners, timeout);
}
// next frame
this._next(transitioners, timeout, props.callbacks || []);
};
/**
### _next(transitioners, timeout, callbacks)
**/
p._next = function(transitioners, timeout, callbacks) {
var transitionsRemaining = transitioners.length;
var bedazzler = this;
var listener;
function runNext() {
if (listener) {
listener.stop();
}
// trigger the callbacks
_.each(callbacks, function(callback) {
if (typeof callback == 'function') {
callback.call(bedazzler, bedazzler.elements);
}
});
// trigger the next update cycle
bedazzler._applyChanges();
}
/*
// if we have a transition, then on transition end, apply the changes
if (transitionsRemaining > 0 && typeof aftershock == 'function') {
listener = aftershock(transitioners, { timeout: timeout + 20 }, function() {
runNext();
});
}
else */
if (transitionsRemaining > 0) {
setTimeout(runNext, (timeout || 0) + 20);
}
else if (this.queued.length > 0) {
runNext();
}
};