TransitGenie
////////////////////////////////////////////////////////////////////////////////
// TransitGenie
//
// [AS3.0] TransitLoaderクラスだ! (1)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1022
// [AS3.0] drawTriangles()メソッド! (4)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1615
////////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/sY2i
*/
////////////////////////////////////////////////////////////////////////////////
// TransitGenie
//
// [AS3.0] TransitLoaderクラスだ! (1)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1022
// [AS3.0] drawTriangles()メソッド! (4)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1615
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
[SWF(backgroundColor="#000000", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private static var basePath:String = "http://assets.wonderfl.net/images/related_images/";
private var photos:Array;
private var transit:TransitLoader;
public function Main() {
init();
}
private function init():void {
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
//
photos = new Array();
photos.push(basePath + "5/59/5947/59475c25ec077563a6f075c89f332f36ddffc305");
photos.push(basePath + "c/c3/c331/c331d0a4aefdac4c38ea1e4f652aad201cf793ca");
photos.push(basePath + "c/cc/ccff/ccff5ef8a381810a246b9dd4e8fbdad2e999a347");
photos.push(basePath + "e/ed/edda/eddac30fdbf622bc82c268d2399382f68da653fd");
photos.push(basePath + "c/c5/c5a8/c5a8f6fa9cf0edf0fc81cd1dd01590436b0ef41c");
photos.push(basePath + "b/b7/b788/b78801872e5b439ea3f8f2dceaebd441eee0ca6f");
//var fade:Fade = new Fade();
//transit = new TransitLoader(fade);
//
var canvas:Shape = new Shape();
addChild(canvas);
canvas.x = 72;
canvas.y = 112;
var genie:GenieEffect = new GenieEffect(canvas.graphics, 320, 240, 32, 24);
transit = new TransitLoader(genie);
addChild(transit);
transit.x = 72;
transit.y = 112;
transit.dataProvider = photos;
}
}
}
//////////////////////////////////////////////////
// TransitLoaderクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
class TransitLoader extends Sprite {
private var loaders:Array;
private var photos:Array;
private var max:uint;
private var hideID:uint = 0;
private var showID:uint = 0;
private var transit:*;
public static const COMPLETE:String = "transitComplete";
public function TransitLoader(transition:*) {
transit = transition;
}
public function set dataProvider(list:Array):void {
photos = list;
max = photos.length;
initialize();
}
private function initialize():void {
loaders = new Array();
for (var n:uint = 0; n < max; n++) {
var loader:ImageLoader = new ImageLoader();
loaders.push(loader);
loader.addEventListener(ImageLoader.INIT, init, false, 0, true);
loader.addEventListener(ImageLoader.COMPLETE, complete, false, 0, true);
}
load(showID);
}
private function load(id:uint):void {
var loader:ImageLoader = loaders[id];
var filePath:String = photos[id];
loader.load(filePath);
}
private function init(evt:Event):void {
var hide:ImageLoader = loaders[hideID];
var show:ImageLoader = loaders[showID];
//if (hide) addChild(hide);
//addChild(show);
//transitEffect();
}
private function complete(evt:Event):void {
var hide:ImageLoader = loaders[hideID];
var show:ImageLoader = loaders[showID];
//if (hide) addChild(hide);
//addChild(show);
transitEffect();
}
private function transitEffect():void {
var show:ImageLoader = loaders[showID];
transit.effect(show);
transit.addEventListener(TransitLoader.COMPLETE, transitPhoto, false, 0, true);
}
private function transitPhoto(evt:Event):void {
evt.target.removeEventListener(TransitLoader.COMPLETE, transitPhoto);
var hide:ImageLoader = loaders[hideID];
//if (hideID != showID) removeChild(hide);
hideID = showID;
showID ++;
if (showID >= max) showID = 0;
load(showID);
}
}
//////////////////////////////////////////////////
// ImageLoaderクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.system.LoaderContext;
class ImageLoader extends Sprite {
public var id:uint;
private var loader:Loader;
private var info:LoaderInfo;
public var content:Bitmap;
private var smoothing:Boolean;
public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
public static const INIT:String = Event.INIT;
public static const COMPLETE:String = Event.COMPLETE;
public function ImageLoader() {
loader = new Loader();
info = loader.contentLoaderInfo;
}
public function load(file:String, s:Boolean = false):void {
smoothing = s;
info.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
info.addEventListener(Event.INIT, initialize, false, 0, true);
info.addEventListener(Event.COMPLETE, complete, false, 0, true);
try {
loader.load(new URLRequest(file), new LoaderContext(true));
} catch (err:Error) {
trace(err.message);
}
}
public function unload():void {
loader.unload();
}
private function progress(evt:ProgressEvent):void {
dispatchEvent(evt);
}
private function ioerror(evt:IOErrorEvent):void {
loader.unload();
dispatchEvent(new Event(ImageLoader.IO_ERROR));
}
private function httpstatus(evt:HTTPStatusEvent):void {
dispatchEvent(new Event(ImageLoader.HTTP_STATUS));
}
private function securityerror(evt:SecurityErrorEvent):void {
dispatchEvent(new Event(ImageLoader.SECURITY_ERROR));
}
private function initialize(evt:Event):void {
content = Bitmap(info.content);
if (smoothing) {
content.smoothing = true;
}
dispatchEvent(new Event(ImageLoader.INIT));
}
private function complete(evt:Event):void {
info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
info.removeEventListener(Event.INIT, initialize);
info.removeEventListener(Event.COMPLETE, complete);
//addChild(content);
dispatchEvent(new Event(ImageLoader.COMPLETE));
}
}
//////////////////////////////////////////////////
// GenieEffectクラス
//////////////////////////////////////////////////
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.display.Graphics;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.display.TriangleCulling;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.ITween;
import org.libspark.betweenas3.events.TweenEvent;
import org.libspark.betweenas3.easing.*;
class GenieEffect extends EventDispatcher {
public static var className:String = "GenieEffect";
public var transitName:String = "genie";
private var loader:ImageLoader;
public static const COMPLETE:String = "transitComplete";
private var graphics:Graphics;
private var texture:BitmapData;
private var hsegments:uint = 1;
private var vsegments:uint = 1;
private var xunit:Number = 1;
private var yunit:Number = 1;
private var width:Number = 1;
private var height:Number = 1;
private var vertexs:Array;
private var targets:Array;
private var indices:Vector.<int>;
private var uvData:Vector.<Number>;
private static var unit:Number = 0.015;
public function GenieEffect(g:Graphics, _w:uint, _h:uint, h:uint = 1, v:uint = 1) {
graphics = g;
if (h > 0) hsegments = h;
if (v > 0) vsegments = v;
xunit = 1/hsegments;
yunit = 1/vsegments;
width = _w;
height = _h;
init();
}
private function init():void {
indices = new Vector.<int>();
for (var v:uint = 0; v < vsegments; v++) {
for (var h:uint = 0; h < hsegments; h++) {
var id0:Number = v*(hsegments + 1) + h;
var id1:Number = v*(hsegments + 1) + h + 1;
var id2:Number = (v + 1)*(hsegments + 1) + h;
var id3:Number = (v + 1)*(hsegments + 1) + h + 1;
indices.push(id0, id1, id2, id1, id3, id2);
}
}
vertexs = new Array();
targets = new Array();
uvData = new Vector.<Number>();
for (var y:uint = 0; y < vsegments + 1; y++) {
vertexs[y] = new Array();
targets[y] = new Array();
for (var x:uint = 0; x < hsegments + 1; x++) {
vertexs[y][x] = new Point(width*xunit*x, height*yunit*y);
targets[y][x] = new Point(width*xunit*x, height*yunit*y);
uvData.push(xunit*x, yunit*y);
}
}
//draw();
}
public function effect(target:ImageLoader):void {
texture = target.content.bitmapData;
var px:Number = Math.random()*400 - 40;
var py:Number = Math.random()*320 - 40;
show(px, py);
}
private function show(cx:Number, cy:Number):void {
var px:Number = hsegments*(cx/width);
var py:Number = vsegments*(cy/height);
var max:Number = 0;
for (var v:uint = 0; v < vsegments + 1; v+=vsegments) {
for (var h:uint = 0; h < hsegments + 1; h+=hsegments) {
var distance:Number = Math.sqrt((h - px)*(h - px) + (v - py)*(v - py));
max = Math.max(max, distance);
}
}
var tweens:Array = new Array();
for (var y:uint = 0; y < vsegments + 1; y++) {
for (var x:uint = 0; x < hsegments + 1; x++) {
var time:Number = unit*(max - Math.sqrt((x - px)*(x - px) + (y - py)*(y - py)));
var point:Point = vertexs[y][x];
var target:Point = targets[y][x];
var tween:ITween = BetweenAS3.delay(
BetweenAS3.tween(point, {x: target.x, y: target.y}, {x: cx, y: cy}, time + unit*2, Quad.easeOut)
, time/2);
tweens.push(tween);
}
}
var itween:ITween = BetweenAS3.parallelTweens(tweens);
itween.addEventListener(TweenEvent.UPDATE, update, false, 0, true);
itween.addEventListener(TweenEvent.COMPLETE, next, false, 0, true);
itween.play();
}
private function next(evt:TweenEvent):void {
evt.target.removeEventListener(TweenEvent.UPDATE, update);
evt.target.removeEventListener(TweenEvent.COMPLETE, next);
//
var px:Number = Math.random()*400 - 40;
var py:Number = Math.random()*320 - 40;
hide(px, py);
}
private function hide(cx:Number, cy:Number):void {
var px:Number = hsegments*(cx/width);
var py:Number = vsegments*(cy/height);
var tweens:Array = new Array();
for (var y:uint = 0; y < vsegments + 1; y++) {
for (var x:uint = 0; x < hsegments + 1; x++) {
var time:Number = unit*Math.sqrt((x - px)*(x - px) + (y - py)*(y - py));
var point:Point = vertexs[y][x];
var tween:ITween = BetweenAS3.delay(
BetweenAS3.to(point, {x: cx, y: cy}, time, Cubic.easeIn)
, time/2);
tweens.push(tween);
}
}
var itween:ITween = BetweenAS3.delay(BetweenAS3.parallelTweens(tweens), 2);
itween.addEventListener(TweenEvent.UPDATE, update, false, 0, true);
itween.addEventListener(TweenEvent.COMPLETE, complete, false, 0, true);
itween.play();
}
private function update(evt:TweenEvent):void {
draw();
}
private function complete(evt:TweenEvent):void {
evt.target.removeEventListener(TweenEvent.UPDATE, update);
evt.target.removeEventListener(TweenEvent.COMPLETE, complete);
dispatchEvent(new Event(GenieEffect.COMPLETE));
}
private function draw():void {
var vertices:Vector.<Number> = new Vector.<Number>();
for (var y:uint = 0; y < vsegments + 1; y++) {
for (var x:uint = 0; x < hsegments + 1; x++) {
var point:Point = vertexs[y][x];
vertices.push(point.x, point.y);
}
}
graphics.clear();
graphics.beginBitmapFill(texture, null, false, true);
graphics.drawTriangles(vertices, indices, uvData, TriangleCulling.NONE);
graphics.endFill();
}
}