ActionScript 3 绘制具有注册点任何坐标的显示对象

示例

    public function drawDisplayObjectUsingBounds(source:DisplayObject):BitmapData {
        var bitmapData:BitmapData;//声明一个BitmapData
        var bounds:Rectangle = source.getBounds(source);//获取源对象的实际大小
        //整数像素值的圆角边界(不包含1px的紫色条纹)
        bounds = new Rectangle(Math.floor(bounds.x), Math.floor(bounds.y), Math.ceil(bounds.width), Math.ceil(bounds.height));

        //避免在width或height为0时发生无效的BitmapData错误
        //(ArgumentError:错误#2015)
        if((bounds.width>0) && (bounds.height>0)){
            //创建一个BitmapData
            bitmapData = new BitmapData(bounds.width, bounds.height, true, 0x00000000);                
            var matrix:Matrix = new Matrix();//创建一个转换矩阵
            //翻译是否适合源的左上角
            matrix.translate(-bounds.x, -bounds.y);
            bitmapData.draw(source, matrix);//画源
            return bitmapData;//返回结果(退出点)
        }
        //如果未创建结果,则返回空的BitmapData
        return new BitmapData(1, 1, true, 0x00000000);
    }

注意:为了getBounds()返回有效值,对象必须至少具有阶段访问权限,否则该值是伪造的。可以添加代码以确保通过的source具有阶段,如果没有,可以将其添加到阶段然后再次删除。