Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

fix: let the incremental animation window reach the last of the data (#3773)

Javier Andrés Garcíaf5413af4f374
diff --git a/src/components/src/common/animation-control/animation-controller.ts b/src/components/src/common/animation-control/animation-controller.ts
index 5e17d0bd4..1730f5f28 100644
--- a/src/components/src/common/animation-control/animation-controller.ts
+++ b/src/components/src/common/animation-control/animation-controller.ts
@@ -200,7 +200,7 @@ function AnimationControllerFactory(): typeof AnimationControllerType {
};
_nextFrameByDomain() {
- const {domain, value, speed = 1, baseSpeed = 600, animationWindow} = this.props;
+ const {domain, value, speed = 1, baseSpeed = 600, animationWindow, steps} = this.props;
if (!domain) {
return;
}
@@ -213,9 +213,23 @@ function AnimationControllerFactory(): typeof AnimationControllerType {
let value1: number;
const windowSize = value[1] - value[0];
if (animationWindow === ANIMATION_WINDOW.incremental) {
- const lastFrame = value[1] + delta > domain[1];
+ // grow the window to the end of the last bin, not just domain[1]: the
+ // domain ends on the last timestamp, so a window ending there would
+ // select the last scene for a single frame. steps are the bin
+ // thresholds with the last one popped, so the last bin ends one bin
+ // width past the last step.
+ let endOfSweep = domain[1];
+ if (Array.isArray(steps) && steps.length > 1) {
+ const endOfLastBin =
+ steps[steps.length - 1] + (steps[steps.length - 1] - steps[steps.length - 2]);
+ if (Number.isFinite(endOfLastBin) && endOfLastBin > endOfSweep) {
+ endOfSweep = endOfLastBin;
+ }
+ }
value0 = value[0];
- value1 = lastFrame ? value[0] + 1 : value[1] + delta;
+ // clamp the last step instead of discarding it, and loop back to the
+ // anchor only after the final frame was emitted
+ value1 = value[1] >= endOfSweep ? value[0] + 1 : Math.min(value[1] + delta, endOfSweep);
} else {
const lastFrame = value[0] + delta > domain[1];
const startValue = domain[0] - windowSize;
diff --git a/test/browser/components/common/animation-control-test.js b/test/browser/components/common/animation-control-test.js
index f44a1e5f5..93b1cf118 100644
--- a/test/browser/components/common/animation-control-test.js
+++ b/test/browser/components/common/animation-control-test.js
@@ -8,9 +8,11 @@ import moment from 'moment';
import {setLayerAnimationTimeConfig} from '@kepler.gl/actions';
import {getTimelineFromAnimationConfig} from '@kepler.gl/utils';
+import {ANIMATION_WINDOW} from '@kepler.gl/constants';
import {IntlWrapper, mountWithTheme} from 'test/helpers/component-utils';
import {
AnimationControlFactory,
+ AnimationControllerFactory,
PlaybackControlsFactory,
FloatingTimeDisplayFactory,
appInjector,
@@ -197,3 +199,53 @@ test('Components -> AnimationControl -> time display -> custom timezone and time
t.end();
});
+
+test('Components -> AnimationController -> incremental window sweeps to the end of the data', t => {
+ const AnimationController = AnimationControllerFactory();
+ // domain runs from the first timestamp to the last one; the histogram bins
+ // extend past it, so steps (bin thresholds with the last one popped) end at
+ // 900 and the last bin covers [900, 1000]
+ const domain = [0, 950];
+ const steps = [0, 100, 200, 300, 400, 500, 600, 700, 800, 900];
+ // delta = (domain[1] - domain[0]) / baseSpeed * speed = 100 per frame
+ const baseSpeed = 9.5;
+
+ const nextFrame = (value, stepsProp = steps) =>
+ new AnimationController({
+ animationWindow: ANIMATION_WINDOW.incremental,
+ domain,
+ value,
+ steps: stepsProp,
+ speed: 1,
+ baseSpeed,
+ setTimelineValue: () => {}
+ })._nextFrameByDomain();
+
+ t.deepEqual(
+ nextFrame([0, 900]),
+ [0, 1000],
+ 'should grow the window past the last timestamp to the end of the last bin'
+ );
+ t.deepEqual(
+ nextFrame([0, 920]),
+ [0, 1000],
+ 'should clamp the final step at the end of the last bin instead of discarding it'
+ );
+ t.deepEqual(
+ nextFrame([0, 1000]),
+ [0, 1],
+ 'should loop back to the anchor only after the final frame was emitted'
+ );
+ t.deepEqual(
+ nextFrame([0, 900], null),
+ [0, 950],
+ 'should clamp to the domain end when there are no bins to go by'
+ );
+ t.deepEqual(
+ nextFrame([0, 950], null),
+ [0, 1],
+ 'should loop back to the anchor at the domain end when there are no bins'
+ );
+
+ t.end();
+});