Animation with slider control


using Colors

using AbstractPlotting

"""
This example is courtesy of @neuralian through the Julia Discourse.
https://discourse.julialang.org/t/makie-interaction-slider-with-animation/25179/10
"""

x₀ = -5.0
#gateState = false
x_range = 10.0
#  deflection
p₀(x) = x / 2.0
# plot
n_points = 100.0
x = (x₀ .+ collect((-n_points/2.0):(n_points/2.0)) / n_points * x_range)
scene = lines(x, p₀(x), linewidth = 4, color = :darkcyan, leg = false)
axis = scene[Axis]
axis[:names][:axisnames] = ("x", "y")

D = Node(x₀)
HC_handle = scatter!(lift(x->[x], D), [2.0], marker=:circle,
                     markersize = 1.0, color = :red)[end]

s1 = slider(LinRange(-10.0, 0.0, 101), raw = true, camera = campixel!, start = -10.0)
kx = s1[end][:value]

scatter!(scene, lift(x->[x; x], kx), lift(x-> [0.5; p₀(x)], kx),
         marker = :hexagon, color = RGBA(0.5, 0.0, 0.5, 0.5),
         markersize = 0.35, strokewidth = 0.01, strokecolor = :black)

S = Scene(resolution = (800, 600))
hbox(scene, s1; parent = S)
@async begin
    while !isopen(S) # wait for screen to be open
        sleep(0.01)
    end
    while isopen(S)
        p = p₀((10.0 + kx[]) / 10.0)
        gateState = rand(1)[] < p
        HC_handle[:color] = gateState ? :gold1 : :dodgerblue1
        D[] = -3.0 + rand() / 5.0
        sleep(0.001)
    end
end

# Do not execute beyond this point!

RecordEvents(S, "output");