Makie mouse click position

Can someone please show me how to get the location of a mouse left click on a Makie.jl scene ?
Here’s what I have now:

using Makie
img = rand(100,100)
scene = Scene(resolution = (100, 100))
heatmap(img)
Makie.add_mousebuttons(scene)
pos = lift_node(scene, :mouseposition) do mpos
    if ispressed(scene, Mouse.left)
        return mpos
    end
end

Thanks a lot!

1 Like

Have you seen:https://github.com/JuliaPlots/Makie.jl/blob/master/docs/src/examples/scattermouse.jl ?

So I guess you’re looking for something like:

using Makie, GeometryTypes, Colors

img = rand(100,100)
scene = Scene()
heatmap(img)
center!(scene, 0.2)
cam = scene[:screen].cameras[:orthographic_pixel]
Makie.add_mousebuttons(scene)
pos = lift_node(scene, :mouseposition) do mpos
    if ispressed(scene, Mouse.left)
        return to_world(Point2f0(mpos), cam)
    end
    Point2f0(Inf)
end
scatter(lift_node(x-> Point2f0[x], pos), markersize = 10)
1 Like

I have, but I couldn’t figure out how to get the mouse position from just one click.

Also, in your example here (thank you btw), I can’t get the position without needing to move the mouse first, while holding down the mouse button. All I want is to click somewhere on the image and get the location of that click (in some variable I can access - a Signal perhaps).

1 Like

Ah!
You need to turn around on what node you “listen” to:

using Makie, GeometryTypes, Colors

img = rand(100,100)
scene = Scene()
heatmap(img)
center!(scene, 0.2)
cam = scene[:screen].cameras[:orthographic_pixel]
Makie.add_mousebuttons(scene)
clicks = to_node(Point2f0[])
pos = lift_node(scene, :mousebuttons) do buttons
    if length(buttons) == 1 && first(buttons) == Mouse.left
        pos = to_world(Point2f0(to_value(scene, :mouseposition)), cam)
        # awkward! one push! for adding to clicks, another to update the node!
        push!(clicks, push!(to_value(clicks), pos))
    end
    return 
end
scatter(clicks, markersize = 10)
3 Likes

Awesome opossum!!

Thank you!

what is the modern equivalent of this? I keep getting the exception
**cannot resize array with shared data**

polygon = Node([ SVector(0.0,0.0),SVector(5.0,0.0),SVector(5.0,5.0) ])
poly!(ax, polygon, color=RGBA(0,1,0,0.1), strokecolor=:black)
	
on(scene.events.keyboardbuttons) do button
		
	if ispressed(button, Keyboard._1)
		polygon[] = append!(polygon[],[SVector(mouseevents.obs.val.data...)])
	end
end
2 Likes

No, link is dead?

I couldn’t find that specific example, but here is something from the docs to get you started:

https://makie.juliaplots.org/stable/documentation/events/#mouse_interaction

Thx @yakir12, I also found the examples folder now under Makie.jl/docs/examples at master · JuliaPlots/Makie.jl (github.com), but without the said example.