我有一个已经应用了旋转的矩形。我想获得未旋转的尺寸(x、y、宽度、高度)。
这是当前元素的尺寸:
Bounds at a 90 rotation: {
height 30
width 0
x 25
y 10
}
以下是旋转设置为无后的尺寸:
Bounds at rotation 0 {
height 0
width 30
x 10
y 25
}
过去,我能够将旋转设置为0,然后读取更新后的边界。但是,我正在使用的其中一个功能存在错误,所以现在我必须手动执行此操作。
是否有一个简单的公式可以使用我已有的信息获取旋转 0 处的边界?
更新:对象围绕对象的中心旋转。
UPDATE:
我需要的是类似下面的函数:
function getRectangleAtRotation(rect, rotation) {
var rotatedRectangle = {}
rotatedRectangle.x = Math.rotation(rect.x * rotation);
rotatedRectangle.y = Math.rotation(rect.y * rotation);
rotatedRectangle.width = Math.rotation(rect.width * rotation);
rotatedRectangle.height = Math.rotation(rect.height * rotation);
return rotatedRectangle;
}
var rectangle = {x: 25, y: 10, height: 30, width: 0 };
var rect2 = getRectangleAtRotation(rect, -90); // {x:10, y:25, height:0, width:30 }
我发现了类似的问题here.
UPDATE 2
这是我的代码。它尝试获取线的中心点,然后获取 x、y、宽度和高度:
var centerPoint = getCenterPoint(line);
var lineBounds = {};
var halfSize;
halfSize = Math.max(Math.abs(line.end.x-line.start.x)/2, Math.abs(line.end.y-line.start.y)/2);
lineBounds.x = centerPoint.x-halfSize;
lineBounds.y = centerPoint.y;
lineBounds.width = line.end.x;
lineBounds.height = line.end.y;
function getCenterPoint(node) {
return {
x: node.boundsInParent.x + node.boundsInParent.width/2,
y: node.boundsInParent.y + node.boundsInParent.height/2
}
}
我知道我的示例使用直角,您可以用它交换 x 和 y,但旋转可以是任意数量。
UPDATE 3
我需要一个返回矩形未旋转边界的函数。我已经有了特定旋转的界限。
function getUnrotatedRectangleBounds(rect, currentRotation) {
// magic
return unrotatedRectangleBounds;
}