Get the source of an event

I wanted to do a small app to move geometric shapes on screen.

I came across Elm SVG Drag and Drop which showcase a circle that can be moved around using the mouse, perfect.

But then I looked at the code to see how I could have more then one shape on screen.

The core is here : circleAttributes circle =[ ... , onMouseDown MouseDown ]

and here : MouseDown coordinate -> ( { model | cursor = Just coordinate }, Cmd.none)

It means there is no obvious way to tell Elm “mousedown” was on this shape.

What is the good approach to handle that ?

Hi @setop,

I didn’t study the code too closely so this suggestion may be wrong.

How about chaging the type from MouseDown Coordinate to MouseDown Int Coordinate?

Than, in the function that create the circle you can use it this way: onMouseDown (MouseDown id) where id is some integer that you pass to that function. This integer will be then available in the update function.

If I try to “parametrize” event with onMouseDown MouseDown id which would be elegant solution, I get The function 'onMouseDown' expects 1 arguments, but got 2 instead :frowning:

You need parenthesis, I added them now in the above message

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.