Actionscript:如何从 BitmapData 中删除过滤器

发布于 2024-11-10 01:55:23 字数 932 浏览 8 评论 0原文

我使用以下代码在图像上添加了黑白滤镜:

var n:Number = 1/3;
var matrix:Array = [n,n,n,0,0,
                    n,n,n,0,0,
                    n,n,n,0,0, 
                    0,0,0,1,0];
var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix);

_bitmap.bitmapData.applyFilter(_buffer, _sourceRect, new Point(), cmf);

我现在希望能够删除此滤镜,但我似乎不知道如何删除。我读过,如果我清除 _bitmap.filters 数组,它应该被删除,但是当我检查时,该数组是空的。

有人对我如何做到这一点有任何建议吗?

编辑 我正在使用 FlashPunk 游戏引擎,并且正在操作 Image.as 类中的 bitmapData。我的所有代码都是使用 FlashDevelop 编写和编译的。

编辑

我无法将过滤器直接应用到位图上,因为 flashpunk flashpunk 引擎中的图像类正在使用 bitmapData.CopyPixels() 函数绘制位图。该过滤器未应用于 bitmapData,因此未绘制。

我已更改渲染方法以使用 bitmapData.draw() 函数,该函数使用实际位图来绘制图像。

我现在可以通过执行以下操作将过滤器添加到我的位图:

_bitmap.filters = [ColorMatrixFilter];

然后我可以通过执行以下操作删除过滤器:

_bitmap.filter = [];

I've added a black and white filter onto an image using the following code:

var n:Number = 1/3;
var matrix:Array = [n,n,n,0,0,
                    n,n,n,0,0,
                    n,n,n,0,0, 
                    0,0,0,1,0];
var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix);

_bitmap.bitmapData.applyFilter(_buffer, _sourceRect, new Point(), cmf);

I now want to be able to remove this filter but I can't seem to figure out how. I've read that if I clear the _bitmap.filters array it should be removed, but when I check, this array is empty.

Does anyone have any suggestion about how I might do this?

Edit
I'm using the FlashPunk game engine and I'm manipulating the bitmapData from the Image.as class. All my code is written and compiled using FlashDevelop.

EDIT

I was not able to apply a filter directly onto the bitmap due to the fact that the image class in the flashpunk flashpunk engine was drawing the bitmap using the bitmapData.CopyPixels() function. The filter was not applied to the bitmapData and was therefore not being drawn.

I've changed the render method to use the bitmapData.draw() function which uses the actual bitmap to draw the image.

I can now add a filter to my bitmap by doing the following:

_bitmap.filters = [ColorMatrixFilter];

I can then remove my filters by doing the following:

_bitmap.filter = [];

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

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

发布评论

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

评论(1

眼睛会笑 2024-11-17 01:55:23

当您将过滤器直接应用于位图数据时,您无法删除它,因为过滤器最终会修改像素颜色,但是,您可以尝试通过执行反向操作来恢复它:

var matrix:Array = [1/n,1/n,1/n,0,0,
                    1/n,1/n,1/n,0,0,
                    1/n,1/n,1/n,0,0, 
                    0,0,0,1,0];

如果您希望能够动态删除过滤器,则必须将它们应用到位图对象而不是位图数据对象。
然后您可以执行 _bitmap.filters = []; 删除所有过滤器

这里有一个示例,显示如何在 4 秒后删除过滤器:

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.filters.ColorMatrixFilter;
    import flash.utils.setTimeout;

    public class TestTextfield extends Sprite
    {
        public function TestTextfield()
        {
            var bd1 : BitmapData = new BitmapData(300,300);
            var randomNum:Number = Math.floor(Math.random() * 10);
            bd1.perlinNoise(100, 80, 6, randomNum, false, true, 1, true, null);

            var n:Number = 1/3;
            var matrix:Array = [n,n,n,0,0,
                                n,1,n,0,0,
                                n,1,n,0,0, 
                                0,0,0,1,0];
            var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix);

            var bitmap : Bitmap = new Bitmap(bd1);

            bitmap.filters = [cmf];

            addChild(bitmap);

            setTimeout(function():void{
                bitmap.filters = [];
            }, 4000);
        }

    }
}

When you apply Filter directly to a bitmapdata, you cannot remove it because filter modify pixel color definitively, however, you can try to revert it by doing reverse operation :

var matrix:Array = [1/n,1/n,1/n,0,0,
                    1/n,1/n,1/n,0,0,
                    1/n,1/n,1/n,0,0, 
                    0,0,0,1,0];

If you want to be able to remove dynamicly filters, you have to apply them on Bitmap Object and not BitmapData Object.
Then you can do _bitmap.filters = []; to remove all filters

Here an example showing how to remove filter after 4 seconds :

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.filters.ColorMatrixFilter;
    import flash.utils.setTimeout;

    public class TestTextfield extends Sprite
    {
        public function TestTextfield()
        {
            var bd1 : BitmapData = new BitmapData(300,300);
            var randomNum:Number = Math.floor(Math.random() * 10);
            bd1.perlinNoise(100, 80, 6, randomNum, false, true, 1, true, null);

            var n:Number = 1/3;
            var matrix:Array = [n,n,n,0,0,
                                n,1,n,0,0,
                                n,1,n,0,0, 
                                0,0,0,1,0];
            var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix);

            var bitmap : Bitmap = new Bitmap(bd1);

            bitmap.filters = [cmf];

            addChild(bitmap);

            setTimeout(function():void{
                bitmap.filters = [];
            }, 4000);
        }

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