forked from: TwinkleEffect (3)
TwinkleEffect (3)
[AS3.0] DetectPixelsクラスに挑戦! (2)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1135
/**
* Copyright HAL8 ( http://wonderfl.net/user/HAL8 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/z6kd
*/
import flash.accessibility.AccessibilityImplementation;// forked from ProjectNya's TwinkleEffect (3)
////////////////////////////////////////////////////////////////////////////////
// TwinkleEffect (3)
//
// [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.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 = "8/80/8094/8094e540f4ab2a1c598986a861c592c143aac9d3";
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 = 32;
loader.alpha = 0.4;
}
private function initialize(evt:Event):void {
evt.target.removeEventListener(Event.INIT, initialize);
var area:Rectangle = new Rectangle(32- 22, 32 - 22, 400, 400);
detection = new DetectPixels(accuracy);
detection.search(loader, area, threshold);
var map:Array = detection.pixels();
//
var rect:Rectangle = new Rectangle(0, 0, 420, 420);
twinkle = new TwinkleEffect(rect, map);
addChild(twinkle);
twinkle.x = 22;
twinkle.y = 22;
twinkle.setup(100, 20);
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;
import frocessing.color.ColorHSV;
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 = 10;
private var interval:uint = 100;
private var life:uint = 10;
private var particles:Array;
private var color:ColorHSV;
private var unit:uint = 30;
private var division:uint = 12;
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();
color = new ColorHSV();
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 < 4; 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);
color.h = 40 + Math.random()*40;
var particle:TwinkleParticle = new TwinkleParticle();
particle.x = px;
particle.y = py;
particle.life = life;
particle.matrix = matrix;
particle.color = color.value;
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.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;
}
}