Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. $(function () {
  2. var $circle = $('#circle'),
  3. $handler = $('#handler'),
  4. $p = $('#test'),
  5. handlerW2 = $handler.width()/2,
  6. rad = $circle.width()/2,
  7. offs = $circle.offset(),
  8. elPos = {x:offs.left, y:offs.top},
  9. mHold = 0,
  10. PI2 = Math.PI/180;
  11. $handler.mousedown(function() { mHold = 1; });
  12. $(document).mousemove(function(e) {
  13. if (mHold) {
  14. var mPos = {x:e.pageX-elPos.x, y:e.pageY-elPos.y},
  15. atan = Math.atan2(mPos.x-rad, mPos.y-rad),
  16. deg = -atan/PI2+180,
  17. perc = (deg*100/360)|0,
  18. X = Math.round(rad* Math.sin(deg*PI2)),
  19. Y = Math.round(rad* -Math.cos(deg*PI2));
  20. $handler.css({left:X+rad-handlerW2, top:Y+rad-handlerW2, transform:'rotate('+deg+'deg)'});
  21. }
  22. }).mouseup(function() { mHold = 0; });
  23. });
  24.  
  25. $(function(){
  26. var $container = $('#rotationSliderContainer');
  27. var $slider = $('#rotationSlider');
  28. var $degrees = $('#rotationSliderDegrees');
  29.  
  30. var sliderWidth = $slider.width();
  31. var sliderHeight = $slider.height();
  32. var radius = $container.width()/2;
  33. var deg = 0;
  34.  
  35. X = Math.round(radius* Math.sin(deg*Math.PI/180));
  36. Y = Math.round(radius* -Math.cos(deg*Math.PI/180));
  37. $slider.css({ left: X+radius-sliderWidth/2, top: Y+radius-sliderHeight/2 });
  38.  
  39. var mdown = false;
  40. $container
  41. .mousedown(function (e) { mdown = true; e.originalEvent.preventDefault(); })
  42. .mouseup(function (e) { mdown = false; })
  43. .mousemove(function (e) {
  44. if(mdown)
  45. {
  46.  
  47. // firefox compatibility
  48. if(typeof e.offsetX === "undefined" || typeof e.offsetY === "undefined") {
  49. var targetOffset = $(e.target).offset();
  50. e.offsetX = e.pageX - targetOffset.left;
  51. e.offsetY = e.pageY - targetOffset.top;
  52. }
  53.  
  54. if($(e.target).is('#rotationSliderContainer'))
  55. var mPos = {x: e.offsetX, y: e.offsetY};
  56. else
  57. var mPos = {x: e.target.offsetLeft + e.offsetX, y: e.target.offsetTop + e.offsetY};
  58.  
  59. var atan = Math.atan2(mPos.x-radius, mPos.y-radius);
  60. deg = -atan/(Math.PI/180) + 180; // final (0-360 positive) degrees from mouse position
  61.  
  62.  
  63. // for attraction to multiples of 90 degrees
  64. var distance = Math.abs( deg - ( Math.round(deg / 90) * 90 ) );
  65.  
  66. if( distance <= 5 )
  67. deg = Math.round(deg / 90) * 90;
  68.  
  69. if(deg == 360)
  70. deg = 0;
  71.  
  72. X = Math.round(radius* Math.sin(deg*Math.PI/180));
  73. Y = Math.round(radius* -Math.cos(deg*Math.PI/180));
  74.  
  75. $slider.css({ left: X+radius-sliderWidth/2, top: Y+radius-sliderHeight/2 });
  76.  
  77. var roundDeg = Math.round(deg);
  78.  
  79. $degrees.html(roundDeg + '&deg;');
  80. $('#imageRotateDegrees').val(roundDeg);
  81.  
  82. }
  83. });
  84. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement