You are viewing the archives of the original Pixelfumes blog. This blog is no longer maintained. All of the content available on this site is available on the new Pixelfumes blog in addition to all of our new posts! Visit: http://blog.pixelfumes.com.

« Home | DHTML Clone of Flash 8 BitmapData Image Reflection... » | Flash 8 BitmapData Image Reflection Example » | PixelStyles - Share, Edit, Apply Dynamic Styles fo... » | Photoshop-like "Styles" in Flash 8 » | Flash 8 - Dragging is Good » | Flash 8 Filter Gotcha on ComboBox » | The Google Talk Bandwagon - I Jumped On » | I'm Famous - In my first podcast » | OT: Google Earth - Katrina » | iTunes 5 - Get It » 

Monday, September 26, 2005 

My Bitmap Reflection Class for Download

I took the time to write my "Reflection" class into a true AS2 class so that I could share it and use it easily when I needed to. The following AS2 code should be saved into an .as file called Reflect. You can then use the class by instantiating the class like:

reflect = new Reflect(my_mc,50,255);

The arguments are the (movieClip,the _alpha of the reflection, and the ration of the fade 0-255)

Here is the class (watch for line wraps that may happen in your web browser):

import flash.display.BitmapData;

class Reflect{
//Ben Pritchard 2005
//Pixelfumes.com
private var mcBMP:BitmapData;
private var reflectionBMP:BitmapData;

function Reflect(mc:MovieClip,alpha:Number,ratio:Number){
//create a bmp obj out of it
mcBMP = new BitmapData(mc._width, mc._height, false, 0x00FFFFFF);
mcBMP.draw(mc);
mc.createEmptyMovieClip("origItem_mc",mc.getNextHighestDepth());
mc.origItem_mc.attachBitmap(mcBMP, 1);

reflectionBMP = new BitmapData(mc._width, mc._height, false, 0x00FFFFFF);
reflectionBMP.draw(mc);
mc.createEmptyMovieClip("reflection_mc",mc.getNextHighestDepth());
mc.reflection_mc.attachBitmap(mcBMP, 1);
mc.reflection_mc._yscale = -100;
mc.reflection_mc._y = mc._height;


//create the gradient mask
mc.createEmptyMovieClip("gradientMask_mc", mc.getNextHighestDepth());
var fillType:String = "linear";
var colors:Array = [0xFFFFFF, 0xFFFFFF];
var alphas:Array = [alpha, 0];
var ratios:Array = [0, ratio];
var matrix = {matrixType:"box", x:0, y:0, w:mc._width, h:mc._height/4, r:(90/180)*Math.PI};
var spreadMethod:String = "pad";

mc.gradientMask_mc.beginGradientFill(fillType, colors, alphas, ratios, matrix, spreadMethod);
mc.gradientMask_mc.moveTo(0, 0);
mc.gradientMask_mc.lineTo(0, mc._height/2);
mc.gradientMask_mc.lineTo(mc._width, mc._height/2);
mc.gradientMask_mc.lineTo(mc._width, 0);
mc.gradientMask_mc.lineTo(0, 0);
mc.gradientMask_mc.endFill();
mc.gradientMask_mc._y = mc._height/2;
mc.reflection_mc.cacheAsBitmap = true;
mc.gradientMask_mc.cacheAsBitmap = true;
mc.reflection_mc.setMask(mc.gradientMask_mc);
}
}

The class does not support embedded FLVs or anything nutty (in-clip animation) though the wrapping clip can be animated a bit. This is because it is being cached as a Bitmap and is a static "snapshot" of the clip. It is handy for image galleries, etc. Hope you like. Let me know. I love feedback.

sweet - was right about to make something like this myself - tx

Does this work in MX 2004? I can't seem to get gradient masks with alpha fading to work.

No - MX 2004 doesn't support gradient masks or the BitmapData object used in this demo.

This effect is awesome. Thank you!!

hi ben,

great work - just what I was looking for. I've edited the class a little to accomidate an offset parameter and transparency which I hope may be useful to some... (there's also a bit of code I needed to comment out - let me know if there was a use for it?)

[CODE]
import flash.display.BitmapData;

class Reflect
{
//
//Pixelfumes.com
private var mcBMP:BitmapData;
private var reflectionBMP:BitmapData;

function Reflect(mc:MovieClip,alpha:Number,ratio:Number, offset:Number, transparent:Boolean)
{
if (transparent == undefined) transparent = false;
//create a bmp obj out of it
mcBMP = new BitmapData(mc._width, mc._height, transparent, 0x00FFFFFF);
mcBMP.draw(mc);

/* commented out by paddy
mc.createEmptyMovieClip("origItem_mc",mc.getNextHighestDepth());
mc.origItem_mc.attachBitmap(mcBMP, 1);
*/

reflectionBMP = new BitmapData(mc._width, mc._height, false, 0x00FFFFFF);
reflectionBMP.draw(mc);
mc.createEmptyMovieClip("reflection_mc",mc.getNextHighestDepth());
mc.reflection_mc.attachBitmap(mcBMP, 1);
mc.reflection_mc._yscale = -100;
mc.reflection_mc._y = mc._height + offset;

//create the gradient mask
mc.createEmptyMovieClip("gradientMask_mc", mc.getNextHighestDepth());
var fillType:String = "linear";
var colors:Array = [0xFFFFFF, 0xFFFFFF];
var alphas:Array = [alpha, 0];
var ratios:Array = [0, ratio];
var matrix = {matrixType:"box", x:0, y:0, w:mc._width, h:mc._height/4, r:(90/180)*Math.PI};
var spreadMethod:String = "pad";

mc.gradientMask_mc.beginGradientFill(fillType, colors, alphas, ratios, matrix, spreadMethod);
mc.gradientMask_mc.moveTo(0, 0);
mc.gradientMask_mc.lineTo(0, mc._height/2);
mc.gradientMask_mc.lineTo(mc._width, mc._height/2);
mc.gradientMask_mc.lineTo(mc._width, 0);
mc.gradientMask_mc.lineTo(0, 0);
mc.gradientMask_mc.endFill();
mc.gradientMask_mc._y = mc._height/2;
mc.reflection_mc.cacheAsBitmap = true;
mc.gradientMask_mc.cacheAsBitmap = true;
mc.reflection_mc.setMask(mc.gradientMask_mc);
}
}
[/CODE]

Post a Comment