Why is this .js SO-SLOW and Finicky ?

Hello
Why is this .js SO-SLOW and Finicky (keeps loosing connection to cursor) :
https://vmars.us/Guitar/Drag-Drop-Clone-Working-Multiple-Elements-SO-SLOW.html

and this SO FAST :
https://www.kirupa.com/html5/examples/drag_multiple.htm

And how can I make SO-Slow Slow Fast
See Codes below :
SO-SLOW:

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
  <title>
https://vmars.us/Guitar/Drag-Drop-Clone-Working-Multiple-Elements-SO-SLOW.html
  </title>
  <style>
    body {
      margin: 20px;
    }

    #imageContainer {
	}

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

    .item {
      touch-action: none;
      user-select: none;
      position: relative;
    }

    .item img {
      pointer-events: auto;
    }

    .item,
    #elem1 {
      width: 32px;
      height: 32px;
/*      top: 0px;
      left: 0px;
      stroke-opacity:0; 
*/
      z-index: 800;
    }

    .item,
    #elem2 {
      width: 28px;
      height: 28px;
      z-index: 500;
    }

    .item,
    #elem3 {
      width: 28px;
      height: 28px;
      z-index: 500;
    }

    .item,
    #elem4 {
      width: 28px;
      height: 28px;
      z-index: 500;
    }

    .item:active {
      opacity: .75;
    }

    .item:hover {
      cursor: pointer;
    }

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

<body>
  <h3>

    <a href="https://vmars.us/Guitar/Drag-Drop-Clone-Working-Multiple-Elements-SO-SLOW.html">
      https://vmars.us/Guitar/Drag-Drop-Clone-Working-Multiple-Elements-SO-SLOW.html</a>
    <br>

  </h3>

    <div id="imageContainer" style="text-align:center">

      <img class="item" id="elem1" src="https://vmars.us/Guitar/1-Red-Circle-Transp-32x32.png">
      <img class="item" id="elem2" src="https://vmars.us/Guitar/flatAccidental-Grey-28x28.png">
      <img class="item" id="elem3" src="https://vmars.us/Guitar/naturalAccidental-Grey-28x28.png">
      <img class="item" id="elem4" src="https://vmars.us/Guitar/sharpAccidental-Grey-28x28.png">

<div style="text-align:center;">
    <hr id="hr01">
</div>

  </div> <!--  id="imageContainer"  -->

  <script>
    var elemNumber = 1;
    var clodeId;
	var clonerID;
    // =================================================

    document.addEventListener('contextmenu', fireContextMenu);

    // This function will be called whenever contextmenu event occurs
    function fireContextMenu(event) {
      event.preventDefault();
      //console.log("function fireContextMenu(event)") ;
      console.log("event.target.id = " + event.target.id)

      // Create a copy of it
      var clone = event.target.cloneNode(true);    // 181

      clone.addEventListener("dragstart", (event) => { drag(event); });


      /*
      if (clone.id == "elem1" || clone.id == "elem2" || clone.id == "elem3" || clone.id == "elem4" || clone.id == "elem5"
       || clone.id == "elem6" || clone.id == "elem7" || clone.id == "elem8" || clone.id == "elem9" || clone.id == "elem10") 
       { console.log("Yep ,  " + clone.id + "  is Clone-able !") ;
       }else{
        console.log("NOPE ,  " + clone.id + "  is NOT Clone-able !") ;
        return ;
      }
      */

      //Update the ID and add a class
      clone.id = 'elem11' + elemNumber;
      console.log("New clone.id = " + clone.id)
      //
      clone.draggable = 'true';  // 148
      //clone.onclick = "helloConsole()"
      clone.zIndex = elemNumber;
      elemNumber = elemNumber + 1

      // KIRUPA: This is to ensure whatever transform was initially applied doesn't carry over to the cloned element
      clone.style.transform = "";

      // Inject it into the DOM
      // KIRUPA: Notice that I am adding it as a child of "container", for your drag code only listens
      //         to drag operations on the container element. The "draggable" attribute doesn't do much for you here.
      container.appendChild(clone);
    }
