I've been searching long time, but can't find a better way solve my problem,
make div draggable, rotate and resize by each handle like these 2 example 1 2,, now it can be draggable, but rotate..
Regarding Prasanth K C, Chango, Yi Jiang ..'s answer, these code maybe not correct,
1. it should have a rotate point around the origin.
2. need to consider radius.
But I don't know how to use sin or cos here to make rotate consider radius?
Any suggestion will be be appreciated.
http://jsfiddle.net/tBgLh/8/
var dragging = false, target_wp;
$('.handle').mousedown(function(e) {
var o_x = e.pageX, o_y = e.pageY; // origin point
e.preventDefault();
e.stopPropagation();
dragging = true;
target_wp=$(e.target).closest('.draggable_wp');
$(document).mousemove(function(e) {
if (dragging) {
var s_x = e.pageX, s_y = e.pageY; // start rotate point
if(s_x !== o_x && s_y !== o_y){ //start rotate
var s_rad = Math.atan2(s_y, s_x);
var degree = (s_rad * (360 / (2 * Math.PI)));
target_wp.css('-moz-transform', 'rotate(' + degree + 'deg)');
target_wp.css('-moz-transform-origin', '50% 50%');
target_wp.css('-webkit-transform', 'rotate(' + degree + 'deg)');
target_wp.css('-webkit-transform-origin', '50% 50%');
target_wp.css('-o-transform', 'rotate(' + degree + 'deg)');
target_wp.css('-o-transform-origin', '50% 50%');
target_wp.css('-ms-transform', 'rotate(' + degree + 'deg)');
target_wp.css('-ms-transform-origin', '50% 50%');
}
}
})
$(document).mouseup(function() {
dragging = false
})
})// end mousemove
html
<div class="draggable_wp">
<div class="el"></div>
<div class="handle"></div>
</div>
They use a matrix function in a transform properties. You can rotate element by muliplay your matrix (element coordinates) by rotation matrix.
More info and examples here: The CSS3 matrix() Transform for the Mathematically Challenged
There are two problems with your approach:
The origin shouldn't be where the user clicked (that is the handle), but a fixed point in your div:
You will use the clicked point also, but for something else (more later):
Finally, the origin should be fixed (i.e. should not change between rotations). One way of doing so is preserving it as a
data
attribute (there are other options though):Update: One good candidate for the origin is the CSS property
transform-origin
, if present - it should ensure that the mouse follow the handle as closely as possible. This is an experimental feature, however, so the actual resulsts may vary. P.S. I'm not sure setting it to50% 50%
is a good idea, since the transformation itself may vary the element's width and height, top and left.To find the angle, you should not call
atan2
on the mouse point only, since it will only calculate the angle between that point and the top left corner of the page. You want the angle between that point and the origin:That'll lead you halfway, but it will still behave oddly (it will rotate around the element origin, but not following the handle as you expect). To make it follow the handle, you should adjust the angle in relation to the clicked point - which will serve as a base for the amount to rotate:
After that you get the rotation working (for one user iteration at least).
You'll notice the handle does not follow the mouse precisely, and the reason is the choice of the origin point - defaulting to the element's top/left corner. Adjust it to somewhere inside the element (maybe using a
data-
attribute) and it should work as expected.However, if the user interacts with the handle multiple times, it's not enough to just set the rotation angle, you must update whatever it was during the last iteration. So I'm adding a
last_angle
var that will be set on the first click and then added to the final angle during drag:Here's the final working example. (Note: I fixed the nesting of your mouse handlers, so they don't get added again after each click)