topical media & game development
#graphic-flex-image-effects-02-Flex-DisplacementMapFilterTest.ax
#graphic-flex-image-effects-02-Flex-DisplacementMapFilterTest.ax
[swf]
[flash]
flex
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.Shape;
import flash.filters.BlurFilter;
import flash.filters.DisplacementMapFilter;
import flash.filters.DisplacementMapFilterMode;
import flash.geom.Matrix;
import flash.geom.Point;
[SWF(width=600, height=400, backgroundColor=0xEEEEEE)]
Tests application of the DisplacementMapFilter to a loaded image.
public class @ax-graphic-flex-image-effects-02-Flex-DisplacementMapFilterTest extends graphic_flex_image_effects_02_Flex_ImageFilterTest {
Constructor. Sends image path to super method.
public function @ax-graphic-flex-image-effects-02-Flex-DisplacementMapFilterTest() {
super("graphic-flex-image-effects-02-assets-checkers.jpg");
}
Draws a shape, then uses it to displace the loaded image.
override protected function applyFilter():void {
// darws a white circle
var shape:Shape = new Shape();
shape.graphics.beginFill(0xFFFFFF);
var radius:Number = (Math.min(_bitmap.width, _bitmap.height) - 150)/2;
shape.graphics.drawCircle(radius, radius, radius);
shape.graphics.endFill();
// creates a BitmapData to draw the shape into
var map:BitmapData = new BitmapData(_bitmap.width, _bitmap.height, false, 0xFF000000);
var matrix:Matrix = new Matrix();
// matrix is used to draw shape into middle of bitmap
matrix.translate((_bitmap.width - radius*2)/2, (_bitmap.height - radius*2)/2);
map.draw(shape, matrix);
// blur is applied to create more levels of gray
var blurFilter:BlurFilter = new BlurFilter(80, 80);
map.applyFilter(map, map.rect, new Point(), blurFilter);
// map is positioned and added to stage
var mapBitmap:Bitmap = new Bitmap(map);
mapBitmap.y = _bitmap.y;
_bitmap.x = mapBitmap.width;
addChild(mapBitmap);
// displacement of image using the drawn map
var filter:DisplacementMapFilter = new DisplacementMapFilter();
filter.mapBitmap = map;
filter.componentX = BitmapDataChannel.RED;
filter.componentY = BitmapDataChannel.RED;
filter.scaleX = 30;
filter.scaleY = 30;
// pixels outside range won't wrap; instead, pixels will be repeated
filter.mode = DisplacementMapFilterMode.CLAMP;
_bitmap.filters = [filter];
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.