// =================================================

  </script>

  <script>
    var container = document.querySelector("#imageContainer");
    var activeItem = null;

    var active = 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);

    function dragStart(e) {
          clonerID = e.target.id;
      if (e.target !== e.currentTarget) {
        active = true;

        // this is the item we are interacting with
        activeItem = e.target;
//        console.log("activeItem = " + activeItem);

        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 {
			if (clonerID == "elem1" || clonerID == "elem2" || clonerID == "elem3" || clonerID == "elem4" || clonerID == "elem5"
 || clonerID == "elem6" || clonerID == "elem7" || clonerID == "elem8" || clonerID == "elem9" || clonerID == "elem10") 
 {            console.log("clonerID = " + clonerID);
               return ; }
            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) {
      e.preventDefault();
      if (active) {
        if (e.type === "touchmove") {

          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 setTranslate(xPos, yPos, el) {
      el.style.transform = "translate3d(" + xPos + "px, " + yPos + "px, 0)";
    }
  </script>

SO FAST :

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<title>Drag Multiple Elements</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: #333;
      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;
    }

    .one {
      width: 100px;
      height: 100px;
      background-color: rgb(245, 230, 99);
      border: 10px solid rgba(136, 136, 136, .5);
      top: 0px;
      left: 0px;
    }

    .two {
      width: 60px;
      height: 60px;
      background-color: rgba(196, 241, 190, 1);
      border: 10px solid rgba(136, 136, 136, .5);
      top: 30%;
      left: 10%;
    }

    .three {
      width: 40px;
      height: 40px;
      background-color: rgb(0, 255, 231);
      border: 10px solid rgba(136, 136, 136, .5);
      top: -40%;
      left: -10%;
    }

    .four {
      width: 80px;
      height: 80px;
      background-color: rgb(233, 210, 244);
      border: 10px solid rgba(136, 136, 136, .5);
      top: -10%;
      left: 5%;
    }

    .item:active {
      opacity: .75;
    }

    .item:hover {
      cursor: pointer;
    }
    
    h1 {
      margin-bottom: 10px;
    }
  </style>
</head>
<body>
<h1>Drag Multiple Elements</h1>
<div id="outerContainer">
<div id="container">
<div class="item one">
</div>
<div class="item two">
</div>
<div class="item three">
</div>
<div class="item four">
</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;

    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);

    function dragStart(e) {

      if (e.target !== e.currentTarget) {
        active = true;

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

        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 {
            console.log("doing something!");
            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) {
        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 setTranslate(xPos, yPos, el) {
      el.style.transform = "translate3d(" + xPos + "px, " + yPos + "px, 0)";
    }
  </script>

When I drag the elements in your example, it seems fast to me. Is what I see in my video not what you are seeing?

vmars_slow

1 Like

Thanks for your reply:
I have modified your code to add 10 circles:
https://vmars.us/Guitar/CLONE-KIRUPA-CIRCLES.html
But I haven’t yet figured out why all the Circles have changed to rectangles .
Next step is to ADD Cloning .
Here is the modified code:


<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<title>file:///C:/2023-Drag-Drop-AnyWhere/TRY-THIS/CLONE-KIRUPA-CIRCLES.html</title>
<!--  <link href="https://www.kirupa.com/ssi/newDesign/kirupa_html5.css" rel="stylesheet" type="text/css">  -->
<style>
    body {
      margin: 20px;
    }
    #containerK {
      width: 600px;
      height: 400px;
      background-color: #333;
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;
      border-radius: 7px;
      touch-action: none;
      float: left;}
    }

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

    #elemK1 {
      width: 100px;
      height: 100px;
      background-color: rgb(245, 230, 99);
      border: 10px solid rgba(136, 136, 136, .5);
      top: 0px;
      left: 0px;
    }

    #elemK2 {
      width: 60px;
      height: 60px;
      background-color: rgba(196, 241, 190, 1);
      border: 10px solid rgba(136, 136, 136, .5);
      top: 30%;
      left: 10%;
    }

    #elemK3 {
      width: 40px;
      height: 40px;
      background-color: rgb(0, 255, 231);
      border: 10px solid rgba(136, 136, 136, .5);
      top: -40%;
      left: -10%;
    }

    #elemK4 {
      width: 80px;
      height: 80px;
      background-color: rgb(233, 210, 244);
      border: 10px solid rgba(136, 136, 136, .5);
      top: -10%;
      left: 5%;
    }

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

}

    .item {
	  display: inline-block;
      border-radius: 50%;
      touch-action: none;
      user-select: none;
      counter-increment: itemCount;
      content: 'count' + itemCount;
      width: 32px;
      height: 32px;
      background-color: #F5F5F5;  //  whitesmoke 
      font-family: Arial, Helvetica, sans-serif;
      text-align:center;
      font-size:28px;
	  z-index: 8;
    }


