Image Effect Sample 001
--------------------------------------------------
* ぼかしフェードイン・アウト
*
* author: motoki matsumoto
* blog: http://www.matzmtok.com/blog
* email: mtmotoki@gmail.com
*---------------------------------------------------
/*--------------------------------------------------
* ぼかしフェードイン・アウト
*
* author: motoki matsumoto
* blog: http://www.matzmtok.com/blog
* email: mtmotoki@gmail.com
*---------------------------------------------------*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.LoaderInfo;
import flash.filters.BlurFilter;
import flash.filters.DisplacementMapFilter;
import flash.filters.DisplacementMapFilterMode;
import flash.geom.Point;
import flash.system.LoaderContext;
import flash.text.TextField;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.utils.getQualifiedClassName;
import flash.system.Security;
[SWF(width="465", height="465", backgroundColor="0xffffff", frameRate="0")]
public class Buckle extends Sprite
{
private var output:TextField;
private var timer:Timer;
private var frameRate:int = 24;
private var t:int;
private var _center:Point;
private var noiseBitmapData:BitmapData;
private var image:Bitmap;
private var scale:Number = 1;
private var initialTime:int;
private var initialY:int;
public function Buckle()
{
Security.loadPolicyFile("http://www.matzmtok.com/wonderfl/crossdomain.xml");
_center = new Point(stage.stageWidth * 0.5, stage.stageHeight * 0.5);
initOutput();
println('Construct ' + getQualifiedClassName(this));
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
public function init():void {
stage.addEventListener(Event.RESIZE, resizeHandler);
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
l.load(new URLRequest("http://www.matzmtok.com/wonderfl/tokyo_olynpic.jpg"), new LoaderContext(true));
}
private function resizeHandler(e:Event):void
{
_center.x = stage.stageWidth * 0.5;
_center.y = stage.stageHeight * 0.5;
}
private function loadComplete(e:Event):void
{
var li:LoaderInfo = e.target as LoaderInfo;
image = li.content as Bitmap;
image.x = _center.x - image.width * 0.5;
image.y = _center.y - image.height * 0.5;
noiseBitmapData = image.bitmapData.clone();
var seed:Number = Math.random();
var offsets:Array = [new Point(0,0)];
noiseBitmapData.perlinNoise(40, 50, 1, seed, false, true, 1, false, offsets);
addChild(image);
timer = new Timer(1000 / frameRate);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
initialTime = getTimer();
initialY = image.y;
}
private function timerHandler(e:TimerEvent):void
{
var factor:Number = (Math.sin((getTimer() - initialTime)/900) + 1) * 0.5;
scale = 300 * factor;
image.alpha = 1 - factor;
var df:DisplacementMapFilter = new DisplacementMapFilter(noiseBitmapData, new Point(), 1, 1, 10, scale, DisplacementMapFilterMode.CLAMP, 0, 0);
var blurSize:Number = 40 * factor;
var bf:BlurFilter = new BlurFilter(blurSize, blurSize);
image.filters = [df, bf];
image.y = initialY + factor * 100;
}
private function addedToStage(e:Event):void
{
removeEventListener(e.type, arguments.callee);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
init();
}
public function get center():Point {
return _center;
}
////////////////
// 出力
///////////////
private function initOutput():void {
var margin:Number = 10;
addChild(output = new TextField());
output.selectable = false;
output.multiline = true;
output.width = stage.stageWidth - margin * 2;
output.height = stage.stageHeight - margin * 2;
}
public function print(... args):void {
output.appendText(args.join(", "));
}
public function println(... args):void {
output.appendText(args.join(", "));
output.appendText("\n");
}
}
}