Skip to content

Commit

Permalink
Simplified rendering pipeline, fixes #8, closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
darsain committed Aug 7, 2014
1 parent 6264102 commit 52c3e3e
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions src/motio.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
}
}());

// Returns time in as precise manner as possible
var getTime = (function () {
var perf = w.performance;
if (perf && perf.now) return perf.now.bind(perf);
return function () {
return +new Date();
};
}());

/**
* Motio.
*
Expand All @@ -51,7 +60,7 @@
var callbacks = {};
var animation = {};
var active = 0;
var pos, bgPos, lastPos, frameID, renderID, i, l;
var pos, bgPos, lastPos, frameID, i, l;

// Exposed properties
self.element = element;
Expand All @@ -66,10 +75,9 @@
* @return {Object} Motio instance.
*/
self.pause = function () {
// frameID can be timeout, or animationFrame ID
cAF(frameID);
clearTimeout(frameID);
frameID = 0;
animation.lastFrame = 0;
if (!self.isPaused) {
self.isPaused = true;
trigger('pause');
Expand Down Expand Up @@ -104,7 +112,7 @@
if (!frameID) {
self.isPaused = false;
trigger('play');
requestRender();
frameID = rAF(render);
}
}

Expand Down Expand Up @@ -233,8 +241,14 @@
* @return {Void}
*/
function render() {
// Reset renderID
renderID = 0;
frameID = rAF(render);
var time = getTime();

// Don't render when it's not time for next frame yet
if (o.fps < 60 && animation.lastFrame && animation.lastFrame + (1000 / o.fps) + 1 > time) return;

animation.lastFrame = time;
positionTick();

// Prepare new background position
bgPos = isPan ? Math.round(pos.x) + 'px ' + Math.round(pos.y) + 'px' : frames[active];
Expand All @@ -257,29 +271,6 @@
}
}

/**
* Render rAF wrapper.
*
* @return {Int} Animation frame index.
*/
function requestRender() {
if (!(animation.finite && animation.to === active)) {
if (animation.immediate) {
frameID = 0;
} else {
if (o.fps >= 60) {
frameID = rAF(requestRender);
} else {
frameID = setTimeout(requestRender, 1000 / o.fps);
}
}
positionTick();
if (!renderID) {
renderID = rAF(render);
}
}
}

/**
* Update one of the dynamic option properties.
*
Expand Down

0 comments on commit 52c3e3e

Please sign in to comment.