#elem1{
  top: 250px;
//  left: 250px:
  width: 24px;
  width: 24px;
  z-index: 1000;
  float: left;}

#elem2 {
  width:24px;
  width:24px;
  z-index: 500;
  //position:absolute;
  top: 250px;
  float: left;}

#elem3 {
    width:24px;
    width:24px;
  z-index: 500;
  top: 250px;
  float: left;}
}

#elem4 {
    width:24px;
    width:24px;
  z-index: 500;
  top: 250px;
  float: left;}
}

#elem5 {
    width:24px;
    width:24px;
  z-index: 500;
  top: 250px;
  float: left;}
}

#elem6 {
    width:24px;
    width:24px;
  z-index: 500;
  top: 250px;
  float: left;}
}

#elem7 {
    width:24px;
    width:24px;
  z-index: 500;
  top: 250px;
  float: left;}
}

#elem8 {
    width:20px;
    width:20px;
  z-index: 250;
  top: 250px;
  float: left;}
}

#elem9 {
    width:20px;
    width:20px;
  top: 250px;
  float: left;}
}

#elem10 {
    width:20px;
    width:20px;
  z-index: 250;
  top: 250px;
  float: left;}
}


    .item:active {
      opacity: .75;
    }

    .item:hover {
      cursor: pointer;
    }
    
    h1 {
      margin-bottom: 10px;
    }
  </style>
</head>
<body>
<h1>Drag Multiple Elements</h1>
<h3>
<a href="file:///C:/2023-Drag-Drop-AnyWhere/TRY-THIS/CLONE-KIRUPA-CIRCLES.html">
file:///C:/2023-Drag-Drop-AnyWhere/TRY-THIS/CLONE-KIRUPA-CIRCLES.html
</a>
<br>
<a href="https://vmars.us/Guitar/CLONE-KIRUPA-CIRCLES.html">
https://vmars.us/Guitar/CLONE-KIRUPA-CIRCLES.html
</a>

</h3>
<div id="outerContainer">

<div id="containerK">
<div class="itemK" id="elemK1">
</div>
<div class="itemK" id="elemK2">
</div>
<div class="itemK" id="elemK3">
</div>
<div class="itemK" id="elemK4">
</div>
</div>
<br>

<div id="elemContainer" style="position: absolute;   top: 40px;  left:650px; display: inline; border: 2px; ">
  <div class="item" id="elem1"  class="one"  style=" position: relative; left: 10px; top: 0; background-color: #FF0004;" >1</div>
<!--    -->
  <div class="item" id="elem2"  class="two" style=" position: relative;  left: 40px; top: 0; background-color: #FE8E05;" >2</div>
<!--    -->
  <div class="item"  id="elem1" class="three"style=" position: relative;  left: 80px; top: 0; background-color: #FFFF0D;" >3</div>
