juliainteractivemakie.jl

Sliders and Observables in Julia GLMakie


I have troubles in understanding which syntax is currently correct to make interactive objects in Julia with GLMakie. I wrote a very easy code as an example to show what I understood and that it doesn't work:

using GLMakie, Makie
using Observables, Colors

x = Observable(4)
y = Observable(0)
raggio = Observable(3)
raggio1_piccolo = Observable(6)
raggio2_piccolo = Observable(6)
offsett = Observable(pi)

fig = Figure(size = (800, 800))
display(fig)
ax = Axis(fig[1,1], title = "cose che ruotano")

x_sl = Slider(fig[2,1], value = x[], range = 0:0.01:10)
y_sl = Slider(fig[3,1], value = y[], range = 0:0.01:10)
raggio_sl = Slider(fig[4,1], value = raggio[], range = 0:0.01:10)
offsett_sl = Slider(fig[5,1], value = offsett[], range = 0:0.01:3.14)

c1 = arc!(Point2f(0, 0), 1, 0, 2pi, color = "gray50")

function fai_cerchio!(ax, center, radius, phase, color)
    arc!(ax, center, radius, phase, 2pi + phase, color)
end

@lift begin
    arc!(ax, Point2f($x, $y), $raggio, $offsett, 2pi+ $offsett, color = "gray50")
end

the circles don't react to the sliders values.

Tried various tutorials but their syntax is old.


Solution

  • There's a few things going on here, but it's mostly that you don't have the Observable objects linked to the listeners for the sliders correctly. Here's a snippet that should do what you're looking for (with a couple minor adjustments - I've removed the offset/phase slider because it doesn't do anything if the arcs are all circles, and I've changed the window size to be more square).

    using GLMakie
    fig = Figure(resolution = (800, 800))
    ax = Axis(fig[1, 1], title = "cose che ruotano")
    
    x_sl = Slider(fig[2, 1], range = 0:0.01:10, startvalue = 4)
    y_sl = Slider(fig[3, 1], range = 0:0.01:10, startvalue = 0)
    radius_sl = Slider(fig[4, 1], range = 0:0.01:10, startvalue = 3)
    
    center = lift(x_sl.value, y_sl.value) do x, y
        Point2f(x, y)
    end
    
    radius = lift(radius_sl.value) do r
        r
    end
    
    arc!(center, radius, 0, 2π, color = "gray50")
    fig
    

    If you want to have a SliderGrid object instead, then you'd swap out the Slider definitions and lift functions with the following:

    sg = SliderGrid(
        fig[2, 1],
        (label = "x", range = 0:0.01:10, format = "{:.1f}", startvalue = 4),
        (label = "y", range = 0:0.01:10, format = "{:.1f}", startvalue = 0),
        (label = "radius", range = 0:0.01:10, format = "{:.1f}", startvalue = 3),
        width = 750
    )
    
    sliderobservables = [s.value for s in sg.sliders]
    
    center = lift(sliderobservables...) do slvalues...
        Point2f(slvalues[1], slvalues[2])
    end
    
    radius = lift(sliderobservables...) do slvalues...
        slvalues[3]
    end