Makie: vlines! and mouse events

Hi,
another Julia moment which lasts too long.
Got stuck when trying to change the manual example to a vertical crosshair:

using GLMakie
GLMakie.activate!()

fig, ax, p = scatter(rand(Point2f0, 100))
pos = Node([Point2f0(0)]) # observable
scatter!(ax, pos, marker = "+")
#vlines!.(ax, pos.val[1][2], color = :red)  # <<<<<<<< how ?
display(fig)

# on move, higher priority that interactions (runs first)
on(events(ax.scene).mouseposition, priority = 2) do _
    if ispressed(ax.scene, Mouse.left)
        pos[] = [mouseposition(ax.scene)]
        return Consume(true) # this would block rectangle zoom
    end
    return Consume(false)
end

# on left click/release
on(events(ax.scene).mousebutton, priority = 2) do mb
    if mb.button == Mouse.left 
        pos[] = [mouseposition(ax.scene)]
        return Consume(true)
    end
    return Consume(false)
end

If you use pos.val you’re accessing the data stored in the observable. So you’re just passing a value to vlines. You want to do something like map(p -> [p[1][2]], pos). Though I think you can also just have map(p -> p[2], pos) with pos[] = mouseposition(ax.scene) in the event.

Interesting, thx!

But how does it actually work?
Why do scatter and vlines! get updated, given that they are not inside the on … if block?

using GLMakie
GLMakie.activate!()

fig, ax, p = scatter(rand(Point2f0, 100))
pos = Node([Point2f0(0)]) # observable
vlines!(ax, map(p -> [p[1][1]], pos))
display(fig)

# on move, higher priority that interactions (runs first)
on(events(ax.scene).mouseposition, priority = 2) do _
    if ispressed(ax.scene, Mouse.left)
        pos[] = [mouseposition(ax.scene)]
        scatter!(ax, pos, marker = "+")
        return Consume(true) # this would block rectangle zoom
    end
    return Consume(false)
end

# on left click/release
on(events(ax.scene).mousebutton, priority = 2) do mb
    if mb.button == Mouse.left 
        pos[] = [mouseposition(ax.scene)]
        return Consume(true)
    end
    return Consume(false)
end