<!--    -->
  <div class="item"  id="elem4" class="four"style=" position: relative;  left: 120px; top: 0; background-color: #0CB051;" >4</div>
<!--    -->
  <div class="item"  id="elem5" class="five"style=" position: relative;  left: 160px; top: 0; background-color: #3F75EC;" >5</div>
<!--    -->
  <div class="item"  id="elem6" class="six"style=" position: relative;  left: 200px; top: 0; background-color: #B064EA;" >6</div>
<!--    -->
  <div class="item" id="elem7"  class="seven"style=" position: relative;  left: 240px; top: 0; background-color: #F7A3C1;" >7</div>
<!--    -->
 <div class="item"  id="elem8" class="accidental"style=" z-index: 7; position: relative;  left: 340px; top: 0; background-color: #DCDCDC;" >♭</div>
<!--    -->
 <div class="item"  id="elem9" class="accidental"style=" z-index: 7; position: relative;  left: 380px; top: 0; background-color: #DCDCDC;" >♯</div>
<!--    -->
  <div class="item"  id="elem10" class="accidental"style=" z-index: 7; position: relative;;  left: 420px; top: 0; background-color: #DCDCDC;" >♮</div>
  
</div>  <!--  id="elemContainer"  -->

<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 containerK = document.querySelector("#containerK");
    var elemContainer = document.querySelector("#elemContainer");
    var activeItem = null;

    var active = false;
	
if (containerK) {
    containerK.addEventListener("touchstart", dragStart, false);
    containerK.addEventListener("touchend", dragEnd, false);
    containerK.addEventListener("touchmove", drag, false);

    containerK.addEventListener("mousedown", dragStart, false);
    containerK.addEventListener("mouseup", dragEnd, false);
    containerK.addEventListener("mousemove", drag, false);
}
	
if (elemContainer) {
    elemContainer.addEventListener("touchstart", dragStart, false);
    elemContainer.addEventListener("touchend", dragEnd, false);
    elemContainer.addEventListener("touchmove", drag, false);

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

    function dragStart(e) {

      if (e.target !== e.currentTarget) {
        active = true;

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

        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 {
            console.log("doing something!");
            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) {
        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 setTranslate(xPos, yPos, el) {
      el.style.transform = "translate3d(" + xPos + "px, " + yPos + "px, 0)";
    }
  </script>
</body>
</html>

Look at the end of your #containerK style rule. You have an extra } bracket. I saw that once or twice in other sections as well. Once you remove that, you’ll see the .itemK style rule getting applied :slight_smile:

1 Like

To add to what Kirupa said…
What editor are you using? When ever I put your code in VS Code it highlights a bunch of errors in your css. Also, always load up your stuff and look in the developer console of your browser, the css section will often point out errors and sometimes even tell you what it is.

2 Likes

Thanks so much for your Help…
I am having 1 more problem here ,
https://vmars.us/Guitar/CLONE-KIRUPA-CIRCLES-HOLD-ID.html
and code below:
On Kirupa’s code when I drag an item , it takes a mouseup and 1 left click to Drop an item .
Non Clones work same as yours .
On my code “for Clones” it takes a mouseup and 2 left clicks to drop an item .
How can I fix that ?
Thanks again…

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>file:///C:/2023-Drag-Drop-AnyWhere/TRY-THIS/CLONE-KIRUPA-CIRCLES-HOLD-ID.html
<br>
https://vmars.us/Guitar/CLONE-KIRUPA-CIRCLES-HOLD-ID.html
</title>

<style>
    body {
      margin: 20px;
    }
    #containerK {
      width: 600px;
      height: 400px;
      background-color: #333;
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;
      border-radius: 7px;
      touch-action: none;
      float: left;
    }

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

    #elemK1 {
      width: 100px;
      height: 100px;
      background-color: rgb(245, 230, 99);
      border: 10px solid rgba(136, 136, 136, .5);
      top: 0px;
      left: 0px;
    }

    #elemK2 {
      width: 60px;
      height: 60px;
      background-color: rgba(196, 241, 190, 1);
      border: 10px solid rgba(136, 136, 136, .5);
      top: 30%;
      left: 10%;
    }

    #elemK3 {
      width: 40px;
      height: 40px;
      background-color: rgb(0, 255, 231);
      border: 10px solid rgba(136, 136, 136, .5);
      top: -40%;
      left: -10%;
    }

    #elemK4 {
      width: 80px;
      height: 80px;
      background-color: rgb(233, 210, 244);
      border: 10px solid rgba(136, 136, 136, .5);
      top: -10%;
      left: 5%;
    }

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

    .item {
      display: flex;
      border-radius: 50%;
      touch-action: none;
      user-select: none;
/*      counter-increment: itemCount;
      content: 'count' + itemCount;
*/
      width: 32px;
      height: 32px;
      background-color: #F5F5F5;  //  whitesmoke 
      font-family: Arial, Helvetica, sans-serif;
      text-align:center;
      font-size:26px;
	  z-index: 8;
    }

