Jun 9, 2023
Using the Brush from Visx; creating a timeline that is automaticaly moves with a set of controls to start and stop the timeline.
Followinf in the success of the 028 lab, I'm not going to use the
@visx/brush
component to create a timeline that is automaticaly moves across
time. The idea is to have a timeline that is moving across time, and have some
controls that can be used to start and stop the timeline.
With a bit of work, I was able to get the timeline to move across time, and have a button that can be used to start and stop the timeline. I also added forward and rewind buttons and have a speed property which changes the number days it jumps each tick.
How it works
I started with the base <Timeline />
component from 028 and added some
buttons to the bottom. These I styles and could hook into the onClick()
to
start and stop the animation.
The state is used to keep track of the selected and window intervals, the count of the current interval, and the speed at which the timeline moves.
The onBrushChange()
function is used to update the selected interval when the
brush is moved. The onTick()
function is used to update the window interval
and move the brush. The onReset()
function is used to reset the timeline to
the start.
The useInterval()
hook is from
usehooks-ts is used to call
the onTick()
function every 200ms if the speed is not 0. This is used to
animate the timeline.
Ideas for improvement
- Add a slider to control the speed
- Add framer motion to smooth out the animation
- Move the animation logic into a custom hook