Makie: mouse position in data coordinates

Hello,

I’m having troubles in getting the mouse position in data coordinates from a Makie plot.
Using mouseposition() seems to return the position in pixel coordinates contrary to what described in the documentation http://makie.juliaplots.org/v0.15.2/documentation/api_reference/index.html#Events. Maybe be related to the function to_world().

Here below a MWE, where mouseposition() returns the same result that mouseposition_px() and to_world(fig.scene,Point2f(pos_px)).

What am I doing wrong?

using GLMakie

img = rand(100, 100)

fig = Figure()
ax1 = Axis(fig[1,1])
image!(ax1,img) 

on(events(fig.scene).mousebutton, priority = 0) do event
     if event.button == Mouse.left
         if event.action == Mouse.press
             pos = mouseposition(fig.scene)
             pos_px = Makie.mouseposition_px(fig.scene)
             twpos = to_world(fig.scene,Point2f(pos_px))
             println("mouseposition(): $pos, mouseposition_px(): $pos_px, to_world(pos): $twpos")
             println()
         end
     end
    return Consume(false)
end

fig
1 Like

One option is to use the higher level interaction interface of the axis:

register_interaction!(ax1, :my_interaction) do event::MouseEvent, axis
    if event.type === MouseEventTypes.leftclick
        println("You clicked on the axis at datapos $(event.data)")
    end
end

see this section in the docs.

1 Like

Great suggestion, thanks a lot!

The reason why mouseposition(fig.scene) is giving you pixel coordinates is that fig.scene is in pixel coordinates. mouseposition(ax.scene) should give you axis coordinates.

3 Likes

That explains why I was not getting the position in data coordinates… Thank you!