开发者

Actionscript: How to remove a filter from BitmapData

开发者 https://www.devze.com 2023-03-08 22:46 出处:网络
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,

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 = [];


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);
        }

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消