Create a Draggable Element in JavaScript

What gesture are you planning to use to rotate the element? The rotation is something that can be done by using a rotate CSS transform.

The game should be for mouse and touch as in your drag tutorial. I am assuming that touch or click plus drag would move the shape (existing code), whereas touch or click plus lift the pointer would rotate the shape. So you move the shape over the pattern then rotate the shape so it will fit in place and then move the shape to final position. The pattern will be a white outline that can be selected from a group of patterns on the right side of the screen and then appear in the center work space. I am suggesting a rotation of 22.5 degree increments so that the kids have more options to make their own objects if they donā€™t want to use a pattern. Separating the drag and rotate functions with similar pointer actions is a problem. The users will be occupational therapy patients, parents, and therapists. How would the CSS rotate transform be incorporated into the existing code and identify the existing items? Thanks, Jack

This is the part I was worried about, but I think we can differentiate between a drag and click. Check out the following snippet:

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
  <title>Drag Multiple Images</title>
  <link href="https://www.kirupa.com/ssi/newDesign/kirupa_html5.css" rel="stylesheet" type="text/css">
  <style>
    body {
      margin: 20px;
    }

    #container {
      width: 600px;
      height: 400px;
      background-color: #EEE;
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;
      border-radius: 7px;
      touch-action: none;
    }

    .item {
      border-radius: 50%;
      touch-action: none;
      user-select: none;
      position: relative;
    }

    .item img {
      pointer-events: none;
    }

    .one {
      width: 100px;
      height: 100px;
      top: 0px;
      left: 0px;
    }

    .two {
      width: 60px;
      height: 60px;
      top: 30%;
      left: 10%;
    }

    .three {
      width: 40px;
      height: 40px;
      top: -40%;
      left: -10%;
    }

    .four {
      width: 80px;
      height: 80px;
      top: -10%;
      left: 5%;
    }

    .item:active {
      opacity: .75;
    }

    .item:hover {
      cursor: pointer;
    }

    h1 {
      margin-bottom: 10px;
    }
  </style>
</head>

<body>
  <h1>Drag Multiple Images</h1>
  <div id="outerContainer">
    <div id="container">
      <div class="item one">
        <img src="https://www.kirupa.com/icon/1f6a7.svg">
      </div>
      <div class="item two">
        <img src="https://www.kirupa.com/icon/1f680.svg">
      </div>
      <div class="item three">
        <img src="https://www.kirupa.com/icon/1f35d.svg">
      </div>
      <div class="item four">
        <img src="https://www.kirupa.com/icon/1f5fa.svg">
      </div>
    </div>
    <br>
    <p class="callout">Check out the <a href="https://www.kirupa.com/html5/drag.htm" class="blueEmphasis">tutorial</a>
      or the <a href="https://forum.kirupa.com/t/create-a-draggable-element-in-javascript/638149/3" class="blueEmphasis">discussion</a>
      around this effect.</p>
  </div>
  <script>
    var container = document.querySelector("#container");
    var activeItem = null;

    var active = false;
    var dragging = false;

    container.addEventListener("touchstart", dragStart, false);
    container.addEventListener("touchend", dragEnd, false);
    container.addEventListener("touchmove", drag, false);

    container.addEventListener("mousedown", dragStart, false);
    container.addEventListener("mouseup", dragEnd, false);
    container.addEventListener("mousemove", drag, false);

    container.addEventListener("click", rotateElement, false);

    function dragStart(e) {
      if (e.target !== e.currentTarget) {
        active = true;
        dragging = false;

        // this is the item we are interacting with
        activeItem = e.target;

        if (activeItem.degrees === undefined) {
          activeItem.degrees = 0;
        }

        if (activeItem !== null) {
          if (!activeItem.xOffset) {
            activeItem.xOffset = 0;
          }

          if (!activeItem.yOffset) {
            activeItem.yOffset = 0;
          }

          if (e.type === "touchstart") {
            activeItem.initialX = e.touches[0].clientX - activeItem.xOffset;
            activeItem.initialY = e.touches[0].clientY - activeItem.yOffset;
          } else {
            activeItem.initialX = e.clientX - activeItem.xOffset;
            activeItem.initialY = e.clientY - activeItem.yOffset;
          }
        }
      }
    }

    function dragEnd(e) {
      if (activeItem !== null) {
        activeItem.initialX = activeItem.currentX;
        activeItem.initialY = activeItem.currentY;
      }

      active = false;
      activeItem = null;
    }

    function drag(e) {
      if (active) {

        dragging = true;

        if (e.type === "touchmove") {
          e.preventDefault();

          activeItem.currentX = e.touches[0].clientX - activeItem.initialX;
          activeItem.currentY = e.touches[0].clientY - activeItem.initialY;
        } else {
          activeItem.currentX = e.clientX - activeItem.initialX;
          activeItem.currentY = e.clientY - activeItem.initialY;
        }

        activeItem.xOffset = activeItem.currentX;
        activeItem.yOffset = activeItem.currentY;

        setTranslate(activeItem.currentX, activeItem.currentY, activeItem);
      }
    }

    function rotateElement(e) {
      if (dragging === false) {

        if (activeItem === null) {
          activeItem = e.target;
        }

        console.log(activeItem.degrees);

        activeItem.degrees += 22.5;
        
        activeItem.querySelector("img").style.transform = `rotate(${activeItem.degrees}deg)`;
      }
    }

    function setTranslate(xPos, yPos, el) {
      el.style.transform = `translate3d(${xPos}px, ${yPos}px, 0)`;
      //el.querySelector("img").style.transform = `rotate(${degrees}deg)`;
    }
  </script>
