5

Please take a look at my jsfiddle to understand my problem: The yellow is the parent div that you can rotate by using the red circle, it's also draggable, inside this div there is another div with a black border that we could consider as a containment to the blue circle. The blue circle is draggable within the containment boundaries, the y coordinate only is required nothing more, the problem happens if you rotate the yellow div ex; 90 degrees or more, the mouse coordinates are not matching the expected dragging position, I've tried to solve the issue by using trigonometry and reversing the axis but it in-vain, I hope someone could solve this and leave a jsfiddle for a working sample is highly appreciated. 'please don't direct me to other posts as I think I have read everything related to this issue'

HTML

<div id="container">
  <div id="containment">
    <div id="circle"></div>
  </div>
</div>

CSS

#container{width:100px;
       height:140px;
       background:#FFEB3B;
       position:absolute;
       top:50%;
       bottom:50%;
       margin-top:-70px;
       margin-right:-50px}

.ui-rotatable-handle {
        height: 30px;
        width: 30px;
        cursor: pointer;
        background:#ff0000;
        border-radius:50%;
        right: -15px;
        bottom: -15px;
        position:absolute;
        z-index:1}

#containment{width:30px;
         height:190px;
         position:absolute;
         bottom:70px;
         left:35px;
         border:solid 1px #000}

#circle{width:30px;
         height:30px;
         position:absolute;
         top:0;
         left:0;
         border-radius:50%;
         background:#3F51B5}             

JavaScript

$(function(){
            $('#container').draggable().rotatable();
            $('#circle').draggable({containment: $('#containment')
            });
          });

2 Answers2

0

Only way I found it's to add "fake" rectangle and show it on needed degrees. I added only 1 fake rectangle for 45deg+ but you can also add other 2 by same logic for 90deg+ and 115deg+. See solution there.

HTML

<div id="container">
  <div id="containment">
    <div id="circle" class="circle"></div>
  </div>
  <div id="containment2">
    <div id="circle2" class="circle">
      
    </div>
  </div>
</div>

CSS

#container {
  width: 100px;
  height: 140px;
  background: #FFEB3B;
  position: absolute;
  top: 50%;
  bottom: 50%;
  margin-top: -70px;
  margin-right: -50px;
}

.ui-rotatable-handle {
  height: 30px;
  width: 30px;
  cursor: pointer;
  background: #ff0000;
  border-radius: 50%;
  right: -15px;
  bottom: -15px;
  position: absolute;
  z-index: 1;
}

#containment,
#containment2 {
  width: 30px;
  height: 190px;
  position: absolute;
  bottom: 70px;
  left: 35px;
  border: solid 1px #000
}

#containment2 {
  width: 190px;
  height: 30px;
  left: 51px;
  bottom: 54px;
  transform: rotate(-90deg);
  border: solid 1px green;
  transform-origin: left;
  display: none;
}

.circle {
  width: 30px;
  height: 30px;
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 50%;
  background: #3F51B5;
  z-index: 10;
}

#circle2 {
  left: calc(100% - 30px);
  display: none;
}

#wrapper {
  width: 100%;
  height: 100%;
  outline: 1px solid red;
}

JavaScript

$(function() {
  var Pi = 3.14;
  $('#container').draggable({}).rotatable({
    rotate: function(event, ui) {
      let deg = ui.angle.current * 180 / Pi;
      $("#containment").show();
      $("#containment2").hide();
      $("#circle").show();
      $("#circle2").hide();
      if (deg > 45) {
        $("#containment").hide();
        $("#containment2").show();
        $("#circle").hide();
        $("#circle2").show();
      }
    },
  });

  $('#circle').draggable({
    stop: function(event, ui) {
        console.log(ui.position.top);
        $("#circle2").css("inset", ui.position.left + "px auto auto " + (160 - ui.position.top) + "px");
    },
    containment: $('#containment')
  });
  $('#circle2').draggable({
    stop: function(event, ui) {
        $("#circle").css("inset", 160 - ui.position.left + "px auto auto " + ui.position.top + "px");
    },
    containment: $('#containment2')
  });
  
});
Yura Kosyak
  • 401
  • 3
  • 16
-1

I had some similar issues with JQuery, inside an rtl application. the reason was that since the orientation of the layout uses left css positioning - the right was missing and the calculations went wrong.

Adding them while creating the element solved it.

yossi
  • 3,090
  • 7
  • 45
  • 65