flash on 2012-6-23
package {
import flash.display.BlendMode;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.display.BitmapDataChannel;
import flash.filters.DisplacementMapFilterMode;
import flash.geom.Point;
import flash.filters.DisplacementMapFilter;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.display.BitmapData;
import flash.display.Sprite;
public class RippleTest extends Sprite {
private const W:int = 465;
private const H:int = 465;
private var canvas:BitmapData = new BitmapData(W, H, true, 0xFF000000);
private var bg:BitmapData = new BitmapData(W, H, true, 0xFF000000);
private var noise1:BitmapData = new BitmapData(W, H, true, 0xFF000000);
private var noise2:BitmapData = new BitmapData(W, H, true, 0xFF000000);
private var noiseCombine:BitmapData = new BitmapData(W, H, true, 0xFF000000);
private var dmf:DisplacementMapFilter = getDispFilter();
private function getDispFilter():DisplacementMapFilter {
var mapBitmap:BitmapData = noiseCombine;
var mapPoint:Point = new Point(0, 0);
var channels:uint = BitmapDataChannel.RED;
var componentX:uint = channels;
var componentY:uint = channels;
var scaleX:Number = 20;
var scaleY:Number = 20;
var mode:String = DisplacementMapFilterMode.WRAP;
var color:uint = 0;
var alpha:Number = 0;
return new DisplacementMapFilter(mapBitmap,
mapPoint,
componentX,
componentY,
scaleX,
scaleY,
mode,
color,
alpha);
}
public function RippleTest() {
drawBg();
noise1.perlinNoise(200, 200, 3, Math.random()*uint.MAX_VALUE, true, true, 7, true);
noise2.perlinNoise(200, 200, 3, Math.random()*uint.MAX_VALUE, true, true, 7, true);
addChild(new Bitmap(canvas));
addEventListener(Event.ENTER_FRAME, enterFrame);
}
private function enterFrame(e:Event):void {
scrollBitmap(noise1, 0, 2);
scrollBitmap(noise2, 2, 0);
noiseCombine.draw(noise1);
noiseCombine.draw(noise2, null, null, BlendMode.OVERLAY);
canvas.applyFilter(bg, bg.rect, new Point(0,0), dmf);
canvas.draw(noiseCombine, null, new ColorTransform(1,1,1,0.3));
}
private function scrollBitmap(bitmapData:BitmapData, scrollX:int, scrollY:int):void{
while(scrollX > bitmapData.width) scrollX -= bitmapData.width;
while(scrollX < -bitmapData.width) scrollX += bitmapData.width;
while(scrollY > bitmapData.height) scrollY -= bitmapData.height;
while(scrollY < -bitmapData.height) scrollY += bitmapData.height;
// the 4 edges of the bitmap
var xPixels:int = Math.abs(scrollX), yPixels:int = Math.abs(scrollY);
var rectR:Rectangle = new Rectangle(bitmapData.width-xPixels,0,xPixels,bitmapData.height);
var rectL:Rectangle = new Rectangle(0,0,xPixels,bitmapData.height);
var rectT:Rectangle = new Rectangle(0,0,bitmapData.width,yPixels);
var rectB:Rectangle = new Rectangle(0,bitmapData.height-yPixels,bitmapData.width,yPixels);
var pointTL:Point = new Point(0,0);
var pointR:Point = new Point(bitmapData.width-xPixels,0);
var pointB:Point = new Point(0,bitmapData.height-yPixels);
var tmp:BitmapData = new BitmapData(bitmapData.width,bitmapData.height,bitmapData.transparent,0x000000);
bitmapData.lock();
if(xPixels){
scrollX > 0 ? tmp.copyPixels(bitmapData,rectR, pointTL) : tmp.copyPixels(bitmapData,rectL, pointR);
bitmapData.scroll(scrollX,0);
scrollX > 0 ? bitmapData.copyPixels(tmp,rectL, pointTL) : bitmapData.copyPixels(tmp,rectR, pointR);
}
if(yPixels){
scrollY > 0 ? tmp.copyPixels(bitmapData,rectB, pointTL) : tmp.copyPixels(bitmapData,rectT, pointB);
bitmapData.scroll(0,scrollY);
scrollY > 0 ? bitmapData.copyPixels(tmp,rectT, pointTL) : bitmapData.copyPixels(tmp,rectB, pointB);
}
tmp.dispose();
bitmapData.unlock();
}
private function drawBg():void {
var x:int, y:int;
var size:int = 15;
var myRect:Rectangle = new Rectangle(0,0,size,size);
for(x = 0; x<W; x+=size) {
for(y = 0; y<H; y+=size) {
myRect.x = x; myRect.y = y;
bg.fillRect(myRect, ((x+y)&1) == 0 ? 0xFF000044 : 0xFF004400 );
}
}
}
}
}