我想让用户像在photoshop中一样旋转图像,我创造了一个小提琴。 旋转度感到有些不适,长期以来一直在努力,需要一些帮助。
Js Fiddle链接 - http://jsfiddle.net/madhuri2987/fZegT/8/
全屏 - http://jsfiddle.net/madhuri2987/fZegT/8/embedded/result/
HTML
<div id="mainTarget">
<img src="http://myreaxns.com/wp-content/uploads/2013/03/priyanka-chopra-image.jpg" width="300" class="mainTarget" />
<div id="target"> </div>
</div>
JS
$(window).load(function(){
var dragging = false
$(function() {
var target = $('#target');
var mainTarget = $('#mainTarget');
target.mousedown(function() {
dragging = true
})
$(document).mouseup(function() {
dragging = false
})
$(document).mousemove(function(e) {
if (dragging) {
var mouse_x = e.pageX;
var mouse_y = e.pageY;
var radians = Math.atan2(mouse_x - 10, mouse_y - 10);
var degree = (radians * (180 / Math.PI) * -1) + 90;
mainTarget.css('-moz-transform', 'rotate(' + degree + 'deg)');
mainTarget.css('-moz-transform-origin', '50% 50%');
mainTarget.css('-webkit-transform', 'rotate(' + degree + 'deg)');
mainTarget.css('-webkit-transform-origin', '50% 50%');
mainTarget.css('-o-transform', 'rotate(' + degree + 'deg)');
mainTarget.css('-o-transform-origin', '50% 50%');
mainTarget.css('-ms-transform', 'rotate(' + degree + 'deg)');
mainTarget.css('-ms-transform-origin', '50% 50%');
}
})
})
});
答案 0 :(得分:0)
我找到了解决方案,修改后的脚本如下:
小提琴链接 - http://jsfiddle.net/madhuri2987/fZegT/9/
JS
$(window).load(function(){
var dragging = false
$(function() {
var target = $('#target');
var mainTarget = $('#mainTarget');
var offset = mainTarget.offset();
target.mousedown(function() {
dragging = true
})
$(document).mouseup(function() {
dragging = false
})
$(document).mousemove(function(e) {
if (dragging) {
var center_x = (offset.left) + (mainTarget.width()/2);
var center_y = (offset.top) + (mainTarget.height()/2);
var mouse_x = e.pageX; var mouse_y = e.pageY;
var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
var degree = (radians * (180 / Math.PI) * -1) + 120;
mainTarget.css('-moz-transform', 'rotate(' + degree + 'deg)');
mainTarget.css('-moz-transform-origin', '50% 50%');
mainTarget.css('-webkit-transform', 'rotate(' + degree + 'deg)');
mainTarget.css('-webkit-transform-origin', '50% 50%');
mainTarget.css('-o-transform', 'rotate(' + degree + 'deg)');
mainTarget.css('-o-transform-origin', '50% 50%');
mainTarget.css('-ms-transform', 'rotate(' + degree + 'deg)');
mainTarget.css('-ms-transform-origin', '50% 50%');
}
})
})
});
如果有人认为我错了,并且有更好的方法可以做到这一点,请告诉我..谢谢:)
答案 1 :(得分:0)
使用较新的jQuery v。您不必定位所有 - 特定于浏览器的属性。
仅使用transform
。
的 LIVE DEMO 强>
var dragging = false;
$(function() {
var target = $('#target');
var mainTarget = $('#mainTarget');
var rad = mainTarget.width()/2;
var elOfs = mainTarget.offset();
var elPos = {
x: elOfs.left,
y: elOfs.top
};
target.mousedown(function() {
dragging = true;
});
$(document).mouseup(function() {
dragging = false;
});
$(document).mousemove(function(e) {
if (dragging) {
var mPos = {
x: e.pageX-elPos.x,
y: e.pageY-elPos.y
};
var getAtan = Math.atan2(mPos.x-rad, mPos.y-rad);
var getDeg = -getAtan/(Math.PI/180) + 135; //135 = (180deg-45deg)
mainTarget.css({transform: 'rotate(' + getDeg + 'deg)'});
}
});
});
理想情况下,你可以这样做:
http://jsbin.com/ifezoq/2/edit
$(function() {
var dragging = 0;
var target = $('#target');
var mainTarget = $('#mainTarget');
var elOfs = mainTarget.offset();
var cent = {X: mainTarget.width()/2, Y: mainTarget.height()/2};
var elPos = {X: elOfs.left, Y: elOfs.top};
target.mousedown(function() {
dragging = true;
});
$(document).mouseup(function() {
dragging = 0;
}).mousemove(function(e) {
if(dragging) {
var mPos = {X: e.pageX-elPos.X, Y: e.pageY-elPos.Y};
var getAtan = Math.atan2(mPos.X-cent.X, mPos.Y-cent.Y);
var getDeg = -getAtan/(Math.PI/180) + 135;
mainTarget.css({transform: 'rotate(' + getDeg + 'deg)'});
}
});
});
但是如果你在圆圈内有一个方形图像,那么可拖动角落总是在45度。
但是你可以看到差距是在处理矩形时得到起始角度差异 - 你需要应用额外的数学,如果你问我就不需要,所以我建议第一个演示。