forked from: 20,000 particles with BetweenAS3
// forked from beinteractive's 20,000 particles with BetweenAS3
package
{
import com.flashdynamix.utils.SWFProfiler;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.PixelSnapping;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Point;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.easing.Back;
import org.libspark.betweenas3.easing.Quart;
import org.libspark.betweenas3.tweens.ITween;
public class ParticleBetween extends Sprite
{
private static const NUM_PARTICLES:uint = 20000;
private static const FADE:ColorTransform = new ColorTransform(1, 1, 1, 1, -32, -16, -16);
public function ParticleBetween()
{
setupParticles();
setupScreen();
setupStage();
SWFProfiler.init(this);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private var _bitmapData:BitmapData;
private var _bitmap:Bitmap;
private var _particles:Particle;
private function setupParticles():void
{
var prev:Particle = _particles = new Particle();
var p:Particle = null;
var a:Number, dx:Number, dy:Number;
var t:ITween;
var i:int = NUM_PARTICLES;
while (--i >= 0) {
a = Math.random() * Math.PI * 2;
dx = Math.cos(a) * 465 + 465 / 2;
dy = Math.sin(a) * 465 + 465 / 2;
p = new Particle();
p.p.x = 465 / 2;
p.p.y = 465 / 2;
t = BetweenAS3.tween(p.p, {x: dx, y: dy}, null, 1.5 + Math.random() * 4.5, Quart.easeIn);
t.stopOnComplete = false;
t.play();
prev.next = p;
prev = p;
}
}
private function setupScreen():void
{
_bitmapData = new BitmapData(465, 465, false, 0x000000);
_bitmap = addChild(new Bitmap(_bitmapData, PixelSnapping.NEVER, false)) as Bitmap;
}
private function setupStage():void
{
stage.frameRate = 60;
stage.quality = StageQuality.LOW;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, resizeHandler);
resizeHandler(null);
}
private function resizeHandler(e:Event):void
{
_bitmap.x = Math.floor((stage.stageWidth - 465) / 2);
_bitmap.y = Math.floor((stage.stageHeight - 465) / 2);
}
private function enterFrameHandler(e:Event):void
{
var bitmapData:BitmapData = _bitmapData;
bitmapData.lock();
bitmapData.colorTransform(_bitmapData.rect, FADE);
var p:Particle = _particles;
var pos:Point;
while ((p = p.next) != null) {
pos = p.p;
bitmapData.setPixel(pos.x >> 0, pos.y >> 0, 0xffffff);
}
bitmapData.unlock();
}
}
}
import flash.geom.Point;
internal class Particle
{
public var p:Point = new Point();
public var next:Particle;
}