Timer or play button in Bonito app

I’m using Bonito with WGLMakie to visualise 2D slices of multi-dimensional data in heatmaps. I got a simple slider working together with heatmap (the slider value slices one variable dimension) for a 3D array data:

app = App() do
    N = size(data)[end]
    slice_idx = Slider(1:N)
    slice = map(slice_idx) do idx
        view(data, :, :, idx)
    end
    fig = Figure(size=(800,800))
    ax = Axis(fig[1,1], aspect=1)
    heat = heatmap!(ax, slice,  colorrange=(0, 500))
    return Bonito.DOM.div(slice_idx, fig)
end

I would like to have the option to animate this automatically with the press of a button (ie, the value of the slice_idx changing with time), but could not find examples with a timer option. I’ve seen something similar in PlutoUI: the Clock() button, but how to do this with Bonito and WGLMakie?

I implemented something like that in NDViewer:

It also has a fallback for a pure GLMakie app:

1 Like

Thank you for the quick answer, this is exactly what I was looking for! And NDViewer looks awesome, has lots of the features I was looking into, so I will play with it for sure.

For completeness, here is your PlaySlider in my simple example from before:

App() do session
    N = size(data)[end]
    slice = Observable(view(data, :, :, 1))
    play = PlaySlider("time", 1:N)
    fig = Figure(size=(800,800))
    ax = Axis(fig[1,1], aspect=1)
    Makie.on_latest(i -> (slice[] = view(data, :, :, i)), session, play.value)
    heat = heatmap!(ax, slice,  colorrange=(0, 500))
    return Bonito.DOM.div(play, fig)
end

I might extend it to make the framerate also an observable, since this is something we often want to control.

Yeah let me know if you want to contribute to NDViewer as well, if you find any improvements :wink:
That’d be amazing, I don’t have time for it right now, but maybe next year and I’m happy to merge improvements!