Parallax Displacement Map
Parallax Displacement Map
move your mouse around
more info here: http://actionsnippet.com/?p=556
/**
* Copyright shapevent ( http://wonderfl.net/user/shapevent )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/zEDi
*/
package {
// Parallax Displacement Map
// move your mouse around
// more info here: http://actionsnippet.com/?p=556
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.filters.*;
[SWF(width=500, height=500, backgroundColor=0xCCCCCC, frameRate=30)]
public class PerlinDisplace extends MovieClip {
private var canvas:BitmapData;
private var radial:Shape;
private var m:Matrix;
private var displace:BitmapData;
private var displacementMap:DisplacementMapFilter;
private var scale:Number;
public function PerlinDisplace(){
// init
canvas = new BitmapData(500, 500, true, 0xFF000000);
addChild(new Bitmap(canvas));
// create a radial gradient
radial = new Shape();
m = new Matrix()
m.createGradientBox(500,500,0,0,0);
radial.graphics.beginGradientFill(GradientType.RADIAL, [0xFFFFFF, 0x000000], [1, 1], [0, 200], m, SpreadMethod.PAD);
radial.graphics.drawRect(0,0,500,500);
radial.x = radial.y = 0;
displace = new BitmapData(500, 500, false,0xFF000000);
displace.perlinNoise(150,150, 3, 30, true, false,0,true);
// try different blendmodes here
displace.draw(radial, null ,null, BlendMode.LIGHTEN);
displacementMap = new DisplacementMapFilter(displace, new Point(0,0), 1, 1, 0, 0, DisplacementMapFilterMode.WRAP);
scale = 50 / stage.stageWidth;
addEventListener(Event.ENTER_FRAME, onLoop);
}
// private methods
private function onLoop(evt:Event):void {
canvas.copyPixels(displace, canvas.rect, new Point(0,0));
displacementMap.scaleX = 25 - mouseX * scale ;
displacementMap.scaleY = 25 - mouseY * scale ;
canvas.applyFilter(canvas, canvas.rect, new Point(0,0), displacementMap);
}
}
}