从包含透明区域和效果区域的displayObject获取BitmapData

发布于 2024-11-28 04:50:54 字数 486 浏览 1 评论 0原文

我有这个功能:

    public static function cloneDpObj(target:DisplayObject):Bitmap
    {
        var duplicate:Bitmap;

        var tBitData:BitmapData = new BitmapData(target.width, target.height);
        tBitData.draw(target);
        duplicate = new Bitmap(tBitData);

        return duplicate;
    }

克隆目标显示对象(MovieClip 或 Sprite)并返回位图对象。

它可以从目标对象获取位图,但似乎无法获取图像的所有区域。

通过给出目标对象的宽度和高度,但是设计中的目标对象是通过发光效果应用的,所以我的问题是我们可以从显示对象中获取位图数据的所有视图吗?

I have this function:

    public static function cloneDpObj(target:DisplayObject):Bitmap
    {
        var duplicate:Bitmap;

        var tBitData:BitmapData = new BitmapData(target.width, target.height);
        tBitData.draw(target);
        duplicate = new Bitmap(tBitData);

        return duplicate;
    }

to clone target displayObject (MovieClip or Sprite) and return Bitmap Object.

It can get bitmap from the target object, but it seem don't get all the area of the image.

By give the width and height of target object, but the target object in design was applied by Glow Effect, so my question can we get the all view of bitmapdata from a displayobject?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

信愁 2024-12-05 04:50:54

BitmapData.draw() 拍摄给定对象的快照,删除舞台上应用的所有转换和滤镜。生成的图像显示了电影库中存在的对象。

使用变换和/或过滤器绘制显示对象时有两个基本选项。

  1. 您可以在绘制期间使用 BitmapData.draw() 的矩阵参数应用所有转换。绘制后,您可以使用 BitmapData.applyFilter() 将滤镜应用到生成的位图。
  2. 只绘制父容器,而不是对象本身。

我通常选择后者。这非常简单。有一些缺点:如果您选择第二种方法,您的目标必须有一个显示列表父级,并且您可能会绘制驻留在父级容器中的不需要的内容。 (但是,这些缺点很容易消除。)

// bounds and size of parent in its own coordinate space
var rect:Rectangle = target.parent.getBounds(target.parent);
var bmp:BitmapData = new BitmapData(rect.width, rect.height, true, 0);

// offset for drawing
var matrix:Matrix = new Matrix();
matrix.translate(-rect.x, -rect.y);

// Note: we are drawing parent object, not target itself: 
// this allows to save all transformations and filters of target
bmp.draw(target.parent, matrix);

BitmapData.draw() takes a snapshot of a given object removing all transformations and filters applied on the stage. Resulting image shows object as it is present in your movie library.

There are two basic options when drawing display objects with transformations and/or filters.

  1. You can apply all transformations during drawing with matrix parameter for BitmapData.draw(). After drawing you can apply filters to resulting bitmap with BitmapData.applyFilter().
  2. Just draw parent container, not the object itself.

I usually choose the latter. That's pretty straightforward. There are some disadvantages: if you choose the second method, your target has to have a display list parent and you may draw unwanted content that resides in parent container. (However, these drawbacks are easily eliminated.)

// bounds and size of parent in its own coordinate space
var rect:Rectangle = target.parent.getBounds(target.parent);
var bmp:BitmapData = new BitmapData(rect.width, rect.height, true, 0);

// offset for drawing
var matrix:Matrix = new Matrix();
matrix.translate(-rect.x, -rect.y);

// Note: we are drawing parent object, not target itself: 
// this allows to save all transformations and filters of target
bmp.draw(target.parent, matrix);
电影里的梦 2024-12-05 04:50:54

您需要计算 DisplayObject 的面积/矩形,包括应用的过滤器占用的面积。幸运的是,您可以通过使用 BitmapData 类的generateFilterRect() 方法。

此外,由于其他原因,如果您需要将 DisplayObject 的转换应用于 BitmapData 快照,您可以传递源 DisplayObject 的 .transform。concatenatedMatrix 作为BitmapData的draw()方法的第二个参数。

You need compute the area/rectangle of your DisplayObject including the area taken by the filter applied. Luckily you can do that with with built-in functionality by using the generateFilterRect() method of the BitmapData class.

Also, for other reasons, if you need the transformation of your DisplayObject applied to the BitmapData snapshot, you can pass the source DisplayObject's .transform.concatenatedMatrix as the second parameter of BitmapData's draw() method.

御弟哥哥 2024-12-05 04:50:54

非常感谢大家抽出宝贵的时间回答我的问题。我改进了该功能,但我更好了,但我注意到捕获结果的宽度是 1 像素偏移,所以我决定将 1 像素添加到位图数据的宽度,我知道这不是一个好的做法。因为我现在必须这样做,所以我还不知道这个问题。这是我们现在的功能:

 public static function cloneDpObj(target:DisplayObject, optWidth:Number = -1, optHeight:Number = -1):Bitmap
    {
        var duplicate:Bitmap;

        if (!target.parent) {
            var tempSprite:Sprite = new Sprite;
            tempSprite.addChild(target);
        }

        var rect:Rectangle = target.parent.getBounds(target.parent);
        var bmp:BitmapData = new BitmapData(rect.width + 1, rect.height, true, 0);

        // offset for drawing
        var matrix:Matrix = new Matrix();
        matrix.translate( -rect.x, -rect.y);

        // Note: we are drawing parent object, not target itself: 
        // this allows to save all transformations and filters of target
        bmp.draw(target.parent, matrix);

        duplicate = new Bitmap(bmp);

        return duplicate;
    } 

Thank you very much to all of you that take valuable time answer my question. I improved that function, but I is better, but I notice that the width of result of capture is 1pixel offset, so I decided to add 1 pixel to width of the bitmapdata, I know that is not a good practice. because I have to do that now, I don't know the issue yet. Here is how our function now:

 public static function cloneDpObj(target:DisplayObject, optWidth:Number = -1, optHeight:Number = -1):Bitmap
    {
        var duplicate:Bitmap;

        if (!target.parent) {
            var tempSprite:Sprite = new Sprite;
            tempSprite.addChild(target);
        }

        var rect:Rectangle = target.parent.getBounds(target.parent);
        var bmp:BitmapData = new BitmapData(rect.width + 1, rect.height, true, 0);

        // offset for drawing
        var matrix:Matrix = new Matrix();
        matrix.translate( -rect.x, -rect.y);

        // Note: we are drawing parent object, not target itself: 
        // this allows to save all transformations and filters of target
        bmp.draw(target.parent, matrix);

        duplicate = new Bitmap(bmp);

        return duplicate;
    } 
烙印 2024-12-05 04:50:54

我实际上会选择 Nox 的第一个选项作为更简单的方法,并且修改你的函数来做到这一点只需要额外的一行代码:

public static function cloneDpObj(target:DisplayObject):Bitmap
{
    var duplicate:Bitmap;

    var tBitData:BitmapData = new BitmapData(target.width, target.height);
    tBitData.draw(target);
    duplicate = new Bitmap(tBitData);
    //add the filters
    duplicate.filters = target.filters;

    return duplicate;
}

I would actually go with Nox's first option as the easier approach, and modifying your function to do it should only take one extra line of code:

public static function cloneDpObj(target:DisplayObject):Bitmap
{
    var duplicate:Bitmap;

    var tBitData:BitmapData = new BitmapData(target.width, target.height);
    tBitData.draw(target);
    duplicate = new Bitmap(tBitData);
    //add the filters
    duplicate.filters = target.filters;

    return duplicate;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文