In need of a good animation example
Great work on the recent releases (deck, luma, react-map-gl, react-vis) folks! Going through "Whats new" is like scanning presents under the Christmas tree looking for your favorite new feature.
My request is to include a best practice example of how to do granular control over animating marks. For example, we love our d3'esq enter, update, and exit methods where we can specify transitions with delays, duration, and easing functions. Resonance is a great react lib for dom elements but what about for a deck.gl WebGL layer? My use case has thousands of data points so dom elements are not viable and the animation effect I'm shooting for is similar to this example. Below is my initial attempt using the ScatterplotLayer but I'm missing all the before mentioned niceties I would have to code by hand.
Any guidance or direction on achieving these types of animations is appreciated. Thanks!
EDIT: I updated the code below which is better than my first attempt. This now simulates enter, update, and exit.
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import DeckGL, { ScatterplotLayer } from 'deck.gl';
import { scaleLinear } from 'd3-scale';
import { interpolate } from 'd3-interpolate';
import { timer } from 'd3-timer';
import { easeExpOut } from 'd3-ease';
class DeckGLOverlay extends Component {
constructor(props) {
super(props);
this.state = {
maxDelay: 0,
timer: null,
updated: null,
locations: [],
timeScale: scaleLinear().domain([0, props.duration]).range([0, 1]),
};
this.animate = this.animate.bind(this);
}
componentWillReceiveProps(nextProps) {
if (nextProps.duration) {
this.setState({ timeScale: this.state.timeScale.domain([0, nextProps.duration]) });
}
if (nextProps.locations && nextProps.locations.length !== this.state.locations.length) {
this.setState({ locations: nextProps.locations });
}
if (nextProps.data && nextProps.locations.length > 0) {
let maxDelay = 0;
nextProps.locations.forEach((d, i) => {
const loc = nextProps.data[nextProps.idAccessor(d)];
const toRadius = loc ? nextProps.attrAccessor(loc) : nextProps.radiusScale.min;
maxDelay += nextProps.delay(d);
d.transition = {
interpolater: interpolate(d.radius, toRadius),
delay: nextProps.delay(d, i),
};
});
this.setState({ maxDelay });
this.startAnimate();
}
}
startAnimate() {
this.stopAnimate();
this.setState({ timer: timer(this.animate) });
}
stopAnimate() {
if (this.state.timer) this.state.timer.stop();
}
animate(t) {
const { timeScale, maxDelay } = this.state;
const { locations, duration } = this.props;
if (t >= duration + maxDelay) {
this.stopAnimate();
}
locations.forEach(d => {
const time = easeExpOut(timeScale(t - d.transition.delay));
d.radius = d.transition.interpolater(time); // eslint-disable-line
});
this.setState({ updated: t });
}
render() {
const { updated } = this.state;
const { viewport, locations } = this.props;
if (!locations) {
return null;
}
const layer = new ScatterplotLayer({
id: 'scatter-plot',
data: locations,
outline: true,
strokeWidth: 1,
radiusScale: 100,
radiusMinPixels: 0.25,
radiusMaxPixels: 50,
getPosition: d => [d.longitude, d.latitude, 0],
updateTriggers: {
getRadius: updated,
},
});
const layer2 = new ScatterplotLayer({
id: 'scatter-plot2',
data: locations,
radiusScale: 100,
radiusMinPixels: 0.25,
radiusMaxPixels: 50,
getPosition: d => [d.longitude, d.latitude, 0],
getRadius: () => 2,
});
return <DeckGL {...viewport} layers={[layer, layer2]} />;
}
}
const propTypes = {
data: PropTypes.object,
locations: PropTypes.array.isRequired,
duration: PropTypes.number,
idAccessor: PropTypes.func,
attrAccessor: PropTypes.func,
delay: PropTypes.func,
radiusScale: PropTypes.shape({
min: PropTypes.number,
max: PropTypes.number,
}),
viewport: PropTypes.shape({
longitude: PropTypes.number,
latitude: PropTypes.number,
zoom: PropTypes.number,
maxZoom: PropTypes.number,
pitch: PropTypes.number,
bearing: PropTypes.number,
}),
};
const defaultProps = {
data: {},
duration: 1000,
idAccessor: d => d.id,
attrAccessor: d => d,
delay: () => 0,
radiusScale: { min: 1, max: 100 },
viewport: {
longitude: -100,
latitude: 40.7,
zoom: 4,
maxZoom: 15,
pitch: 0,
bearing: 0,
},
};
DeckGLOverlay.propTypes = propTypes;
DeckGLOverlay.defaultProps = defaultProps;
DeckGLOverlay.displayName = 'DeckGLOverlay';
export default DeckGLOverlay;
@bstaats
Improved animation support is an area we are planning to focus on for deck.gl v5. Your input is timely as there are many possible requirements on animation, and what we have in mind might not match your expectations.
As a start, we'll certainly take a look at resonance and see if it, or something like it, could work for deck.gl.
For now, maybe the easiest way for us to iterate on this would be if you could place the code above into a new example (examples/animation) and open a PR (assuming there are no license issues with the data).
We could then run it and see if we can make any improvements, and it would also help us understand what is missing compared to your reference example.
@1chandu
We have layer transition functionality in v5.1 for animations.