1.0.2 • Published 2 years ago

mapboxgl-timeline v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Mapbox GL Timeline Slider

NPM NPM NPM

Add a timeline slider to a mapbox map.

mapbox timeline slider control

Get started

Install npm package

npm install mapboxgl-timeline

Add timeline control to map

import TimelineControl from 'mapboxgl-timeline';
import 'mapboxgl-timeline/dist/style.css';

const timeline = new TimelineControl({
  start: '2022-01-01',
  end: '2022-12-31',
  step: 1000 * 3600 * 24,
  onChange: date => {
    /*  Magic happening here 🎉 */
  },
});

map.addControl(timeline, 'top-left');

Example setup file in index.html

Options

Timeline can be configured on instantiation or using the set().

optiontypedefaultdescription
start, endDate, number, stringundefinedBounding dates. Can be any valid parseable Date.
initialDate, number, stringstartInitial date
stepnumber1Time distance between timeline stops in milliseconds
placeholderstringundefinedLabel placeholder
speednumber1Speed rate of slider play. Base running time is 1s
lazybooleanfalseCall to onChange is done once the slider is released. Use for computationaly expensive callback operations.
stackedbooleanfalseShow label and control stacked for narrower control.
autoplaybooleanfalsePlay timeline on start
autohidebooleanfalseHide timeline when values are undefined
formatfunctionundefinedLabel formatting function. Receives de current Date object and must return a string.
onStart, onEnd, onChange, onPause, onResumefunctionundefinedCallbacks for timeline events. Receive the current Date object and the index of the stop

Styles

Timeline defaults to a simple design inspired by standard mapbox-gl controls, but can be tunned by changing CSS variables or overriding styles. Check default values in /src/timeline.scss.

.mapboxgl-ctrl-timeline {
  --color-bg: #333;
  --color-text: #eee;
  --color-track: #fff2;
  --border: 1px solid #fff2;
}

Development

Create a .env file and set your Mapbox token as VITE_MAPBOX_TOKEN.

Run the development server with the command

npm run dev