简体   繁体   English

顺时针(仅)围绕元素旋转

[英]clockwise (only) rotation around an element

I'm working on a project using this code to drag an element around another circular element: http://jsfiddle.net/sandeeprajoria/x5APH/11/ 我正在使用此代码将项目拖动到另一个圆形元素周围的项目: http//jsfiddle.net/sandeeprajoria/x5APH/11/

    function rotateAnnotationCropper(offsetSelector, xCoordinate, yCoordinate, cropper){
                //alert(offsetSelector.left);

                var x = xCoordinate - offsetSelector.offset().left - offsetSelector.width()/2;
                var y = -1*(yCoordinate - offsetSelector.offset().top - offsetSelector.height()/2);
                var theta = Math.atan2(y,x)*(180/Math.PI);        


                var cssDegs = convertThetaToCssDegs(theta);
                var rotate = 'rotate(' +cssDegs + 'deg)';
                cropper.css({'-moz-transform': rotate, 'transform' : rotate, '-webkit-transform': rotate, '-ms-transform': rotate});
                $('body').on('mouseup', function(event){ $('body').unbind('mousemove')});

        }

        function convertThetaToCssDegs(theta){
            var cssDegs = 90 - theta;
            return cssDegs;
        }

        $(document).ready(function(){               

            $('#marker').on('mousedown', function(){
                $('body').on('mousemove', function(event){
                    rotateAnnotationCropper($('#innerCircle').parent(), event.pageX,event.pageY, $('#marker'));    
                });

            });                    
        }); 

I need to amend it so it only rotates in one direction, either clockwise or anti-clockwise. 我需要对其进行修改,使其仅在一个方向上旋转,顺时针或逆时针旋转。

Any advice on how to tackle it as i am stumped. 关于如何处理它的任何建议,因为我难倒。

Just keep track of the previous rotation-degrees, and only adjust css when the result is greater than previous: 只需跟踪前一个旋转度,只在结果大于前一个时调整css:

 var previousCssDegs = 45;
 var clockwise = true; // set false for anti-clockwise

 function rotateAnnotationCropper() {
     // snip
     var cssDegs = convertThetaToCssDegs(theta);

     var isClockwise = 
         (
             cssDegs > previousCssDegs 
             && cssDegs - previousCssDegs < 20 // disallow to move from -80 to 80
         )
         || cssDegs < -80 && previousCssDegs > 80 // allow to move pass 90 > -90
     ;
     if(isClockwise == clockwise) {
          var rotate = 'rotate(' +cssDegs + 'deg)';
          cropper.css({'-moz-transform': rotate, 'transform' : rotate, '-webkit-transform': rotate, '-ms-transform': rotate});
          previousCssDegs = cssDegs;
     }
     $('body').on('mouseup', function(event){ $('body').unbind('mousemove')});
 }

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM