Twinkle Test (1)
TwinkleEffectのためのテスト
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/dcSL
*/
////////////////////////////////////////////////////////////////////////////////
// Twinkle Test (1)
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.geom.Rectangle;
import net.hires.debug.Stats;
[SWF(backgroundColor="#333333", width="465", height="465", frameRate="60")]
public class Main extends Sprite {
private var twinkle:TwinkleEffect;
public function Main() {
//Wonderfl.capture_delay(1);
init();
addChild(new Stats());
}
private function init():void {
graphics.beginFill(0x333333);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
//
var rect:Rectangle = new Rectangle(0, 0, 465, 465);
twinkle = new TwinkleEffect(rect);
addChild(twinkle);
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.geom.ColorTransform;
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 bitmapData:BitmapData;
private var bitmap:Bitmap;
private static var radius:uint = 10;
private static var life:uint = 40;
private var sources:Array;
private var particles:Array;
private var colorTrans:ColorTransform;
private var color:ColorHSV;
private var timer:Timer;
private static var interval:uint = 50;
public function TwinkleEffect(r:Rectangle) {
rect = r;
init();
}
private function init():void {
bitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
bitmap = new Bitmap(bitmapData);
addChild(bitmap);
sources = new Array();
for (var n:uint = 0; n <= life; n++) {
var src:Star = new Star(radius, n/life);
sources.push(src);
}
particles = new Array();
colorTrans = new ColorTransform();
color = new ColorHSV();
}
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 px:Number = Math.random()*(rect.width - radius*2) + radius;
var py:Number = Math.random()*(rect.height - radius*2) + radius;
plot(px, py);
}
}
private function plot(px:Number, py:Number):void {
var matrix:Matrix = new Matrix();
matrix.translate(px - radius, py - radius);
color.h = Math.random()*360;
var particle:TwinkleParticle = new TwinkleParticle();
particle.x = px;
particle.y = py;
particle.life = life;
particle.matrix = matrix;
particle.color = color.value;
particles.push(particle);
var src:Star = sources[particle.life];
bitmapData.lock();
bitmapData.draw(src, matrix);
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];
var src:Star = sources[particle.life];
colorTrans.color = particle.color;
bitmapData.draw(src, particle.matrix, colorTrans);
particle.life --;
if (particle.life < 0) {
particles.splice(n, 1);
particle = null;
}
}
bitmapData.unlock();
}
}
//////////////////////////////////////////////////
// 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() {
}
}
//////////////////////////////////////////////////
// Starクラス
//////////////////////////////////////////////////
import flash.display.BitmapData;
class Star extends BitmapData {
private var radius:uint = 0;
private var percent:Number = 0;
private static var radian:Number = Math.PI/180;
private static var division:uint = 12;
public function Star(r:uint, p:Number) {
radius = r;
percent = p;
super(radius*2, radius*2, true, 0x00000000);
init();
}
private function init():void {
var length:uint = uint(radius*percent);
var unit:uint = uint(360/division);
for (var n:uint = 0; n < length; n++) {
for (var a:uint = 0; a < 360; a+=unit) {
var px:Number = radius + n*Math.cos(a*radian);
var py:Number = radius + n*Math.sin(a*radian);
setPixel32(px, py, 0xFFFFFFFF);
}
}
}
}