#elem1{
  top: 250px;
//  left: 250px:
  width: 28px;
  width: 28px;
  z-index: 1000;
  float: left;
  }

#elem2 {
  width: 28px;
  width: 28px;
  z-index: 500;
  top: 250px;
  float: left;
  }

#elem3 {
  width: 28px;
  width: 28px;
  z-index: 500;
  top: 250px;
  float: left;
}

#elem4 {
  width: 28px;
  width: 28px;
  z-index: 500;
  top: 250px;
  float: left;
}

#elem5 {
  width: 28px;
  width: 28px;
  z-index: 500;
  top: 250px;
  float: left;
}

#elem6 {
  width: 28px;
  width: 28px;
  z-index: 500;
  top: 250px;
  float: left;
}

#elem7 {
  width: 28px;
  width: 28px;
  z-index: 500;
  top: 250px;
  float: left;
}

.accidental , #elem8 {
    width:27px;
    width:27px;
  z-index: 250;
  top: 250px;
  float: left;
}

.accidental , #elem9 {
    width:27px;
    width:27px;
  top: 250px;
  float: left;
}

accidental , #elem10 {
    width:27px;
    width:27px;
  z-index: 250;
  top: 250px;
  float: left;
}

hr , #hr01 {

}
/*
    .item:active {
      opacity: .75;
    }
*/

    .item:hover {
      cursor: pointer;
    }
    
    h1 {
      margin-bottom: 10px;
    }
  </style>
</head>
<body>
<h1>Drag Multiple Elements</h1>
<h3>
<a href="file:///C:/2023-Drag-Drop-AnyWhere/TRY-THIS/CLONE-KIRUPA-CIRCLES-HOLD-ID.html">
file:///C:/2023-Drag-Drop-AnyWhere/TRY-THIS/CLONE-KIRUPA-CIRCLES-HOLD-ID.html
</a>
<br>
<a href="https://vmars.us/Guitar/CLONE-KIRUPA-CIRCLES-HOLD-ID.html">
https://vmars.us/Guitar/CLONE-KIRUPA-CIRCLES-HOLD-ID.html
</a>

</h3>
<div id="outerContainer">

<div id="containerK">
<div class="itemK" id="elemK1">
</div>
<div class="itemK" id="elemK2">
</div>
<div class="itemK" id="elemK3">
</div>
<div class="itemK" id="elemK4">
</div>
</div>

<div id="elemContainer" style="position: absolute;   top: 120px;  left:650px; display: inline; border: 2px; ">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <div class="item" id="elem1"   style=" position: relative;  left: 10px;  top: 0; background-color: #FF0004;" >&nbsp;1</div>
<!--    -->
  <div class="item" id="elem2"   style=" position: relative;   left: 30px;  top: 0; background-color: #FE8E05;" >&nbsp;2</div>
<!--    -->
  <div class="item"  id="elem1"  style=" position: relative;  left: 50px; top: 0; background-color: #FFFF0D;" >&nbsp;3</div>
