【发布时间】:2014-02-21 12:06:17
【问题描述】:
我正在开发一个用于在谷歌地图上定位图像的用户界面。 我从:http://overlay-tiler.googlecode.com/svn/trunk/upload.html 开始,这与我想要的非常接近。
但我想要一个旋转工具、一个缩放工具和一个平移工具(后者存在),而不是 3 个接触点。
我尝试添加一个旋转工具,但它没有按预期工作:
我在左下角放了一个点来控制旋转(围绕图像的中心)。鼠标拖动控制点,我计算其他 3 个点。
我的代码基于移动器对象,但我更改了 onMouseMove 函数:
overlaytiler.Rotater.prototype.rotateDot_ = function(dot, theta, origin) {
dot.x = ((dot.x - origin.x) * Math.cos(theta) - (dot.y - origin.y) * Math.sin(theta)) + origin.x;
dot.y = ((dot.x - origin.x) * Math.sin(theta) + (dot.y - origin.y) * Math.cos(theta)) + origin.y;
dot.render();
};
overlaytiler.Rotater.prototype.onMouseMove_ = function(e) {
var dots = this.controlDots_;
var center = overlaytiler.Rotater.prototype.getCenter_(dots);
// Diagonal length
var r = Math.sqrt(Math.pow(this.x - center.x, 2) + Math.pow(this.y - center.y, 2));
var old = {
x: this.x,
y: this.y
};
// Real position
var newPos = {
x: this.x + e.clientX - this.cx,
y: this.y + e.clientY - this.cy
}
var newR = Math.sqrt(Math.pow(newPos.x - center.x, 2) + Math.pow(newPos.y - center.y, 2));
var theta = - Math.acos((2 * r * r - (Math.pow(newPos.x - old.x, 2) + Math.pow(newPos.y - old.y, 2))) / (2 * r * r));
// Fixed distance position
this.x = (newPos.x - center.x) * (r / newR) + center.x;
this.y = (newPos.y - center.y) * (r / newR) + center.y;
dots[1].x = center.x + (center.x - this.x);
dots[1].y = center.y + (center.y - this.y);
dots[1].render();
overlaytiler.Rotater.prototype.rotateDot_(dots[2], theta, center);
overlaytiler.Rotater.prototype.rotateDot_(dots[0], theta, center);
// Render
this.render();
this.cx = e.clientX;
this.cy = e.clientY;
};
不幸的是,精度和角度符号存在问题。
经过几次旋转后,图像高度失真,仅支持一个方向的旋转。
我想知道如何才能实现高精度且不失真。
也许我的方法在这里没用(尝试将角移动到正确的坐标),我尝试使用画布旋转图像但我的尝试不成功。
编辑:完整工作版:http://jsbin.com/iQEbIzo/7/
【问题讨论】:
标签: javascript google-maps image-manipulation image-rotation