</body>

</html>

Does this work for your needs? The way this example work is by using a dragging variable to differentiate between a regular click and a drag operation. If the click was indeed a click, I rotate the child image by 22.5 degrees. The repositioning logic deals with the parent element, and the rotation deals with the child image element. This is a silly trick dealing with how transforms work and figuring out a way to set two different transform values.

:running_man:

Your solution works great! How can I thank you? Jack

Glad to hear it!

Help someone else out one day who has a strange technical problem :stuck_out_tongue:

Kirupa, My previous problem was with dragging and rotating shapes in a Tangram game. That can now be done, but when dragging the user must be careful not to accidentally rotate the shape when stopping drag. Also when dragging the user must have the cursor on the shape when they stop dragging. If cursor is not on the shape when they click or lift finger to stop dragging, the dragging action is not stopped (though the shape may not be moving at the moment) when they move the cursor near the shape it starts moving again without any new click, and until they get the cursor on the shape (not easy on small shapes) they cannot stop the dragging function. ā€œMake a Faceā€ (a therapy program I am doing to help children identify and locate facial parts) has the same problems. Any help would be appreciated. The problems can be easily seen by viewing https://www.therapygames.online/face/face.html and trying to move a face component rapidly. The part does not always stay with the cursor. Thanks, Jack

Also above does not appear to be a problem using a finger on a touch pad but is a problem on a touch pad using a mouse. Thanks, Jack

I totally see the problem, but Iā€™m a bit swamped to look into this right now. Iā€™ll do my best to get back to you on this soon. This might be a really simple fix or something that will involve some complicated tricks, and I donā€™t have a guess on where in that range the right answer will fall :ambulance:

Thank you. I have research the issues between mouse and touch and have not found any kind of a fix, just a lot of discussion about the issues.

Hello,
The script works perfectly !
but I have one question, how to prevent element from disappearing when i drag him through the border line?, how to stop him inside to not let him pass the container shape

Thanks in advance :slight_smile:

Hi Kirupa!
Donā€™t know if you remember me - doing well with your book!
I am currently making a simulated cassette player for my website, including a ā€œfakeā€ range slider as a volume control, using your excellent ā€œdraggingā€ technique. I have limited it to the horizontal direction only, but I need to restrict the left-right movement to between two fixed points. Suggestions?

Hi jmacky - donā€™t know if this will be your answer, but while Kirupa is busy (itā€™s a difficult life being so popular!) you could check it out.

The dragging function is cancelled when you release the mouse over the dragged item. The ā€œmouseupā€ event wonā€™t fire if the pointer is not over it. Try also making the dragEnd() function run on ā€œmouseoutā€ - when it leaves the dragged item.

Unless the dragging function is cancelled the item will keep following the mouse pointer.

I can fiddle with that and see if I can provide you with some code.

For what you are trying to do, have you thought about using an actual slider element? That way you get some of the dragging for free, and you also get accessibility, keyboard shortcuts, and more as well.

Hi Mr K!! Long time no chat!!
I managed to come up with a solution already. I did think of using a range slider but I wanted a custom old fashioned image for the knob to match an old cassette. The ā€œthumbā€ does not seem to be consistently implemented yet and accessing it to style it seems a bit of a mission. It was easier to use your brilliant ā€œtranslate3dā€ idea, limit it to one axis, and use an ā€œifā€¦else ifā€¦elseā€ to impose a minimum and maximum value to the slider. Iā€™ll send you a link once I have the rest of it performing correctly. Getting very excited here!

Once Iā€™m done you might tell me if my code is clumsy or inefficient or whatever so I can learn some more from the master!

Thanks for being out there for us all!

1 Like

Greetings again O Awesome JS Guru!
Iā€™m all excited now I have the Cassette Player working completely as I want it! Hereā€™s a link:
http://www.smooth-operator.co.uk/SmoothCassette.html
It will next be integrated into our website. Looking forward to your feedback!
Have a great week!

1 Like

BTW - you will notice it only loads the tracks when you select them. The main images will also be loaded while the construction is invisible offscreen. What I am also excited about is that if I want to add tracks, which of course I will, I have only to edit the external js file, which is basically just a list. Everything else is automatic.

Felicitations once more Mr K!
The Cassette Player is now intregrated into our main site.
http://www.smooth-operator.co.uk/
So the draggable element here is the volume control. Hope my code is pleasing to you!

1 Like

Hello,is it poosible to change particle moving path on the canvas on events such us touch or mousemove?
I mean, i have particle that needs to move from point a to b,c,d(ā€¦) so along predefinied path of x and y coordinates, can i distort points of this path on touch or when pointer is near the path point?

Thank you so much for such a greet tutorial.
I just need a help
I want to drag and clone image from one div to another
can you please help me ??

Thanks for a great tutorial. Clearly explained what I wanted to know.

How to have this remember each dragged-to location, and for these to be updated in a database, so that positions would get remembered when you revisit the page?