Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pause animation loop when removing canvas source #5243

Merged
merged 3 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/source/canvas_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,16 @@ class CanvasSource extends ImageSource {
let loopID;

this.play = function() {
loopID = this.map.style.animationLoop.set(Infinity);
this.map._rerender();
if (loopID === undefined) {
loopID = this.map.style.animationLoop.set(Infinity);
this.map._rerender();
}
};

this.pause = function() {
this.map.style.animationLoop.cancel(loopID);
if (loopID !== undefined) {
loopID = this.map.style.animationLoop.cancel(loopID);
}
};

this._finishLoading();
Expand All @@ -87,14 +91,17 @@ class CanvasSource extends ImageSource {
}

onAdd(map: Map) {
if (this.map) return;
this.map = map;
this.load();
if (this.canvas) {
if (this.animate) this.play();
}
}

onRemove() {
this.pause();
}

/**
* Sets the canvas's coordinates and re-renders the map.
*
Expand Down
41 changes: 40 additions & 1 deletion test/unit/source/canvas_source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const test = require('mapbox-gl-js-test').test;
const CanvasSource = require('../../../src/source/canvas_source');
const Transform = require('../../../src/geo/transform');
const AnimationLoop = require('../../../src/style/animation_loop');
const Evented = require('../../../src/util/evented');
const util = require('../../../src/util/util');
const window = require('../../../src/util/window');
Expand Down Expand Up @@ -30,7 +31,7 @@ class StubMap extends Evented {
constructor() {
super();
this.transform = new Transform();
this.style = { animationLoop: { set: function() {} } };
this.style = { animationLoop: new AnimationLoop() };
}

_rerender() {
Expand Down Expand Up @@ -94,6 +95,44 @@ test('CanvasSource', (t) => {
source.onAdd(map);
});

t.test('onRemove stops animation', (t) => {
const source = createSource();
const map = new StubMap();

source.onAdd(map);

t.equal(map.style.animationLoop.stopped(), false, 'should animate initally');

source.onRemove();

t.equal(map.style.animationLoop.stopped(), true, 'should stop animating');

source.onAdd(map);

t.equal(map.style.animationLoop.stopped(), false, 'should animate when added again');

t.end();
});

t.test('play and pause animation', (t) => {
const source = createSource();
const map = new StubMap();

source.onAdd(map);

t.equal(map.style.animationLoop.stopped(), false, 'initially animating');

source.pause();

t.equal(map.style.animationLoop.stopped(), true, 'can be paused');

source.play();

t.equal(map.style.animationLoop.stopped(), false, 'can be played');

t.end();
});

t.end();
});

Expand Down