A SERVICE OF

logo

Macromedia MAX 2005 - Anaheim, CA What’s New In Flash 8
115
Using the Displacement Map Filter
The DisplacementMapFilter class uses the pixel values from the specified BitmapData object (called
the displacement map image) to perform a displacement of an instance that's on the Stage, such as
a movie clip instance or a bitmap data instance. You can use this filter to achieve a warped or
mottled effect on a specified instance.
This filter is only available by using ActionScript.
The following procedure loads a JPEG image and applies a displacement map filter to it, which
causes the image to look distorted. Whenever the user moves the mouse, the displacement map is
regenerated.
To distort an image with the displacement map filter:
1. Create a new Flash document and save it as displacement.fla.
2. Add the following ActionScript to Frame 1 of the Timeline:
import flash.filters.DisplacementMapFilter;
import flash.geom.Point;
import flash.display.BitmapData;
var perlinBmp:BitmapData;
var displacementMap:DisplacementMapFilter;
var mclListener:Object = new Object();
mclListener.onLoadInit = function(target_mc:MovieClip):Void {
target_mc._x = (Stage.width - target_mc._width) / 2;
target_mc._y = (Stage.height - target_mc._height) / 2;
perlinBmp = new BitmapData(target_mc._width,
target_mc._height);
perlinBmp.perlinNoise(target_mc._width, target_mc._height,
10, Math.round(Math.random() * 100000), false, true, 1, false);
displacementMap = new DisplacementMapFilter(perlinBmp, new
Point(0, 0), 1, 1, 100, 100, "color");
shapeClip.filters = [displacementMap];
};
var shapeClip:MovieClip = this.createEmptyMovieClip("shapeClip",
1);
shapeClip.createEmptyMovieClip("holderClip", 1);
var imageLoader:MovieClipLoader = new MovieClipLoader();
imageLoader.addListener(mclListener);
imageLoader.loadClip("http://www.helpexamples.com/flash/images/im
age1.jpg", shapeClip.holderClip);
var mouseListener:Object = new Object();
mouseListener.onMouseMove = function():Void {
perlinBmp.perlinNoise(shapeClip._width, shapeClip._height,
10, Math.round(Math.random() * 100000), false, true, 1, false);