views:

956

answers:

4

I'm trying to add a filter in AS3/Flex SDK. I can add a filter just fine to any one single object - but I want to apply the filter to everything that is a child of a certain object.

Think a pause window pops up, and everything below the pause window goes blurry.

Applying a filter to each individual object (eg: iterating through a list) doesn't work, as the filters from each object can then overlap and look pretty ugly.

Does anyone know how to go about doing this? Is there a way to apply a filter to everything?

Here's a simplified version of the code:

myCanvas.graphics.beginFill(0x00FF00,0.5);
myCanvas.graphics.drawRect(0,0,100,100);
myCanvas.addChild(new vectorImage());
myCanvas.addChild(new vectorImage2());
var blur:BlurFilter = new BlurFilter();
myCanvas.filters = [blur];

Neither the directly-drawn graphics nor the children get the blur effect applied. I've tried altering the defaults and tried other filters:

var colors:Array = [0xEDEDED, 0xCCCCCC, 0x211b28, 0x211b28, 0x211b28];
var alphas:Array = [0, 1, .35, .5, 1];
var ratios:Array = [0, 50, 100, 115, 155];     
myCanvas.filters = [new GradientGlowFilter(0, 0, colors, alphas, ratios, 50, 50, 1, 3, "full", false)];

With identical effects (that is to say: none). What DOES work, is:

var vi:MovieClip = new vectorImage();
myCanvas.addChild(vi);
vi.filters = [blur];

but produces the above mentioned problems with multiple filters not aligning properly.

A: 

if i understand you question right, applying the filters to the stage would be the most simple thing, no?

back2dos
Ah - perhaps I shouldn't have said *everything*. Just MOST things (think a pause screen - everything blurs except the pause window). Applying a filter to the stage would blur the pause window as well (and referencing stages in FlexSDK can cause problems)
Andy Moore
well, for that case, the two others pointed out the right approach ... put whatever you need to apply the filter to into containers ...
back2dos
+1  A: 

//in timeline code or Document class

var blur:BitmapFilter = new BlurFilter(5, 5, BitmapFilterQuality.HIGH); var allFilters:Array = new Array(); allFilters.push(filter); filters = allFilers;

grumblebee
Sorry if it wasn't clear - this is in FlexSDK, no access to timeline or document classes.
Andy Moore
+1  A: 

You should be able to just apply your blur to the container. The filters property is an array though. Try something like this:

var blur:BlurFilter = new BlurFilter();
myCanvas.filters = [blur];
krichard
Doesn't seem to do the trick - updated question with code example with no results. :/ I was really hoping it was this easy originally!
Andy Moore
+2  A: 

As I see it, you have two possible approaches:

  • put everthing you want to blur in a separate container as the rest and apply the filters to that container.

  • create a BitmapData of the size of the stage (or the part you want to blur), draw the stage, applyFilter, add it to the the display list (on top of everything) and on top of that add all sprites that shouldn't be blurred (I imagine an alert box or similar). Note that you should update the BitmapData on resize.

UPDATE: Your code seems correct, but I'm not familiar with Flex, so maybe you can't add filters to Canvas (is it a DisplayObject?)... maybe setting his cacheAsBitmap to false works (it's quite buggy sometimes)... anyway, something like this should do the trick:

var container=new Sprite();
myCanvas.addChild(container);
container.addChild(new vectorImage());
container.addChild(new vectorImage2());
container.filters=[blur];

or maybe Canvas has a container property already...

Cheers...

Cay
Yeah, myCanvas is a new Sprite(); with nothing special added to it - essentially identical as your "container" object. :(
Andy Moore
I'm out of ideas... I think it's either a bug, or somewhere else your application is removing myCanvas' filters... maybe a tween engine applying some other filters (TweenLite(myCanvas,time,{colorMatrixFilter... ), or something like that :S
Cay
Oh, and if you are positioning some object within myCanvas to some far position (a very common AS1 practice), filters won't work with such big objects... check if the height or width of myCanvas are not too big (check this post http://stackoverflow.com/questions/1019360/maximum-size-of-a-sprite-in-as3 )
Cay
That's the problem. I have a large canvas I'm scrolling around, it's 3200x900 and exceeds the bounds. I tried blurring a smaller object and it worked fine! thanks.
Andy Moore