mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
125 lines
4.3 KiB
JavaScript
125 lines
4.3 KiB
JavaScript
(function() {
|
|
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; };
|
|
|
|
EventEmitter = require('events').EventEmitter;
|
|
|
|
_ref = require('request-animation-frame'), requestAnimationFrame = _ref.requestAnimationFrame, cancelAnimationFrame = _ref.cancelAnimationFrame;
|
|
|
|
ms = require('ms');
|
|
|
|
now = (_ref2 = Date.now) != null ? _ref2 : function() {
|
|
return new Date().getTime();
|
|
};
|
|
|
|
this.Animation = (function() {
|
|
|
|
__extends(Animation, EventEmitter);
|
|
|
|
function Animation(opts) {
|
|
var _ref3, _ref4, _ref5;
|
|
if (opts == null) opts = {};
|
|
this.nextTick = __bind(this.nextTick, this);
|
|
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 = (_ref5 = opts.toggle) != null ? _ref5 : false;
|
|
this.frametime = opts.frame;
|
|
if (this.frametime != null) this.frametime = ms(this.frametime);
|
|
this.queue = [];
|
|
this.running = false;
|
|
this.paused = false;
|
|
Animation.__super__.constructor.apply(this, arguments);
|
|
}
|
|
|
|
Animation.prototype.need_next_tick = function() {
|
|
return this.running && !this.paused && (this.queue.length || !this.autotoggle);
|
|
};
|
|
|
|
Animation.prototype.work_queue = function(started, dt, executiontime) {
|
|
var t, _base, _results;
|
|
t = now();
|
|
_results = [];
|
|
while (this.queue.length && t - started < executiontime) {
|
|
if (typeof (_base = this.queue.shift()) === "function") _base(dt);
|
|
_results.push(t = now());
|
|
}
|
|
return _results;
|
|
};
|
|
|
|
Animation.prototype.push = function(callback) {
|
|
this.queue.push(callback);
|
|
if (this.running && this.autotoggle) return this.resume();
|
|
};
|
|
|
|
Animation.prototype.nextTick = function(callback) {
|
|
var request, t, tick, timeout, _ref3;
|
|
_ref3 = [null, null], timeout = _ref3[0], request = _ref3[1];
|
|
t = now();
|
|
tick = function(success) {
|
|
var dt, executiontime, nextid, started;
|
|
if (this.need_next_tick()) nextid = this.nextTick();
|
|
started = now();
|
|
dt = started - t;
|
|
executiontime = success ? this.executiontime : this.timoutexecutiontime;
|
|
if (success) {
|
|
clearTimeout(timeout);
|
|
} else {
|
|
cancelAnimationFrame(request);
|
|
}
|
|
this.emit('tick', dt);
|
|
if (typeof callback === "function") callback(dt);
|
|
this.work_queue(started, dt, executiontime);
|
|
if (nextid == null) 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) {
|
|
timeout = setTimeout(tick.bind(this, false), this.timeouttime);
|
|
request.timeout = timeout;
|
|
}
|
|
return request;
|
|
};
|
|
|
|
Animation.prototype.start = function() {
|
|
if (this.running) return;
|
|
this.running = true;
|
|
this.emit('start');
|
|
if (!this.paused && this.autotoggle && !this.queue.length) {
|
|
return this.pause();
|
|
} else {
|
|
return this.nextTick();
|
|
}
|
|
};
|
|
|
|
Animation.prototype.stop = function() {
|
|
if (!this.running) return;
|
|
this.running = false;
|
|
return this.emit('stop');
|
|
};
|
|
|
|
Animation.prototype.pause = function() {
|
|
if (this.paused) return;
|
|
this.paused = true;
|
|
return this.emit('pause');
|
|
};
|
|
|
|
Animation.prototype.resume = function() {
|
|
if (!this.paused) return;
|
|
this.paused = false;
|
|
this.emit('resume');
|
|
if (this.running && (!this.autotoggle || this.queue.length === 1)) {
|
|
return this.nextTick();
|
|
}
|
|
};
|
|
|
|
return Animation;
|
|
|
|
})();
|
|
|
|
}).call(this);
|