Skip to content

Commit

Permalink
Port of 'explicitly cancel frame loop on stop' from @znah (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
sritchie authored Jan 27, 2022
1 parent 6b9a97e commit 2261607
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/core/loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ THREE.Bootstrap.registerPlugin("loop", {

install: function (three) {
this.running = false;
this.lastRequestId = null;

three.Loop = this.api(
{
start: this.start.bind(this),
stop: this.stop.bind(this),
running: false,
window: window,
},
three
);
Expand All @@ -40,16 +42,13 @@ THREE.Bootstrap.registerPlugin("loop", {
three.Loop.running = this.running = true;

var trigger = three.trigger.bind(three);
var frames = 0;
var loop = function () {
this.running && requestAnimationFrame(loop);
frames = (frames + 1) % Math.max(1, this.options.each);
if (frames == 0) {
this.events.map(trigger);
}
if (!this.running) return;
this.lastRequestId = three.Loop.window.requestAnimationFrame(loop);
this.events.map(trigger);
}.bind(this);

requestAnimationFrame(loop);
this.lastRequestId = three.Loop.window.requestAnimationFrame(loop);

three.trigger({ type: "start" });
},
Expand All @@ -58,6 +57,9 @@ THREE.Bootstrap.registerPlugin("loop", {
if (!this.running) return;
three.Loop.running = this.running = false;

three.Loop.window.cancelAnimationFrame(this.lastRequestId);
this.lastRequestId = null;

three.trigger({ type: "stop" });
},
});

0 comments on commit 2261607

Please sign in to comment.