- Home
- Tutorials
- Flash
- Intermediate
- Creating a Reflection Effect with AS3

Dealing with Filters
If we apply a shadow to our target object a simple BimapData draw (bottom) gives us this:
The shadow is ignored because the size of the BitmapData matches only the size of the target object without the filter. If we make the BimapData bigger then the shadow would be drawn. Here I manually added 10 pixels in the width and height of our BitmapData:
So our only difficulty is to make sure our BitmapData is big enough to draw the target object with any filter associated. The gradient should then be as big as the BitmapData. So what we need to do is, keep the filters associated with the target object, determine how big our BitampData should be, do any matrix translation if need be, and draw the target object in our bitmap.
So let’s get started on this. First, we change the getTargetProperty() method so we don’t remove the filters from the target object. Second, we work with the getRegistration() method. This is where logically we’ll get the real size of our object. No need to run any code for this if there are no filters associated with our target object so this is of course inside a conditional:
private function getRegistration():void{
if(TargetDisplayObject.filters.length > 0){//no filters? no need to run this code then
var temp:BitmapData = new BitmapData(TargetDisplayObject.width, TargetDisplayObject.height, true, 0x00ffffff);
//create a simple bitmapdata with the size of our target object
temp.floodFill(0,0,0xFFFF0000);//fill the bitmapdata with some color
var tempRect:Rectangle = new Rectangle(0, 0, temp.width, temp.height);//make a rectangle out of our bitmapdata
filteredRect = tempRect.clone();//make another one just like the preceding
for(var i:Number = 0; i<TargetDisplayObject.filters.length; i++){//run a for loop as many times as there are filters
var adding:Rectangle = temp.generateFilterRect(tempRect, TargetDisplayObject.filters[i]);
//this is the cool method that allows us to find out the real size with filters
filteredRect = filteredRect.union(adding);//merge each new rectangle to the preceding
}
Xfilter = filteredRect.x;//If we need any registration catch up due to filters that will be stored here
Yfilter = filteredRect.y;//If we need any registration catch up due to filters that will be stored here
rect = TargetDisplayObject.getBounds(TargetDisplayObject.stage);
}
else{//no filters so run a normal code
rect = TargetDisplayObject.getBounds(TargetDisplayObject.stage);
filteredRect = rect.clone();
Xfilter = 0;
Yfilter = 0;
}
Xoffset = rect.x+Xfilter;//set the registration catch up with any filter catch up is nessecary
Yoffset = rect.y+Yfilter;
}
That’s a bit nasty I know. Basically we create a bimapdata the size of the actual target object, then we “test” all filters with this bitmapdata and its method generateFilterRect() which tests a filter. Then we generate a rectangle instance and add each generated rectangle together to form the maximum size needed. Finally, we assign the result to our new member variable filteredRect. Next we need to modify our createReflection() method a bit:
matrix.translate(-Xoffset+Xfilter, -Yoffset+Yfilter); //now we translate our matrix with also the x and y property of our filteredRect
//rectangle variable since now our filteredRect variable might have negative x and y property because of the filters.
We also need to move the final bitmap and gradient a bit:
bitmap.x = Xreg+(Xfilter/2); //divide by 2 to get a balance
bitmap.y = Yreg+(Yfilter/2);
masker.x = Xreg+(Xfilter/2);
masker.y = Yreg+(Yfilter/2);
And we are done:
All of our problems have been answered but to make this class usable we need to give the user a lot more options. Let’s see that in the next section.

