forked from: WarpEffect - フィルター「ゆがみ」
/**
* Copyright mathieu ( http://wonderfl.net/user/mathieu )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cOjD
*/
// forked from cpu_t's WarpEffect - フィルター「ゆがみ」
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.filters.DisplacementMapFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.text.TextField;
public class WarpEffect extends Sprite
{
private const BITMAP_FILE:String="http://assets.wonderfl.net/images/related_images/a/a1/a1dd/a1dd17e877334ce7bb3253bb57f55bd0e512110c";
private var bdSrc:BitmapData;
private var bdView:BitmapData;
private var bdDisplasement:BitmapData;
private var displasementFilter:DisplacementMapFilter;
private var isMouseDown:Boolean=false;
private var lastMousePos:Point;
public function WarpEffect()
{
var tfOut:TextField=new TextField();
addChild(tfOut);
tfOut.autoSize="left";
tfOut.appendText("bitmap loading...\n");
var loader1:Loader=new Loader();
loader1.load(new URLRequest(BITMAP_FILE),new LoaderContext(true));
loader1.contentLoaderInfo.addEventListener(Event.INIT,function(e:Event):void
{
var loader2:Loader=new Loader();
loader2.loadBytes(loader1.contentLoaderInfo.bytes);
loader2.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void
{
tfOut.appendText("load complete");
removeChild(tfOut);
bdSrc=new BitmapData(loader2.width,loader2.height);
bdSrc.draw(loader2);
bdView=bdSrc.clone();
var bitmap:Bitmap=new Bitmap(bdView);
addChild(bitmap);
bdDisplasement=new BitmapData(bdSrc.width,bdSrc.height,false,0x8080);
displasementFilter=new DisplacementMapFilter(bdDisplasement,new Point(),2,4,100,100,"clamp");
lastMousePos=new Point(mouseX,mouseY);
stage.addEventListener(MouseEvent.MOUSE_DOWN,function(e:MouseEvent):void
{
isMouseDown=true;
});
stage.addEventListener(MouseEvent.MOUSE_UP,function(e:MouseEvent):void
{
isMouseDown=false;
});
stage.addEventListener(MouseEvent.MOUSE_MOVE,mmoveHandler);
//addChild(new Bitmap(bdDisplasement));
});
});
}
private function mmoveHandler(e:MouseEvent):void
{
if(isMouseDown)
{
var mat:Matrix=new Matrix();
mat.translate(mouseX,mouseY);
var mode:String;
var xdis:int=mouseX-lastMousePos.x;
var ydis:int=mouseY-lastMousePos.y;
var ct:ColorTransform=new ColorTransform();
ct.greenMultiplier=1/0xFF*Math.abs(xdis);
ct.blueMultiplier=1/0xFF*Math.abs(ydis);
var sp:Sprite=new Sprite();
sp.filters=[new BlurFilter(32,32)];
sp.graphics.lineStyle(20,0x00FF00);
sp.graphics.moveTo(0,0);
sp.graphics.lineTo(-xdis,-ydis);
if(xdis<0)mode="add";
else mode="subtract";
bdDisplasement.draw(sp,mat,ct,mode);
sp.graphics.clear();
sp.graphics.lineStyle(50,0x0000FF);
sp.graphics.moveTo(0,0);
sp.graphics.lineTo(-xdis,-ydis);
if(ydis<0)mode="add";
else mode="subtract";
bdDisplasement.draw(sp,mat,ct,mode);
bdView.applyFilter(bdSrc,bdSrc.rect,bdSrc.rect.topLeft,displasementFilter);
}
lastMousePos=new Point(mouseX,mouseY);
}
}
}