Let’s create a method that creates the gradient. We will then position our masker object the same way than our bitmap object:

masker.x = Xreg;//masker is our mask!
masker.y = Yreg;

Now let’s create our method:

private function createGradient():void{
var W:Number = rect.width;
var H:Number = rect.height;
var myMatrix:Matrix = new Matrix();

myMatrix.createGradientBox(W, H, gradientAngle, 0, 0);//create our gradient box
masker.graphics.clear();//make sure we clear what we drew earlier
masker.graphics.beginGradientFill(GradientType.LINEAR, _colors, _alphas, _ratios, myMatrix);
masker.graphics.drawRect(0, 0, W, H);//and draw our gradient

And here is the result with a left mode reflection:

Now if we rotate a bit:

Not good. The gradient does not follow the angle. We can fix that by taking into account the actual angle of the target object:

gradientAngle = gradientAngle+(angle*Math.PI/180);//angle is the angle of the target object
//here we translate degrees in radians

And the result is perfect, now click on the stage to animate this a little and see the gradient is always in line with the target object:

We are left with the filter problem. We want to be able to draw any filters that are associated with the target object but the difficulty here is that filters can alter the size of the target object without us being directly able to get this new size. Let’s see if we can find a solution to this problem in the next section.