Comparing version 0.1.0 to 0.1.1
(function() { | ||
var EventEmitter, cancelAnimationFrame, ms, now, requestAnimationFrame, _ref; | ||
var EventEmitter, cancelAnimationFrame, ms, now, requestAnimationFrame, _ref, _ref2; | ||
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }; | ||
@@ -11,3 +11,3 @@ | ||
now = function() { | ||
now = (_ref2 = Date.now) != null ? _ref2 : function() { | ||
return new Date().getTime(); | ||
@@ -21,10 +21,10 @@ }; | ||
function Animation(opts) { | ||
var _ref2, _ref3, _ref4; | ||
var _ref3, _ref4, _ref5; | ||
if (opts == null) opts = {}; | ||
this.nextTick = __bind(this.nextTick, this); | ||
this.timoutexecutiontime = ms((_ref2 = opts.timeoutexecution) != null ? _ref2 : 20); | ||
this.executiontime = ms((_ref3 = opts.execution) != null ? _ref3 : 5); | ||
this.timoutexecutiontime = ms((_ref3 = opts.timeoutexecution) != null ? _ref3 : '32ms'); | ||
this.executiontime = ms((_ref4 = opts.execution) != null ? _ref4 : '8ms'); | ||
this.timeouttime = opts.timeout; | ||
if (this.timeouttime != null) this.timeouttime = ms(this.timeouttime); | ||
this.autotoggle = (_ref4 = opts.toggle) != null ? _ref4 : false; | ||
this.autotoggle = (_ref5 = opts.toggle) != null ? _ref5 : false; | ||
this.frametime = opts.frame; | ||
@@ -38,2 +38,6 @@ if (this.frametime != null) this.frametime = ms(this.frametime); | ||
Animation.prototype.need_next_tick = function() { | ||
return this.running && !this.paused && (this.queue.length || !this.autotoggle); | ||
}; | ||
Animation.prototype.work_queue = function(started, dt, executiontime) { | ||
@@ -44,3 +48,3 @@ var t, _base, _results; | ||
while (this.queue.length && t - started < executiontime) { | ||
if (typeof (_base = this.queue.shift()) === "function") _base(); | ||
if (typeof (_base = this.queue.shift()) === "function") _base(dt); | ||
_results.push(t = now()); | ||
@@ -57,15 +61,14 @@ } | ||
Animation.prototype.nextTick = function(callback) { | ||
var request, t, tick, timeout, _ref2; | ||
_ref2 = [null, null], timeout = _ref2[0], request = _ref2[1]; | ||
var request, t, tick, timeout, _ref3; | ||
_ref3 = [null, null], timeout = _ref3[0], request = _ref3[1]; | ||
t = now(); | ||
tick = function(success) { | ||
var dt, executiontime, started; | ||
var dt, executiontime, nextid, started; | ||
if (requestAnimationFrame.isNative) { | ||
if (this.need_next_tick()) nextid = this.nextTick(); | ||
} | ||
started = now(); | ||
dt = started - t; | ||
executiontime = success ? this.executiontime : this.timoutexecutiontime; | ||
if (success) { | ||
executiontime = this.executiontime; | ||
} else { | ||
executiontime = this.timoutexecutiontime; | ||
} | ||
if (success) { | ||
clearTimeout(timeout); | ||
@@ -78,18 +81,18 @@ } else { | ||
this.work_queue(started, dt, executiontime); | ||
if (this.running && !this.paused) { | ||
if (this.queue.length) { | ||
return this.nextTick(); | ||
} else { | ||
if (this.autotoggle) { | ||
return this.pause(); | ||
} else { | ||
return this.nextTick(); | ||
} | ||
} | ||
if (nextid == null) { | ||
if (this.need_next_tick()) nextid = this.nextTick(); | ||
return; | ||
} | ||
if (!this.need_next_tick()) { | ||
if (this.timeouttime != null) clearTimeout(nextid.timeout); | ||
cancelAnimationFrame(nextid); | ||
this.pause(); | ||
} | ||
}; | ||
request = requestAnimationFrame(tick.bind(this, true), this.frametime); | ||
if (this.timeouttime != null) { | ||
return timeout = setTimeout(tick.bind(this, false), this.timeouttime); | ||
timeout = setTimeout(tick.bind(this, false), this.timeouttime); | ||
request.timeout = timeout; | ||
} | ||
return request; | ||
}; | ||
@@ -101,12 +104,6 @@ | ||
this.emit('start'); | ||
if (!this.paused) { | ||
if (this.autotoggle) { | ||
if (this.queue.length) { | ||
return this.nextTick(); | ||
} else { | ||
return this.pause(); | ||
} | ||
} else { | ||
return this.nextTick(); | ||
} | ||
if (!this.paused && this.autotoggle && !this.queue.length) { | ||
return this.pause(); | ||
} else { | ||
return this.nextTick(); | ||
} | ||
@@ -113,0 +110,0 @@ }; |
{ "name": "animation" | ||
, "description": "animation timing & handling" | ||
, "version": "0.1.0" | ||
, "version": "0.1.1" | ||
, "homepage": "https://github.com/dodo/node-animation" | ||
@@ -5,0 +5,0 @@ , "author": "dodo (https://github.com/dodo)" |
@@ -16,3 +16,4 @@ # [animation](https://github.com/dodo/node-animation/) | ||
```javascript | ||
animation = new Animation({frame:'100ms'}); | ||
// get a tick every 100ms | ||
var animation = new Animation({frame:'100ms'}); | ||
animation.on('tick', function (dt) { … }); | ||
@@ -22,4 +23,39 @@ animation.start(); | ||
```javascript | ||
// get next tick with delta time to last tick | ||
var animation = new Animation({frame:'100ms'}); | ||
var animate = function (dt) { | ||
// do your animation stuff | ||
if (process.stdout.write(data)) { | ||
animation.nextTick(animate); | ||
} else { | ||
var t = new Date().getTime() | ||
process.stdout.once('drain', function () { | ||
var now = new Date().getTime(); | ||
animate(now - t + dt); | ||
}); | ||
} | ||
}; | ||
animation.nextTick(animate); // no start required | ||
``` | ||
```javascript | ||
// doesnt really matter when its executed, but it should happen | ||
// (use this in browser if you want to update your dom on requesAnimationFrame) | ||
var animation = new Animation(); | ||
animation.start(); | ||
animation.push(function (dt) { | ||
// happens (once) on the next few ticks, | ||
// depending on how much tasks are allready pushed | ||
}); | ||
``` | ||
[Δt](http://dodo.github.com/node-dynamictemplate/) adapters for [DOM](http://dodo.github.com/node-dt-dom/) and [jQuery](http://dodo.github.com/node-dt-dom/) depending on this module to do heavy DOM manipulation like insertion only on requesAnimationFrame. | ||
[surrender-cube](https://github.com/dodo/node-surrender-cube/blob/master/src/index.coffee) uses this module to draw a rotating wireframe cube in terminal. | ||
[ceilingled](https://github.com/c3d2/ceilingled) uses this to draw images fetched from superfeedr to draw either on SDL or on a LED wall. | ||
## Animation | ||
@@ -121,3 +157,3 @@ | ||
`dt` is the time since last animation tick. | ||
`dt` is the time since last animation tick finished. | ||
@@ -124,0 +160,0 @@ Use this to do your animation stuff. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9004
163
114