drawTriangles (4)
////////////////////////////////////////////////////////////////////////////////
// drawTriangles (4)
//
// [AS3.0] drawTriangles()メソッド! (4)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1615
//
// 参考スクリプト (@clockmaker)
// http://wonderfl.net/c/qnTR
// http://wonderfl.net/c/gf3B/
////////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/nahu
*/
////////////////////////////////////////////////////////////////////////////////
// drawTriangles (4)
//
// [AS3.0] drawTriangles()メソッド! (4)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1615
//
// 参考スクリプト (@clockmaker)
// http://wonderfl.net/c/qnTR
// http://wonderfl.net/c/gf3B/
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.MouseEvent;
import flash.display.Shape;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.ITween;
import org.libspark.betweenas3.events.TweenEvent;
import org.libspark.betweenas3.easing.*;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var fontloader:FontLoader;
//private static var basePath:String = "";
private static var basePath:String = "http://www.project-nya.jp/images/flash/";
private static var fontPath:String = "fonts/MyriadProSemibold.swf";
private static var className:String = "MyriadProSemibold";
private var progressBar:ProgressBar;
private var loader:ImageLoader;
private static var filePath:String = "piyo_drawTriangles.png";
private var container:Sprite;
private var canvas:Shape;
private var texture:BitmapData;
private static var ox:uint = 232 - 160;
private static var oy:uint = 232 - 120;
private var effect:GenieEffect;
public function Main() {
//Wonderfl.capture_delay(4);
init();
}
private function init():void {
fontloader = new FontLoader();
fontloader.addEventListener(FontLoader.COMPLETE, loaded, false, 0, true);
fontloader.load(basePath + fontPath, className);
//
container = new Sprite();
addChild(container);
container.alpha = 0;
container.visible = false;
canvas = new Shape();
container.addChild(canvas);
canvas.x = ox;
canvas.y = oy;
}
private function loaded(evt:Event):void {
fontloader.removeEventListener(FontLoader.COMPLETE, loaded);
fontloader = null;
//
progressBar = new ProgressBar(465);
addChild(progressBar);
progressBar.x = 232;
progressBar.y = 222;
progressBar.addEventListener(ProgressBar.FADE_OUT, fadeOut, false, 0, true);
progressBar.addEventListener(ProgressBar.BAR_FADE_OUT, barFadeOut, false, 0, true);
progressBar.init();
//
loader = new ImageLoader();
loader.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
loader.addEventListener(Event.COMPLETE, complete, false, 0, true);
loader.load(basePath + filePath, true);
}
private function progress(evt:ProgressEvent):void {
progressBar.progress(evt.bytesLoaded, evt.bytesTotal);
}
private function complete(evt:Event):void {
loader.removeEventListener(ProgressEvent.PROGRESS, progress);
loader.removeEventListener(Event.COMPLETE, complete);
//
texture = loader.content.bitmapData;
setup();
}
private function fadeOut(evt:Event):void {
progressBar.removeEventListener(ProgressBar.FADE_OUT, fadeOut);
}
private function barFadeOut(evt:Event):void {
progressBar.removeEventListener(ProgressBar.BAR_FADE_OUT, barFadeOut);
//
var itween:ITween = BetweenAS3.to(container, {alpha: 1, visible: 1}, 0.6, Linear.easeNone);
itween.addEventListener(TweenEvent.COMPLETE, faded, false, 0, true);
itween.play();
}
private function faded(evt:Event):void {
evt.target.removeEventListener(TweenEvent.COMPLETE, faded);
removeChild(progressBar);
progressBar = null;
loader = null;
}
private function setup():void {
effect = new GenieEffect(canvas.graphics, texture, 32, 24);
effect.addEventListener(Event.COMPLETE, _complete, false, 0, true);
stage.addEventListener(MouseEvent.CLICK, click, false, 0, true);
}
private function click(evt:MouseEvent):void {
stage.removeEventListener(MouseEvent.CLICK, click);
if (!effect.hiden) {
effect.hide(mouseX - ox, mouseY - oy);
} else {
effect.show(mouseX - ox, mouseY - oy);
}
}
private function _complete(evt:Event):void {
stage.addEventListener(MouseEvent.CLICK, click, false, 0, true);
}
}
}
//////////////////////////////////////////////////
// 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 {
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>;
public var hiden:Boolean = false;
private static var unit:Number = 0.02;
public function GenieEffect(g:Graphics, t:BitmapData, h:uint = 1, v:uint = 1) {
graphics = g;
texture = t;
if (h > 0) hsegments = h;
if (v > 0) vsegments = v;
xunit = 1/hsegments;
yunit = 1/vsegments;
width = texture.width;
height = texture.height;
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 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.parallelTweens(tweens);
itween.addEventListener(TweenEvent.UPDATE, update, false, 0, true);
itween.addEventListener(TweenEvent.COMPLETE, complete, false, 0, true);
itween.play();
}
public 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, 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);
hiden = !hiden;
dispatchEvent(new Event(Event.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();
}
}
//////////////////////////////////////////////////
// FontLoaderクラス
//////////////////////////////////////////////////
import flash.events.EventDispatcher;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.text.Font;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.system.ApplicationDomain;
import flash.system.SecurityDomain;
import flash.system.LoaderContext;
import flash.utils.getDefinitionByName;
class FontLoader extends EventDispatcher {
public var id:uint;
private var loader:Loader;
private var info:LoaderInfo;
private var _className:String;
private var _font:Font;
private var _fontName:String;
private var embeded:Boolean = false;
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 FontLoader() {
loader = new Loader();
info = loader.contentLoaderInfo;
}
public function load(file:String, name:String, e:Boolean = false):void {
_className = name;
embeded = e;
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 {
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
context.securityDomain = SecurityDomain.currentDomain;
loader.load(new URLRequest(file), context);
} 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(FontLoader.IO_ERROR));
}
private function httpstatus(evt:HTTPStatusEvent):void {
dispatchEvent(new Event(FontLoader.HTTP_STATUS));
}
private function securityerror(evt:SecurityErrorEvent):void {
dispatchEvent(new Event(FontLoader.SECURITY_ERROR));
}
private function initialize(evt:Event):void {
dispatchEvent(new Event(FontLoader.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);
var FontClass:Class = Class(ApplicationDomain.currentDomain.getDefinition(className));
if (!embeded) {
Font.registerFont(FontClass);
_font = Font(new FontClass());
} else {
var document:Object = new FontClass();
_font = document.font;
}
_fontName = _font.fontName;
dispatchEvent(new Event(FontLoader.COMPLETE));
}
public function get className():String {
return _className;
}
public function get font():Font {
return _font;
}
public function get fontName():String {
return _fontName;
}
}
//////////////////////////////////////////////////
// ProgressBarクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
class ProgressBar extends Sprite {
private var base:Sprite;
private var bar:Shape;
private var prog:Shape;
private var title:TextField;
private var txt:TextField;
private static var label:String = "loading...";
private static var fontType:String = "Myriad Pro Semibold";
private var _width:uint = 800;
private static var _height:uint = 40;
private static var bHeight:uint = 10;
private static var tWidth:uint = 50;
private static var tHeight:uint = 20;
private static var bColor:uint = 0x000000;
private static var tColor:uint = 0x000000;
private static var pColor:uint = 0x000000;
private static var eColor:uint = 0xCC0000;
private var shade:DropShadowFilter;
private var _percent:Number = 0;
private var targetPercent:Number;
private static var interval:uint = 500;
private static var deceleration:Number = 0.4;
private var timer:Timer;
public static const FADE_OUT:String = "fadeOut";
public static const BAR_FADE_OUT:String = "baseFadeOut";
public function ProgressBar(w:uint) {
_width = w;
draw();
percent = 0;
visible = false;
}
private function draw():void {
createChildren();
createText();
}
public function update(w:uint):void {
_width = w;
base.x = -_width/2;
createBar(bar, _width, pColor);
createLine(prog, _width, bColor);
}
private function createChildren():void {
base = new Sprite();
bar = new Shape();
prog = new Shape();
addChild(base);
base.addChild(bar);
base.addChild(prog);
base.x = -_width/2;
base.y = 8;
createBar(bar, _width, pColor);
createLine(prog, _width, bColor);
bar.mask = prog;
}
private function createText():void {
title = new TextField();
txt = new TextField();
addChild(title);
addChild(txt);
title.x = -tWidth;
title.y = -_height/2 + 2;
title.width = tWidth;
title.height = tHeight - 2;
title.type = TextFieldType.DYNAMIC;
title.selectable = false;
title.embedFonts = true;
title.antiAliasType = AntiAliasType.NORMAL;
var tfl:TextFormat = new TextFormat();
tfl.font = fontType;
tfl.size = 12;
tfl.align = TextFormatAlign.LEFT;
title.defaultTextFormat = tfl;
title.textColor = tColor;
title.text = label;
txt.x = 0;
txt.y = -_height/2 + 2;
txt.width = tWidth;
txt.height = tHeight - 2;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
txt.embedFonts = true;
txt.antiAliasType = AntiAliasType.NORMAL;
var tfr:TextFormat = new TextFormat();
tfr.font = fontType;
tfr.size = 12;
tfr.align = TextFormatAlign.RIGHT;
txt.textColor = tColor;
txt.defaultTextFormat = tfr;
}
public function init():void {
alpha = 1;
createBar(bar, _width, pColor);
percent = 0;
prog.x = 0;
prog.scaleX = 0;
visible = true;
}
public function error(e:String):void {
alpha = 1;
txt.text = "0%";
createBar(bar, _width, eColor);
prog.scaleX = 1;
visible = true;
}
public function progress(l:Number, t:Number):void {
targetPercent = Math.round(l/t*100);
addEventListener(Event.ENTER_FRAME, progressTo, false, 0, true);
}
private function progressTo(evt:Event):void {
percent += (targetPercent - percent)*deceleration;
if (Math.abs(targetPercent - percent) < 0.5) {
percent = targetPercent;
removeEventListener(Event.ENTER_FRAME, progressTo);
}
if (percent >= 100) {
timer = new Timer(interval, 1);
timer.addEventListener(TimerEvent.TIMER, fadeOut, false, 0, true);
timer.start();
}
}
private function fadeOut(evt:TimerEvent):void {
timer.removeEventListener(TimerEvent.TIMER, fadeOut);
addEventListener(Event.ENTER_FRAME, reduction, false, 0, true);
}
private function reduction(evt:Event):void {
prog.x += (_width - prog.x)*deceleration;
if (Math.abs(_width - prog.x) < 0.5) {
prog.x = _width;
removeEventListener(Event.ENTER_FRAME, reduction);
addEventListener(Event.ENTER_FRAME, baseFadeOut, false, 0, true);
dispatchEvent(new Event(ProgressBar.FADE_OUT));
}
}
private function baseFadeOut(evt:Event):void {
var speed:Number = 0.1;
alpha -= speed;
if (alpha <= 0) {
removeEventListener(Event.ENTER_FRAME, baseFadeOut);
alpha = 0;
visible = false;
dispatchEvent(new Event(ProgressBar.BAR_FADE_OUT));
}
}
public function get percent():Number {
return _percent;
}
public function set percent(value:Number):void {
_percent = value;
manage(_percent);
}
private function manage(p:Number):void {
txt.text = String(Math.round(p)) + "%";
prog.scaleX = p/100;
}
private function createBase(target:Shape, x:int, y:int, w:uint, h:uint, color:uint):void {
target.graphics.beginFill(color);
target.graphics.drawRect(x, y, w, h);
target.graphics.endFill();
}
private function createLine(target:Shape, w:uint, color:uint, alpha:Number = 1):void {
target.graphics.clear();
target.graphics.beginFill(color, alpha);
target.graphics.drawRect(0, 0, w, 1);
target.graphics.endFill();
}
private function createBar(target:Shape, w:uint, color:uint):void {
target.graphics.clear();
target.graphics.beginFill(color);
target.graphics.drawRect(0, 0, w, 1);
target.graphics.endFill();
}
}
//////////////////////////////////////////////////
// ImageLoaderクラス
//////////////////////////////////////////////////
import flash.events.EventDispatcher;
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;
class ImageLoader extends EventDispatcher {
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));
} 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);
dispatchEvent(new Event(ImageLoader.COMPLETE));
}
}