If the target object is scaled we might start to see a loss in the quality of the reflection. This is because we draw the target at its normal size so when it’s scaled, as any bitmap would do, there is a loss of quality. So we should answer this by giving the user of the class the possibility to set a quality. Finally we should also be able to set if we need the reflection to be redrawn or not. So let’s define 3 new constants for the quality:

public static const NORMAL:String = "notmal";
public static const AUTO:String = "auto";
public static const HIGH:String = "high";

The normal mode is how the class is working so far. The auto mode will set the quality according to the actual target object scale factor. The high mode will set the quality to the highest possible resolution. The BitmapData we create is simply made of pixels. To raise its resolution we just need to increase the amount of pixels. Once we have done that we can simply resize the bitmap holding our BitmapData to the target object size. We of course create setter and getter for the quality and you already know how we do that so let’s move on directly to the two new methods we create. All we really need to do is multiply some key values by a scale factor. Obviously in normal mode this value will be 1 as in 10*1 = 10, meaning no change. In auto mode we multiply by the actual target object scale factor but there can be a different scale factor for the width and height so we need to get the highest value and we also need to make sure we don’t have a scale factor that will make our BitmapData exceed the maximum size allowed in Flash Player 9 which is 2880 pixels (note that Flash Player 10 allows for bigger values). So here is the method that calculates this:

private function getAutoQualityFactor():Number{
var factor:Number = Math.max(SCALEX, SCALEY); //get the biggest value between the two target object scale factors
var isTooBig:Boolean = true;//start assuming the scale is too big
while(isTooBig){ //while the scale factor is too big
if(TargetDisplayObject.width*factor < 2880 && TargetDisplayObject.height*factor < 2880){
isTooBig = false;//if we are under 2880 then break the while loop

}else{factor--; }//else decrease the scale factor
}
return factor;
}

Now to get the highest factor possible:

private function getBestQualityFactor():Number{
var factor:Number = Math.max(2880/TargetDisplayObject.width, 2880/TargetDisplayObject.height);
//we divide the maximum number of pixel allowed by our actual width and height and get the highest of these two values
var isTooBig:Boolean = true;//let's assume it's too big to start with
while(isTooBig){
if(TargetDisplayObject.width*factor < 2880 && TargetDisplayObject.height*factor < 2880){
isTooBig = false;//if we are good then break the while loop
} else{factor--; }//else decrease the scale factor
}
return factor;
}

Now let’s add our scale factor value to our code. We start with some if else statements:

if(currentQuality == "normal"){
qualityScaleFactor = 1;
}
else if(currentQuality == "auto"){

qualityScaleFactor = getAutoQualityFactor();
}
else if(currentQuality == "high"){
qualityScaleFactor = getBestQualityFactor();
}

qualityScaleFactor is the member variable we’ll use. Here depending on the quality option choosen we set this variable to the right value.

var temp:BitmapData = new BitmapData(rect.width*qualityScaleFactor, rect.height*qualityScaleFactor, true, 0x0000FF00);

Create a big enough BitmapData. Here we multiply the rect width and height by the scale factor.

matrix.translate((-Xoffset+Xfilter)*qualityScaleFactor, (-Yoffset+Yfilter)*qualityScaleFactor);
matrix.scale(qualityScaleFactor, qualityScaleFactor);

Our matrix must catch up with the new size. Here we move the matrix according to the new size. We also of course scale the matrix accordingly.

mheight = -filteredRect.height*qualityScaleFactor;

Depending on the direction chosen we also catch up the offset by multiplying by the scale factor.

matrix = new Matrix();
matrix.translate(mwidth,mheight);
matrix.scale(mX,mY);
bmd = new BitmapData(temp.width, temp.height, true, 0x00FF0000);
bmd.draw(temp, matrix, null, null, null, true);

Here we draw our final BitmapData according to the scaled first BitmapData.

bitmap.width = rect.width;
bitmap.height = rect.height;

And finally we reset the size by giving our bitmap the right dimensions. Now if we scale our target object and set our quality to normal we get this:


Do you see the pixilated circle? Our BitmapData is about 100×100 pixels here. Now let’s set the quality to auto:


That looks way better. Here our BitmapData is about 300×300 pixels. Finally let’s try to set to high:


Very nice too. Here our BitmapData is about 2800×2800 pixels which is big! Obviously we’ll want to keep the high quality for a particular case. The auto quality will be mostly the one we want if we are going to scale the target object a lot. In all other cases the normal mode will do just fine.