508 Animation, Filters, and Drawings
To blur an image based on the mouse pointer’s position:
1. Create a new Flash document and save it as dynamicblur.fla.
2. Add the following code to Frame 1 of the Timeline:
import flash.filters.BlurFilter;
System.security.allowDomain("http://www.helpexamples.com");
var mclListener:Object = new Object();
mclListener.onLoadInit = function(target_mc:MovieClip) {
// Center the target_mc movie clip on the Stage.
target_mc._x = (Stage.width - target_mc._width) / 2;
target_mc._y = (Stage.height - target_mc._height) / 2;
};
this.createEmptyMovieClip("img_mc", 10);
var img_mcl:MovieClipLoader = new MovieClipLoader();
img_mcl.addListener(mclListener);
img_mcl.loadClip("http://www.helpexamples.com/flash/images/image1.jpg",
img_mc);
var blur:BlurFilter = new BlurFilter(10, 10, 2);
var mouseListener:Object = new Object();
mouseListener.onMouseMove = function():Void {
/* Moving the pointer to the center of the Stage sets the blurX and
blurY properties to 0%. */
blur.blurX = Math.abs(_xmouse - (Stage.width / 2)) / Stage.width * 2 *
255;
blur.blurY = Math.abs(_ymouse - (Stage.height / 2)) / Stage.height * 2
* 255;
img_mc.filters = [blur];
};
Mouse.addListener(mouseListener);
The first section of this code loads and positions a dynamically loaded image on the Stage.
The second section defines a listener that is called whenever the mouse moves. You
calculate the amount of horizontal and vertical blurring based on the mouse pointer’s
current position on the Stage. The further you move the pointer away from the center of
the Stage, the more blurring is applied to the instance.