Working with filters using ActionScript 529
This filter is only available by using ActionScript. For more information on this filter, see
DisplacementMapFilter (flash.filters.DisplacementMapFilter) in the ActionScript 2.0
Language Reference.
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/
image1.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);
shapeClip.filters = [displacementMap];
};
Mouse.addListener(mouseListener);
This code loads a JPEG image and places it on the Stage. After the image is completely
loaded, this code creates a BitmapData instance and uses the
perlinNoise() method to
fill it with randomly placed pixels. The BitmapData instance passes to the displacement
map filter, which is applied to the image and causes the image to look distorted.