Skip to content

Commit

Permalink
Merge pull request #92 from catdad/#90-throttle-frames-to-60fps
Browse files Browse the repository at this point in the history
throttle frames to 60fps
  • Loading branch information
catdad authored Dec 21, 2019
2 parents 5a516c0 + 2731224 commit 45441f3
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/confetti.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,36 @@
}

var raf = (function () {
var TIME = Math.floor(1000 / 60);
var frame, cancel;
var frames = {};
var lastFrameTime = 0;

if (typeof requestAnimationFrame === 'function' && typeof cancelAnimationFrame === 'function') {
frame = function (cb) {
return requestAnimationFrame(cb);
var id = Math.random();

frames[id] = requestAnimationFrame(function onFrame(time) {
if (lastFrameTime === time || lastFrameTime + TIME - 1 < time) {
lastFrameTime = time;
delete frames[id];

cb();
} else {
frames[id] = requestAnimationFrame(onFrame);
}
});

return id;
};
cancel = function (timer) {
return cancelAnimationFrame(timer);
cancel = function (id) {
if (frames[id]) {
cancelAnimationFrame(frames[id]);
}
};
} else {
frame = function (cb) {
return setTimeout(cb, 1000 / 60);
return setTimeout(cb, TIME);
};
cancel = function (timer) {
return clearTimeout(timer);
Expand Down

0 comments on commit 45441f3

Please sign in to comment.