我正在尝试将 BitmapData 的内容绘制到另一个尚未创建的内容中。
但在绘制之前,我需要缩放和旋转图像,然后绘制它。
我的问题是我不知道转换后 BitmapData 的大小,所以我无法创建新的来绘制它。
这个方法显示了我的意思:
public function getTransformedBitmapData(origin:BitmapData):BitmapData
{
var matrix:Matrix = new Matrix();
// ajusting the anchor point and rotating
matrix.translate(-origin.width / 2, -origin.height / 2);
matrix.rotate(Math.PI / 4); // 45 deg
matrix.translate(origin.width / 2, origin.height / 2);
// scaling
matrix.scale(1.5, 1.5);
// Calculating the size of the new BitmapData
var width:Number = 0; // I don't know this value!
var height:Number = 0; // I don't know this value!
// Creating and drawing (with transformation)
var result:BitmapData = new BitmapData(width, height, true, 0);
result.draw(origin, matrix);
return result;
}
有人知道我应该怎么做才能找出(计算)转换后该图像的大小?
这张图片说明了旋转的 Action ,以及我想了解的内容:
最佳答案
好的,使用@ansiart 答案作为起点,我设法以这种方式计算尺寸:
public function getTransformedBitmapData(origin:BitmapData):BitmapData
{
var matrix:Matrix = new Matrix();
// ajusting the anchor point and rotating
matrix.translate(-origin.width / 2, -origin.height / 2);
matrix.rotate(Math.PI / 4); // 45 deg
matrix.translate(origin.width / 2, origin.height / 2);
// scaling
matrix.scale(1.5, 1.5);
// Finding the four corners of the bounfing box after transformation
var topLeft:Point = matrix.transformPoint(new Point(0, 0));
var topRight:Point = matrix.transformPoint(new Point(origin.width, 0));
var bottomLeft:Point = matrix.transformPoint(new Point(0, origin.height));
var bottomRight:Point = matrix.transformPoint(new Point(origin.width, origin.height));
// Calculating "who" is "where"
var top:Number = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
var bottom:Number = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
var left:Number = Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
var right:Number = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
// Ajusting final position
matrix.translate(-left, -top);
// Calculating the size of the new BitmapData
var width:Number = right - left;
var height:Number = bottom - top;
// Creating and drawing (with transformation)
var result:BitmapData = new BitmapData(width, height, false, 0);
result.draw(origin, matrix);
return result;
}
我认为这可能有点矫枉过正,但确实有效。
关于actionscript-3 - 基于矩阵计算新的 BitmapData 的大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11481197/