Is there a way to code a drag and drop behavior on an image?

Trying to add code to drag an image when it is pressed by the mouse and drop it when the mouse is released. Is this possible in App Lab?

In Javascript an drag operation involves three kinds of events:

  1. A mousedown event indicates that the user has pushed down a mouse button while the curser was positioned over the draggable event.
  2. A series of mousemove events is generated as the user moves the curser around the screen.
  3. A mouseup event that signals that the user has released the mouse button, completing the drag operation.

These events are available in AppLab.

Yes. I think click and drag is an interesting exercise because it reveals how something so simple seeming can be challenging from scratch.

Here is a very basic demo that shows the principle: https://studio.code.org/projects/applab/yOp_38MuwcDhWKNLmoB2mQ/

The key insights you need are:

(1) that the event parameter is an object that contains lots of properties for detecting the position of the mouse. do a console.log on the event property to see the possibilities. But e.g. event.x will give you the x-position of the mouse at the time the event occurred (for which you have an event handler setup).

(2) You need to maintain some kind of variable to track the state of the object - i.e. isDragging so that the three discrete events you can check know what to do. ie. mousedown on the images sets isDragging to true; mouseup sets isDragging to false, and mousemove applied to the entire screen can check whether isDragging is true to know whether to reposition the image.

Hope this helps,

Baker

2 Likes

Hi Baker,

what you described here is exactly what I am searching for, the demo is really nice!
But unfortunately mouse events don’t work for that on my android phone. Do you have a solution for a draggable element also working on mobile phones in general?

Hope you can help,

Nadine

Hi @nadine,

This is a great question - have you tried e-mailing support@code.org about this question? I think the engineers there might be able to give you an answer on this one since it is a bit more technical. If you find out something helpful from them, please report back, I am interested in knowing this too!

KT

I tried Baker’s code just now and it didn’t work very smoothly for me ('19-'20 AppLab, Mozilla, MacOS).

I made some changes, mainly getting the screen to collect mouseup events. This version allows different elements to be dragged, and adding drag-ability to any UI element just requires a mousedown event handler.

1 Like

@nilvednairb I changed your code some, there was drift when dragging probably caused by race conditions. I made it so it just centers the object on the mouse while dragging. This may be a desireable side effect. https://studio.code.org/projects/applab/QcScG0VVlyezGgvGsKvimBfOEDp57ND7s0Dd684X04s

If it is not a desirable side effect I corrected for it. Though it makes code more complex.
https://studio.code.org/projects/applab/g3Q6so46nK07lxfzyjF2waEkbpsJ6Rs5oFX3Meb18PE