forked from: Image Effect Sample 001
--------------------------------------------------
* ぼかしフェードイン・アウト
*
* author: motoki matsumoto
* blog: http://www.matzmtok.com/blog
* email: mtmotoki@gmail.com
*---------------------------------------------------
// forked from mtok's Image Effect Sample 001
/*--------------------------------------------------
* ぼかしフェードイン・アウト
*
* 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="30")]
public class Buckle extends Sprite
{
// 定数
private const STOP_SEC:Number = 3; // 静止時間(秒)
private const FRAME_RATE:int = 30; // フレームレート
private const THRESHOLD:Number = 0.001; // 閾値
// Timer
private var fadeInOutTimer:Timer;
private var stopTimer:Timer;
private var output:TextField;
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;
private var stopFlag:Boolean = false;
public function Buckle()
{
Security.loadPolicyFile("http://www.matzmtok.com/wonderfl/crossdomain.xml");
_center = new Point(stage.stageWidth * 0.5, stage.stageHeight * 0.5);
initOutput();
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, onLoadHandler);
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 onLoadHandler(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);
image.visible = false;
addChild(image);
fadeInOutTimer = new Timer(1000 / FRAME_RATE);
fadeInOutTimer.addEventListener(TimerEvent.TIMER, timerInitStartHandler);
fadeInOutTimer.start();
initialTime = getTimer();
initialY = image.y;
}
private function timerInitStartHandler(e:TimerEvent):void
{
var factor:Number = (Math.cos((getTimer() - initialTime)/900) + 1) * 0.4;
if(!stopFlag && factor < THRESHOLD){
fadeInOutTimer.stop();
stopFlag = true;
stopTimer = new Timer(STOP_SEC * 1000);
stopTimer.addEventListener(TimerEvent.TIMER, stopTimerHandler);
stopTimer.start();
}
if(!stopFlag){
scale = 500 * 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;
}
image.visible = true;
}
private function stopTimerHandler(e:TimerEvent):void {
stopFlag = false;
stopTimer.stop();
fadeInOutTimer= new Timer(1000 / FRAME_RATE);
fadeInOutTimer.addEventListener(TimerEvent.TIMER, timerFadeInOutHandler);
fadeInOutTimer.start();
initialTime = getTimer();
}
private function timerFadeInOutHandler(e:TimerEvent):void {
var factor:Number = (Math.sin((getTimer() - initialTime)/900) + 1) * 0.5;
if(!stopFlag && factor < THRESHOLD){
fadeInOutTimer.stop();
stopFlag = true;
stopTimer = new Timer(STOP_SEC * 1000);
stopTimer.addEventListener(TimerEvent.TIMER, stopTimerHandler);
stopTimer.start();
}
if(!stopFlag){
scale = 500 * 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 = 0;
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");
}
}
}