In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: bouncing balls in 3d space(3)

someone please refactor this.
/**
 * Copyright harupiyo ( http://wonderfl.net/user/harupiyo )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/iB3K
 */

// forked from uwi's forked from:  bouncing balls in 3d space(3)
// forked from k__'s  bouncing balls in 3d space(3)
// forked from k__'s bouncing balls in 3d space
//someone please refactor this.

package {
	import flash.utils.IDataInput;
	import flash.accessibility.Accessibility;
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
    import net.hires.debug.Stats;

    [SWF(frameRate=60)]
	public class Main extends Sprite {
		private var particles:Array = [];
		private var _focalLength:Number = 350;
		private var camX:Number = 0,camY:Number = -100,camZ:Number = 0;
		private var cx:Number,cy:Number;
		private var canvas:BitmapData;
		private var angle : Number = 0;
		private var images : Array = [];
		private var colors : Array = [
			0x00FF9300,
			0x006DE20E,
			0x00B011FD,
			0x00F50107,
			0x000992F7
		];
		private function _getRandomColor():int {
			return colors[ int(Math.random() * colors.length ) ];
		}
		
		public function Main() {
			var bg : Sprite = new Sprite();
			addChild(bg);
			bg.graphics.beginFill( _getRandomColor() );
			bg.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
			bg.graphics.endFill();

            canvas = new BitmapData(stage.stageWidth,stage.stageHeight,false, _getRandomColor() );
			addChild(new Bitmap(canvas));

			cx = stage.stageWidth / 2;
			cy = stage.stageWidth / 2;

			var ss:Sprite = new Sprite();
			var s:Sprite = new Sprite();
			s.addChild(ss);
			ss.graphics.beginGradientFill(GradientType.RADIAL,[0xffffff,0xff0000],[0.5,0.1],[50,255]);
			ss.graphics.drawCircle(40,40,100);
			ss.graphics.endFill();
			ss.x = ss.y = 40;
			var bd:BitmapData;
			var b:Bitmap;
			var blur:BlurFilter;
			for (var i:uint = 1; i < 100; i ++) {
				ss.scaleX = i / 7 / 15;
				ss.scaleY = i / 7 / 15;
				if (i > 20) {
					blur = new BlurFilter((i - 20) /4,(i -20) / 4);
					ss.filters = [blur];
				}
				bd = new BitmapData(80,80,true,0x00000000 );
				bd.draw(s);
				images.push(bd);
			}
			particles.push(new Particle());
			addEventListener(Event.ENTER_FRAME, h_enterFrame);

            addChild(new Stats());
		}

		private function h_enterFrame(evt:Event):void {
//			if (Math.random() < 1) {
				particles.push(new Particle());
				particles.push(new Particle()); // twice
//			}
			draw();
		}

	    private var bmd:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,true,0x000000);

		private function draw():void {

			var i:int,j:uint;
			var tParticles:Array = [];
			var p3d:Object;
			var p2d:Object;
            var ppp : Point = new Point();

         	bmd.fillRect(bmd.rect, _getRandomColor() );
			var tz:uint = 0;
			for (j = 0; j < 1; j ++) {
				angle++;
                 var rad : Number = angle / 180 * Math.PI;
                 var ccc : Number = Math.cos(rad);
                 var sss : Number = Math.sin(rad);
                                
				for (i = particles.length - 1; i >= 0; i --) {
					particles[i].update();

					if (particles[i].life<0) {
                     	if(i == particles.length - 1){
                         	particles.pop();
                         }else{
                             particles[i] = particles.pop();
                         }
					}else{
                         tParticles.push({
	                         x : ccc*particles[i].x-sss*particles[i].z - camX,
	                         y : particles[i].y - camY,
	                         z : ccc*particles[i].z+sss*particles[i].x - camZ
	                     });
                     }
				}

				for each(p3d in tParticles){
					if (p3d.z < -_focalLength) {
						continue;
					}
					p2d=p3Dto2D(p3d,_focalLength);
                     ppp.x = p2d.x - 30 + cx;
                     ppp.y = p2d.y - 30 + cy;
					tz=p2d.scale*20;
					if (tz>98) {
						tz=98;
					}
                     bmd.copyPixels(images[tz], images[tz].rect, ppp, null, null, true);
				}
			}

			canvas.lock();
			canvas.applyFilter(canvas, canvas.rect, new Point(), cmf);
			canvas.draw(bmd,null , null, BlendMode.ADD);
			canvas.unlock();
		}

        private var cmf : ColorMatrixFilter = new ColorMatrixFilter([
                0.95, 0, 0, 0, 0,
                0, 0.95, 0, 0, 0,
                0, 0, 0.8, 0, 0,
                0, 0, 0, 0.95, 0
        ]);

		private function rotateX(p3d:Object, angle:Number):Object {
			angle=angle/180*Math.PI;
			var c : Number = Math.cos(angle);
			var s : Number = Math.sin(angle);
            return {x : p3d.x, y : c*p3d.y-s*p3d.z, z : c*p3d.z+s*p3d.y};
		}

		private function rotateY(p3d:Object, angle:Number):Object {
			angle=angle/180*Math.PI;
                        var c : Number = Math.cos(angle);
                        var s : Number = Math.sin(angle);
                        return {x : c*p3d.x-s*p3d.z, y : p3d.y, z : c*p3d.z+s*p3d.x};
		}

		private function rotateZ(p3d:Object, angle:Number):Object {
			angle=angle/180*Math.PI;
                        var c : Number = Math.cos(angle);
                        var s : Number = Math.sin(angle);
                        return {x : c*p3d.x-s*p3d.y, y : c*p3d.y+s*p3d.x, z : p3d.z};
		}

		private function p3Dto2D(p3d:Object, _fl:Number):Object {
			var scale:Number = _fl / (_fl + p3d.z);
			return {x : p3d.x * scale, y : p3d.y * scale, scale : scale};
		}


	}
}

class Particle {
	import flash.display.*;
	public var x:Number=0,y:Number=0,z:Number=0;
	public var vx:Number=0,vy:Number=0,vz:Number=0;
	public var life:int=150;
	function Particle() {
		x=Math.random()*100;
		y=- Math.random()*100;
		z=Math.random()*100+100;
		vx=Math.random()*5-2.5;
		vy=Math.random()*-4 -4;
		vz=Math.random()*-1-1.5;
	}

	public function update():void {
		if (y>=0) {
			vy*=-0.8;
			if (vy < 2 && vy > -2) {
				vy=0;
			}
			y=0;
		} else {
			vy+=0.1;
		}
		x+=vx;
		y+=vy;
		z+=vz;
		life--;
		if (z>1000) {
			life=0;
		}
	}
}