日食っぽいの
////////////////////////////////////////////////////////////////////////////////
// 日食っぽいの
//
// [AS3.0] FontLoaderクラスに挑戦! (1)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1337
// [AS3.0] 日食もどき (3)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1498
////////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/agY2
*/
////////////////////////////////////////////////////////////////////////////////
// 日食っぽいの
//
// [AS3.0] FontLoaderクラスに挑戦! (1)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1337
// [AS3.0] 日食もどき (3)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1498
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;
import flash.geom.Rectangle;
[SWF(backgroundColor="#000000", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var loader: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 label:Label;
private var eclipse:FlareCircle;
public function Main() {
//Wonderfl.capture_delay(1);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
init();
}
private function init():void {
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
//
var rect:Rectangle = new Rectangle(0, 0, 400, 400);
eclipse = new FlareCircle(rect, 140);
addChild(eclipse);
eclipse.x = 32;
eclipse.y = 32;
//
loader = new FontLoader();
loader.addEventListener(FontLoader.COMPLETE, complete, false, 0, true);
loader.load(basePath + fontPath, className);
}
private function complete(evt:Event):void {
loader.removeEventListener(FontLoader.COMPLETE, complete);
//
label = new Label(200, 60, 60, Label.CENTER);
addChild(label);
label.x = 132;
label.y = 202;
label.textColor = 0xFFFFFF;
label.text = "eclipse";
label.alpha = 0.4;
//
eclipse.start();
}
}
}
//////////////////////////////////////////////////
// FlareCircleクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Shape;
import flash.geom.Rectangle;
class FlareCircle extends Sprite {
private var rect:Rectangle;
private var radius:uint;
private var shape:Shape;
private var flare:FlareMap;
private var detection:DetectPixels;
public function FlareCircle(area:Rectangle, r:uint) {
rect = area;
radius = r;
init();
}
private function init():void {
shape = new Shape();
shape.graphics.lineStyle(8, 0xFFFFFF, 0.5);
shape.graphics.drawCircle(rect.width/2, rect.height/2, radius);
//addChild(shape);
shape.x = rect.x;
shape.y = rect.y;
flare = new FlareMap(rect);
addChild(flare);
flare.x = rect.x + rect.width/2;
flare.y = rect.y + rect.height/2;
detection = new DetectPixels(2);
detection.search(shape, rect, 0x66FFFFFF);
flare.map = detection.pixels();
flare.setup(60);
flare.addEventListener(Event.COMPLETE, complete, false, 0, true);
//flare.start();
}
public function start():void {
flare.start();
}
public function stop():void {
flare.stop();
}
private function complete(evt:Event):void {
dispatchEvent(new Event(Event.COMPLETE));
}
}
//////////////////////////////////////////////////
// FlareMapクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.filters.BlurFilter;
import flash.filters.GlowFilter;
import flash.display.BlendMode;
//import sketchbook.colors.ColorUtil;
class FlareMap extends Sprite {
private var rect:Rectangle;
private var fire:Rectangle;
private var flare:BitmapData;
private var bitmapData:BitmapData;
private var bitmap:Bitmap;
private var rPalette:Array;
private var gPalette:Array;
private var bPalette:Array;
private static var point:Point = new Point(0, 0);
private var speeds:Point = new Point(0, 0);
private static var unit:uint = 2;
private var segments:uint = 8;
private var blur:BlurFilter;
private var maps:Array;
public var offset:Object = {x: 0, y: 0};
private var faded:uint = 0;
public static const COMPLETE:String = Event.COMPLETE;
public function FlareMap(r:Rectangle) {
rect = r;
initialize();
draw();
}
public function setup(seg:uint = 8):void {
segments = seg;
}
public function set map(list:Array):void {
maps = list;
}
private function initialize():void {
rPalette = new Array();
gPalette = new Array();
bPalette = new Array();
for (var n:uint = 0; n < 256; n++) {
var luminance:uint = (n < 128) ? n*2 : 0;
//var rgb:Object = ColorUtil.HLS2RGB(n*360/256, luminance, 100);
var rgb:Object = HLS2RGB(n*360/256, luminance, 100);
var color:uint = rgb.r << 16 | rgb.g << 8 | rgb.b;
rPalette[n] = color;
gPalette[n] = 0;
bPalette[n] = 0;
}
blur = new BlurFilter(4, 4, 3);
blendMode = BlendMode.ADD;
}
private function draw():void {
fire = new Rectangle(0, 0, rect.width, rect.height);
flare = new BitmapData(fire.width, fire.height, false, 0xFF000000);
bitmapData = new BitmapData(rect.width, rect.height, false, 0xFF000000);
bitmap = new Bitmap(bitmapData);
addChild(bitmap);
bitmap.x = - rect.width/2;
bitmap.y = - rect.height/2;
}
public function start():void {
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
}
public function stop():void {
removeEventListener(Event.ENTER_FRAME, update);
faded = 0;
addEventListener(Event.ENTER_FRAME, clear, false, 0, true);
}
private function update(evt:Event):void {
if (!maps) return;
flare.lock();
bitmapData.lock();
for (var n:uint = 0; n < segments; n++) {
var id:uint = Math.random()*maps.length;
var px:int = maps[id].x + offset.x;
var py:int = maps[id].y + offset.y;
var range:Rectangle = new Rectangle(px, py, unit, unit)
flare.fillRect(range, 0xFFFFFF);
}
flare.applyFilter(flare, fire, speeds, blur);
bitmapData.paletteMap(flare, rect, point, rPalette, gPalette, bPalette);
flare.unlock();
bitmapData.unlock();
}
private function clear(evt:Event):void {
faded ++;
flare.lock();
bitmapData.lock();
flare.applyFilter(flare, fire, speeds, blur);
bitmapData.paletteMap(flare, rect, point, rPalette, gPalette, bPalette);
if (faded > 20) {
bitmapData.fillRect(rect, 0x000000);
removeEventListener(Event.ENTER_FRAME, clear);
dispatchEvent(new Event(FlareMap.COMPLETE));
}
flare.unlock();
bitmapData.unlock();
}
private function HLS2RGB(h:Number, l:Number, s:Number):Object{
var max:Number;
var min:Number;
h = (h < 0)? h % 360+360 : (h>=360)? h%360: h;
l = (l < 0)? 0 : (l > 100)? 100 : l;
s = (s < 0)? 0 : (s > 100)? 100 : s;
l *= 0.01;
s *= 0.01;
if (s == 0) {
var val:Number = l*255;
return {r:val, g:val, b:val};
}
if (l < 0.5) {
max = l*(1 + s)*255;
} else {
max = (l*(1 - s) + s)*255;
}
min = (2*l)*255 - max;
return _hMinMax2RGB(h, min, max);
}
private function _hMinMax2RGB(h:Number, min:Number, max:Number):Object{
var r:Number;
var g:Number;
var b:Number;
var area:Number = Math.floor(h/60);
switch(area){
case 0:
r = max;
g = min + h * (max - min)/60;
b = min;
break;
case 1:
r = max - (h - 60)*(max - min)/60;
g = max;
b = min;
break;
case 2:
r = min ;
g = max;
b = min + (h - 120)*(max - min)/60;
break;
case 3:
r = min;
g = max - (h - 180)*(max - min)/60;
b =max;
break;
case 4:
r = min + (h - 240)*(max - min)/60;
g = min;
b = max;
break;
case 5:
r = max;
g = min;
b = max - (h - 300)*(max - min)/60;
break;
case 6:
r = max;
g = min + h*(max - min)/60;
b = min;
break;
}
r = Math.min(255, Math.max(0, Math.round(r)));
g = Math.min(255, Math.max(0, Math.round(g)));
b = Math.min(255, Math.max(0, Math.round(b)));
return {r:r, g:g, b:b};
}
}
//////////////////////////////////////////////////
// DetectPixelsクラス
//////////////////////////////////////////////////
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.display.IBitmapDrawable;
class DetectPixels {
private var bd:IBitmapDrawable;
private var rect:Rectangle;
private var map:BitmapData;
private var mapList:Array;
private var accuracy:uint;
private var threshold:uint = 0x80FFFFFF;
private var offset:Object = {x: 0, y: 0};
public function DetectPixels(a:uint = 1) {
accuracy = a;
}
public function search(t:IBitmapDrawable, r:Rectangle, th:uint = 0x80FFFFFF, o:Object = null):void {
bd = t;
rect = r;
threshold = th;
if (o) offset = o;
var w:uint = rect.width/accuracy;
var h:uint = rect.height/accuracy;
detect(w, h);
}
private function detect(w:uint, h:uint):void {
map = new BitmapData(w, h, true, 0x00000000);
var matrix:Matrix = new Matrix();
matrix.translate(-rect.x, -rect.y);
matrix.scale(1/accuracy, 1/accuracy);
map.lock();
map.draw(bd, matrix);
map.unlock();
mapList = new Array();
for (var x:uint = 0; x < w; x++) {
for (var y:uint = 0; y < h; y++) {
var color:uint = map.getPixel32(x, y);
if (color >= threshold) {
var px:int = x*accuracy + rect.x + offset.x;
var py:int = y*accuracy + rect.y + offset.y;
var point:Point = new Point(px, py);
mapList.push(point);
}
}
}
}
public function pixels():Array {
return mapList;
}
}
//////////////////////////////////////////////////
// PerlinNoiseクラス
//////////////////////////////////////////////////
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
class PerlinNoise extends BitmapData {
private var bx:uint;
private var by:uint;
private var octaves:uint;
private var seed:uint;
private var stitch:Boolean = true;
private var fractalNoise:Boolean = true;
private var channel:uint = 0;
private var grayScale:Boolean = true;
private var offsets:Array = new Array();
public function PerlinNoise(rect:Rectangle, x:uint, y:uint, o:uint = 1, g:Boolean = true, c:uint = 0, s:uint = 1, st:Boolean = false, f:Boolean = true) {
super(rect.width, rect.height, false, 0xFF000000);
bx = x;
by = y;
octaves = o;
grayScale = g;
channel = c;
if (grayScale) channel = 0;
for (var n:uint = 0; n < octaves; n++) {
var point:Point = new Point();
offsets.push(point);
}
stitch = st;
fractalNoise = f;
create(s, offsets);
}
private function create(s:uint, o:Array = null):void {
seed = s;
offsets = o;
if (offsets == null) offsets = [new Point()];
lock();
perlinNoise(bx, by, octaves, seed, stitch, fractalNoise, channel, grayScale, offsets);
draw(this);
unlock();
}
public function update(speeds:Array):void {
for (var n:uint = 0; n < octaves; n++) {
var offset:Point = offsets[n];
var speed:Point = speeds[n];
offset.x += speed.x;
offset.y += speed.y;
}
lock();
perlinNoise(bx, by, octaves, seed, stitch, fractalNoise, channel, grayScale, offsets);
draw(this);
unlock();
}
}
//////////////////////////////////////////////////
// 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;
}
}
//////////////////////////////////////////////////
// Labelクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
class Label extends Sprite {
private var txt:TextField;
private static var fontType:String = "Myriad Pro Semibold";
private var _width:uint = 20;
private var _height:uint = 20;
private var size:uint = 12;
public static const LEFT:String = TextFormatAlign.LEFT;
public static const CENTER:String = TextFormatAlign.CENTER;
public static const RIGHT:String = TextFormatAlign.RIGHT;
public function Label(w:uint, h:uint, s:uint = 12, align:String = LEFT) {
_width = w;
_height = h;
size = s;
draw(align);
}
private function draw(align:String):void {
txt = new TextField();
addChild(txt);
txt.width = _width;
txt.height = _height;
txt.autoSize = align;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
txt.embedFonts = true;
txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = size;
tf.align = align;
txt.defaultTextFormat = tf;
textColor = 0x000000;
}
public function set text(param:String):void {
txt.text = param;
}
public function set textColor(param:uint):void {
txt.textColor = param;
}
}