Only return mouse position if inside an Axis using GLMakie

I’m making an interactive heatmap where I get the mouse position and update a second plot. I provided a MWE where a horizontal line moves with the mouse, but I only want to update the line when the mouse is inside of this Axis. I see that pick() might be the way to do this, but I can’t seem to figure out how. What should I be using?

using GLMakie

fig = Figure()

yline = Observable(50.0)

ax = Axis(fig[1, 1])
hm = heatmap!(rand(100, 100))
hlines!(yline, color = :red)

on(events(ax).mouseposition) do mpos
    yline[] =  trunc(Int, mouseposition(ax.scene)[2])
    yaxis_limits = ax.yaxis.attributes.limits[]
    
    if yline[] >= yaxis_limits[1] && yline[] <= yaxis_limits[2]
        println("In bounds")
    else
        println("Out of bounds")
    end
end

fig

Maybe something like this:

yaxis_limits = ax.yaxis.attributes.limits[]
xaxis_limits = ax.xaxis.attributes.limits[]

on(events(ax).mouseposition) do mpos
    if (yaxis_limits[1] <= trunc(Int, mouseposition(ax.scene)[2]) <= yaxis_limits[2]) &&
       (xaxis_limits[1] <= trunc(Int, mouseposition(ax.scene)[1]) <= xaxis_limits[2])
        yline[] =  trunc(Int, mouseposition(ax.scene)[2])
    end
end
1 Like

Try:

	on(events(fig).mouseposition) do pos
		if is_mouseinside(ax)
			yourvariable[] = mouseposition(ax)
		end

Yes, thank you. This works and is the sort of method I was looking for.