The best way to apply a filter to an image with ActionScript 3.0 is to use the applyFilter method of the BitmapData class.
In the example, I'll be using the Document Class and another class that extends the BitmapData to load and image from the gallery..then play a little with the BlurFilter on the loaded image.
I create an FLA and save it as ' blur.fla ' . I import a random image from the gallery (file>import>import to library and browse until I find the image I wish to select). I select the loaded image, click right, option linkage and in the Class field I insert the word Image (like the name of the class I'm about to create).
I create the Document Class, an AS file that I save as ' BlurMe.as '. Here is what I write:
Code:
package
{
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.filters.BlurFilter;
import flash.geom.Rectangle;
import flash.events.Event;
public class BlurMe extends MovieClip
{
private var bit:Bitmap;
private var immagine:Immagine;
private var w:Number;
private var h:Number;
public function BlurMe()
{
initListener();
}
private function initListener():void
{
addEventListener(Event.ENTER_FRAME,applicaFiltro);
}
private function applicaFiltro(e:Event):void
{
var immagine:Immagine=new Immagine();
bit=new Bitmap(immagine);
addChild(bit);
w=mouseX-bit.x;
h=mouseY-bit.y;
var filtro:BlurFilter=new BlurFilter(8,8,1);
var rettangolo:Rectangle=new Rectangle(0,0,w,h);
bit.bitmapData.applyFilter(bit.bitmapData,rettangolo,rettangolo.topLeft,filtro);
}
}
}
Now I create another class, another AS file that I save as 'Immagine.as' , like so;
Code:
package
{
import flash.display.BitmapData;
public class Immagine extends BitmapData
{
public function Immagine()
{
super(0,0);
}
}
}
This class loads an image from the FLA gallery. It's the same class used in the article:
How to load an image from a gallery with Flash CS3
Here is the result: (hover the mouse pointer over the image to see the effect)
Source files: