TwinkleEffect [A Happy New Year]
////////////////////////////////////////////////////////////////////////////////
// TwinkleEffect [A Happy New Year]
//
// [AS3.0] TwinkleEffectクラスだ! (3)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1273
// [AS3.0] DetectPixelsクラスに挑戦! (2)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1135
////////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wQEC
*/
////////////////////////////////////////////////////////////////////////////////
// TwinkleEffect [A Happy New Year]
//
// [AS3.0] TwinkleEffectクラスだ! (3)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1273
// [AS3.0] DetectPixelsクラスに挑戦! (2)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1135
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.LoaderContext;
[SWF(backgroundColor="#000000", width="465", height="465", frameRate="60")]
public class Main extends Sprite {
private var loader:Loader;
private static var basePath:String = "http://assets.wonderfl.net/images/related_images/";
private static var photoPath:String = "d/d4/d49f/d49f6923a043860aed5d60334893f27205017d3a";
private var detection:DetectPixels;
private static var accuracy:uint = 2;
private var threshold:uint = 0xFF808080;
private var twinkle:TwinkleEffect;
public function Main() {
//Wonderfl.capture_delay(1);
init();
}
private function init():void {
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
//
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, initialize, false, 0, true);
loader.load(new URLRequest(basePath + photoPath), new LoaderContext(true));
addChild(loader);
loader.x = 32;
loader.y = 264;
loader.alpha = 0.2;
}
private function initialize(evt:Event):void {
evt.target.removeEventListener(Event.INIT, initialize);
var content:Bitmap = Bitmap(evt.target.content);
detection = new DetectPixels(accuracy);
var area:Rectangle = new Rectangle(0, 0, 400, 200);
detection.search(content, area, threshold);
var map:Array = detection.pixels();
//
var rect:Rectangle = new Rectangle(0, 0, 400, 200);
twinkle = new TwinkleEffect(rect, map);
addChild(twinkle);
twinkle.x = 32;
twinkle.y = 264;
twinkle.setup(100, 10);
twinkle.start();
}
}
}
//////////////////////////////////////////////////
// TwinkleEffectクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
class TwinkleEffect extends Sprite {
private var rect:Rectangle;
private var map:Array;
private var bitmapData:BitmapData;
private var bitmap:Bitmap;
private static var radius:uint = 8;
private var interval:uint = 100;
private var life:uint = 10;
private var particles:Array;
private static var white:uint = 0xFFFFFF;
private static var blue:uint = 0xCCFFFF;
private static var red:uint = 0xFFCCCC;
private static var yellow:uint = 0xFFFFCC;
private static var green:uint = 0xCCFFCC;
private static var colors:Array = [white, blue, red, yellow, green];
private var unit:uint = 36;
private var division:uint = 10;
private static var radian:Number = Math.PI/180;
private var timer:Timer;
public function TwinkleEffect(r:Rectangle, m:Array) {
rect = r;
map = m;
init();
}
private function init():void {
bitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
bitmap = new Bitmap(bitmapData);
addChild(bitmap);
particles = new Array();
unit = uint(360/division);
}
public function setup(i:uint = 100, l:uint = 10):void {
interval = i;
life = l;
}
public function start():void {
timer = new Timer(interval);
timer.addEventListener(TimerEvent.TIMER, create, false, 0, true);
timer.start();
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
}
public function stop():void {
timer.stop();
removeEventListener(Event.ENTER_FRAME, update);
}
private function create(evt:TimerEvent):void {
for (var n:uint = 0; n < 10; n++) {
var id:uint = uint(Math.random()*map.length);
var px:Number = map[id].x;
var py:Number = map[id].y;
plot(px, py);
}
}
private function plot(px:Number, py:Number):void {
var matrix:Matrix = new Matrix();
matrix.translate(px - radius, py - radius);
var particle:TwinkleParticle = new TwinkleParticle();
particle.x = px;
particle.y = py;
particle.life = life;
particle.matrix = matrix;
particle.color = colors[uint(Math.random()*colors.length)];
particles.push(particle);
bitmapData.lock();
draw(px, py, 0xFFFFFF, life/life);
bitmapData.unlock();
}
private function update(evt:Event):void {
bitmapData.lock();
bitmapData.fillRect(rect, 0x00000000);
for (var n:uint = 0; n < particles.length; n++) {
var particle:TwinkleParticle = particles[n];
draw(particle.x, particle.y, particle.color, particle.life/life);
particle.life --;
if (particle.life < 0) {
particles.splice(n, 1);
particle = null;
}
}
bitmapData.unlock();
}
private function draw(px:uint, py:uint, color:uint, percent:Number):void {
var length:uint = uint(radius*percent);
var argb:uint = 0xFF << 24 | color;
for (var n:uint = 0; n < length; n++) {
for (var a:uint = 0; a < 360; a+=unit) {
var dx:Number = px + n*Math.cos(a*radian);
var dy:Number = py + n*Math.sin(a*radian);
bitmapData.setPixel32(dx, dy, argb);
}
}
}
}
//////////////////////////////////////////////////
// TwinkleParticleクラス
//////////////////////////////////////////////////
import flash.geom.Matrix;
class TwinkleParticle {
public var x:uint;
public var y:uint;
public var life:int;
public var matrix:Matrix;
public var color:uint;
public function TwinkleParticle() {
}
}
//////////////////////////////////////////////////
// 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;
public function DetectPixels(a:uint = 1) {
accuracy = a;
}
public function search(t:IBitmapDrawable, r:Rectangle, th:uint = 0x80FFFFFF):void {
bd = t;
rect = r;
threshold = th;
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;
var py:int = y*accuracy + rect.y;
var point:Point = new Point(px, py);
mapList.push(point);
}
}
}
}
public function pixels():Array {
return mapList;
}
}