3d particles forked from: flash on 2009-11-18
/**
* Copyright DLabz ( http://wonderfl.net/user/DLabz )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/q3pO
*/
// forked from littlepad's flash on 2009-11-18
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.PixelSnapping;
import flash.display.BlendMode;
import flash.geom.ColorTransform;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.events.Event;
import flash.geom.Point;
public class FlashTest extends Sprite {
private const WIDTH:Number = 456;
private const HEIGHT:Number = 456;
private var _canvas:BitmapData;
private var _glow:BitmapData;
private var _cTra:ColorTransform;
private var _rect:Rectangle;
private var _particles:Array;
private var _cPoint:Point;
private var speed:Number;
public function FlashTest() {
init();
}
private function init():void {
_rect = new Rectangle(0, 0, WIDTH, HEIGHT);
_cTra = new ColorTransform(.8, .8, .9, 1);
_particles = new Array();
_cPoint = new Point(WIDTH/2, HEIGHT/2);
_canvas = new BitmapData(WIDTH, HEIGHT, false, 0xFF000000);
addChild(new Bitmap(_canvas));
_glow = new BitmapData(WIDTH/4, HEIGHT/4, false, 0xFF000000);
var bmp:Bitmap = new Bitmap(_glow, PixelSnapping.NEVER, true);
bmp.scaleX = bmp.scaleY = 4;
bmp.blendMode = BlendMode.ADD;
addChild(bmp);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
var timer:Timer = new Timer(100);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
}
private function moveParticles ():void{
for(var i:uint = 0; i < _particles.length; i++){
var p:Particle = _particles[i];
var radian:Number = Math.atan2( _cPoint.y - p.y, _cPoint.x - p.x );
p.vx += 10 * Math.cos(radian);
p.vz -= 10* Math.cos(radian);
p.vy +=10 * Math.sin(radian);
p.vx *=0.3+0.7*(mouseX/WIDTH);
p.vy *= 0.3+0.8*(mouseY/HEIGHT);
p.vz *= 0.8;//*(mouseX/WIDTH);
p.x += p.vx;
p.y += p.vy;
p.z = p.vz;
}
}
private function onEnterFrame(e:Event):void {
moveParticles();
_particles.sortOn("z", Array.NUMERIC);//pseudo z-sort
for(var i:uint = 0; i < _particles.length; i++){
var p:Particle = _particles[i];
// if ( _canvas.getPixel32(p.x+m, p.y+n) <= 0xFFf5f5f5){
for (var m:uint = 0; m <5*Math.sqrt( p.z); m++){
for (var n:uint = 0; n <5* Math.sqrt(p.z); n++){
_canvas.setPixel32(p.x+m, p.y+n, p.c);//TODO: Project a rectangle in perspective, instead of drawing a square
}
}
// }
if(Math.abs(p.x - _cPoint.x) < 0.5 && Math.abs(p.y - _cPoint.y) < 0.5) {
_particles.splice(i, 1);
}
}
_glow.draw(_canvas, new Matrix(1/4, 0, 0, 1/4));
//_canvas.applyFilter(_canvas, _rect, new Point(), new BlurFilter(20, 20));
_canvas.colorTransform(_rect, _cTra);
}
private function onTimer(e:TimerEvent):void {
var n:uint = 10;
while(_particles.length < 300) {
var p:Particle = new Particle();
p.x = WIDTH/2 + 10*mouseX * Math.random();//WIDTH * Math.random();
p.y = HEIGHT/2 + 10*mouseY * Math.random();// HEIGHT * Math.random();
p.z -= p.x;//10 * Math.random();//
p.c = 0xFFFFFFFF * Math.random();
_particles.push(p);
}
}
}
}
internal class Particle {
public var x:Number;
public var y:Number;
public var z:Number;
public var vx:Number;
public var vy:Number;
public var vz:Number;
public var c:Number;
public function Particle() {
x = 0;
y = 0;
z = 800;
vx = 0;
vy = 0;
vz = 0;
c = 0xFFFFFFFF;
}
}