<!--    -->
  <div class="item"  id="elem4"  style=" position: relative;  left: 70px; top: 0; background-color: #0CB051;" >&nbsp;4</div>
<!--    -->
  <div class="item"  id="elem5"  style=" position: relative;  left: 90px; top: 0; background-color: #3F75EC;" >&nbsp;5</div>
<!--    -->
  <div class="item"  id="elem6"  style=" position: relative;  left: 110px; top: 0; background-color: #B064EA;" >&nbsp;6</div>
<!--    -->
  <div class="item" id="elem7"   style=" position: relative;  left: 130px; top: 0; background-color: #F7A3C1;" >&nbsp;7</div>
<!--    -->
 <div class="item"  id="elem8" class="accidental" style="  position: relative;  left: 180px; top: 0; background-color: #DCDCDC;" >&nbsp;♭</div>
<!--    -->
  <div class="item"  id="elem9" class="accidental" style="  position: relative;;  left: 200px; top: 0;  background-color: #DCDCDC;" >&nbsp;♮</div>
<!--    -->
 <div class="item"  id="elem10" class="accidental" style="  position: relative;  left: 220px; top: 0; background-color: #DCDCDC;" >&nbsp;♯</div>
<!--    -->

<br><br>
<hr id="hr01">
&nbsp;



</div>  <!--  id="elemContainer"  -->

</div> <!--  id="outerContainer"  -->

<script>
    var containerK = document.querySelector("#containerK");
    var elemContainer = document.querySelector("#elemContainer");
    var activeItem = null;

    var active = false;
	
if (containerK) {
    containerK.addEventListener("touchstart", dragStart, false);
    containerK.addEventListener("touchend", dragEnd, false);
    containerK.addEventListener("touchmove", drag, false);

    containerK.addEventListener("mousedown", dragStart, false);
    containerK.addEventListener("mouseup", dragEnd, false);
    containerK.addEventListener("mousemove", drag, false);
}
	
if (elemContainer) {
    elemContainer.addEventListener("touchstart", dragStart, false);
    elemContainer.addEventListener("touchend", dragEnd, false);
    elemContainer.addEventListener("touchmove", drag, false);

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

    function dragStart(e) {

      if (e.target !== e.currentTarget) {
        active = true;

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

        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 {
            console.log("doing something!");
            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) {
        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 setTranslate(xPos, yPos, el) {
      el.style.transform = "translate3d(" + xPos + "px, " + yPos + "px, 0)";
    }
</script>

<script>
var elemNumber = 1;
var clodeId ;
var holdID;
// =================================================

document.addEventListener('contextmenu', fireContextMenu);
	
// This function will be called whenever contextmenu event occurs
function fireContextMenu(event) {
    event.preventDefault();
console.log("function fireContextMenu(event)") ;
holdID = event.target.id
console.log("holdID = " + holdID) ;

// Create a copy of it
var clone = event.target.cloneNode(true);    // 181

clone.addEventListener("dragstart", (event) => { drag(event); });

if (clone.id == "elem1" || clone.id == "elem2" || clone.id == "elem3" || clone.id == "elem4" || clone.id == "elem5"
 || clone.id == "elem6" || clone.id == "elem7" || clone.id == "elem8" || clone.id == "elem9" || clone.id == "elem10") 
 { console.log("Yep ,  " + clone.id + "  is Clone-able !") ;
 }else{
  console.log("NOPE ,  " + clone.id + "  is NOT Clone-able !") ;
  return ;
}

//Update the ID and add a class
clone.id = 'elem2' + elemNumber;
console.log("New clone.id = " + clone.id)
//
clone.draggable = 'true';  // 150
//clone.onclick = "helloConsole()"
clone.zIndex = elemNumber;
elemNumber = elemNumber + 1

// Inject it into the DOM
hr01.after(clone);
}
// =================================================
  </script>
</body>
</html>