Skip to content

Commit

Permalink
perf: only fire one requestAnimationFrame event
Browse files Browse the repository at this point in the history
  • Loading branch information
aadityataparia committed May 14, 2019
1 parent 38350c1 commit c6304cb
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 32 deletions.
28 changes: 17 additions & 11 deletions dist/sifrr.animate.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/sifrr.animate.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/sifrr.animate.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions dist/sifrr.animate.module.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/sifrr.animate.module.js.map

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions src/animateone.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ const Bezier = require('./bezier');
const types = require('./types');
const wait = require('./wait');
const digitRgx = /(\d+\.?\d*)/;
const frames = new Set();

function runFrames(currentTime) {
frames.forEach(f => f(currentTime));
window.requestAnimationFrame(runFrames);
}
window.requestAnimationFrame(runFrames);

function animateOne({
target,
Expand All @@ -26,22 +33,22 @@ function animateOne({
}
}
type = typeof type === 'function' ? type : new Bezier(types[type] || type);
const rawObj = { raw };

return wait(delay).then(() => new Promise(res => {
return wait(delay).then(() => new Promise(resolve => {
let startTime = performance.now();
function frame(currentTime) {
const frame = function(currentTime) {
const percent = (currentTime - startTime) / time, bper = type(percent >= 1 ? 1 : percent);
const next = diffs.map((d, i) => {
const next = bper * d + fromNums[i];
return round ? Math.round(next) : next;
const n = bper * d + fromNums[i];
return round ? Math.round(n) : n;
});
const val = String.raw({ raw }, ...next);
const val = String.raw(rawObj, ...next);
target[prop] = Number(val) || val;
if (onUp) onUpdate(target, prop, target[prop]);
if (percent >= 1) return res();
window.requestAnimationFrame(frame);
}
window.requestAnimationFrame(frame);
if (percent >= 1) resolve(frames.delete(frame));
};
frames.add(frame);
}));
}

Expand Down

0 comments on commit c6304cb

Please sign in to comment.