Draggable <div>

How to make 4 corners resize-able?

by trentHarlem

HTML

<h2>Resize Queen</h2>
<div id="mydiv">
  <div class='resizer nw'></div>
  <div class='resizer ne'></div>
  <div class='resizer sw'></div>
  <div class='resizer se'></div>

  <div id="mydivheader">Click anywhere to move

    <div id='content'>

<div id='image-container'><img height='auto' width='100%' src='https://picsum.photos/600' /></div>

    </div>
  </div>
</div>


<script>
function myFunction() {
  var div = document.getElementById("myDiv");
  var rect = div.getBoundingClientRect();
  x = rect.left;
  y = rect.top;
  w = rect.width;
  h = rect.height;
  alert ("Left: " + x + ", Top: " + y + ", Width: " + w + ", Height: " + h);
}
</script>



<button onclick="myFunction()">Get top-left corner + witdth and height of the element with red border</button>

CSS

* {
  margin: 0;
  padding: 0;
}

#mydiv {
  position: absolute;
  /* NECESSARY */
  background-color: whitesmoke;
  box-sizing: border-box;
  text-align: center;
/*   border: 1px solid #222;
   */  height: 200px;
  width: 200px;
  /* resize: both; /* CSS RESIZE */
  overflow: hidden;
  /* CSS RESIZE */
}

#mydivheader {

/*   padding: 10px;
   */  cursor: move;
  background-color: dodgerblue;
  color: #fff;
}

#content {
  color: #000;
  margin: 0px;
  background-color: whitesmoke;
}


/* ::-webkit-resizer {
  position: absolute;
  height: 20px;
  width: 20px;
  border-top-left-radius: 25px;
  background-color: #dd0;
  z-index: 2;
} */

.resizer {
  position: absolute;
  height: 20px;
  width: 20px;
  background-color: #dd0;
  z-index: 2;
}

.resizer.nw {
  top: -1px;
  left: -1px;
  cursor: nw-resize;
  border-bottom-right-radius: 25px;
}

.resizer.ne {
  top: -1px;
  right: -1px;
  cursor: ne-resize;
  border-bottom-left-radius: 25px;
}

.resizer.sw {
  bottom: -1px;
  left: -1px;
  cursor: sw-resize;
  border-top-right-radius: 25px;
}

.resizer.se {
  bottom: -1px;
  right: -1px;
  cursor: se-resize;
  border-top-left-radius: 25px;
}

JavaScript

const myDiv = document.getElementById('mydiv')
let isResizing = false;

//Make the DIV element draggagle:
dragElement(myDiv);

function dragElement(elmnt) {
  if (!isResizing) {
    let pos1 = 0,
      pos2 = 0,
      pos3 = 0,
      pos4 = 0;
    if (document.getElementById(elmnt.id + "header")) {
      //if present, the header is where you move the DIV from:
      document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
    } else {
      //otherwise, move the DIV from anywhere inside the DIV:
      elmnt.onmousedown = dragMouseDown;
    }

    function dragMouseDown(e) {
      e = e || window.event;
      e.preventDefault();
      // get the mouse cursor position at startup:
      pos3 = e.clientX;
      pos4 = e.clientY;
      document.onmouseup = closeDragElement;
      // call a function whenever the cursor moves:
      document.onmousemove = elementDrag;
    }

    function elementDrag(e) {
      e = e || window.event;
      e.preventDefault();
      // calculate the new cursor position:
      pos1 = pos3 - e.clientX;
      pos2 = pos4 - e.clientY;
      pos3 = e.clientX;
      pos4 = e.clientY;
      // set the element's new position:
      elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
      elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
    }

    function closeDragElement() {
      //stop moving when mouse button is released:
      document.onmouseup = null;
      document.onmousemove = null;
    }
  }
}

(function fourCorners() {

  const resizers = document.querySelectorAll('.resizer')
  let currentResizer
  console.log(isResizing)

  for (let resizer of resizers) {
    resizer.addEventListener('mousedown', mouseDown)

    function mouseDown(e) {
      //e.preventDefault() 

      currentResizer = e.target
      isResizing = true;
      console.log(isResizing)

      let posX = e.clientX;
      let posY = e.clientY;
      myDiv.addEventListener('mousemove', mouseMove)
      myDiv.addEventListener('mouseup', mouseUp)

     ...