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: Particle と Attractor の練習

[パーティクルとアトラクタの練習]
* しばらく画面を眺めていると描画されます。
* もう少し処理が軽くならないか試行錯誤中です。
* クリックでリセット。 
* 
* 参考:
* http://www.tom-carden.co.uk/p5/additive_particles4/applet/index.html
// forked from enoeno's Particle と Attractor の練習
/*
* [パーティクルとアトラクタの練習]
* しばらく画面を眺めていると描画されます。
* もう少し処理が軽くならないか試行錯誤中です。
* クリックでリセット。 
* 
* 参考:
* http://www.tom-carden.co.uk/p5/additive_particles4/applet/index.html
*/

package {
	
	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;
	
	import net.hires.debug.Stats;
	
	public class Main extends MovieClip {
		
		private var WIDTH:Number;
		private var HEIGHT:Number;
				
		private var _particle:Vector.<Particle>;
		private var _attractor:Vector.<Attractor>;
		private var _posX:Vector.<Number>;
		private var _posY:Vector.<Number>;
		
		private const NUM_PARTICLE:uint = 3500;
		private const NUM_ATTRACTOR:uint = 8;
		
		public var damp:Number = 0.00002;
		public var accel:Number = 10000;
		
		private var _screen:BitmapData;
		private var _canvas:Sprite;
		
		private var rad:Number;
		private var dRad:Number;
		private var colR:Number, colG:Number, colB:Number;
		private var rnd1:int, rnd2:int, rnd3:int;
		
		public function Main():void
		{
			init();
		}
		
		private function init():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			WIDTH = stage.stageWidth;
			HEIGHT = stage.stageHeight;
			
			_screen = new BitmapData( WIDTH, HEIGHT, false, 0x0 );
			addChild( new Bitmap(_screen) ) as Bitmap;
			
			stage.addEventListener( Event.ENTER_FRAME, update );
			stage.addEventListener( MouseEvent.CLICK, reset );
			stage.addEventListener( Event.RESIZE, resize );
//			addChild(new Stats());
			Wonderfl.capture_delay(180);
			reset();
		}
		
		private function reset( e:Event = null ):void
		{
			//_particle = [];
			//_attractor = [];
			_particle = new Vector.<Particle>(NUM_PARTICLE, true);
			_attractor = new Vector.<Attractor>(NUM_ATTRACTOR, true);
			_posX = new Vector.<Number>(NUM_PARTICLE, true);
			_posY = new Vector.<Number>(NUM_PARTICLE, true);
			
			rad = 0;
			dRad = Math.random()*0.02 + 0.03;
			
			rnd1 = Math.random()+0.5;
			rnd2 = Math.random()+0.5;
			rnd3 = Math.random()+0.5;
			
			colB = Math.floor( Math.random()*128 + 128 );
			colG = Math.floor( Math.random()*( colB * 0.5 ) + colB * 0.105 );
			colR = Math.floor( Math.random()*( colG * 0.5 ) + colG * 0.105 );
			
			_screen.fillRect( _screen.rect, 0x0 );
			
			var i:uint;
			
			// init Attractors
			for ( i = 0; i < NUM_ATTRACTOR; i++) {
				
				var a:Attractor = new Attractor();
				a.x = Math.random()*WIDTH;
				a.y = Math.random()*HEIGHT;
				
				_attractor[i] = a;
			}
			
			//init Particles
			for (i = 0; i < NUM_PARTICLE; i++) {
				var p:Particle = new Particle();
				
				p.x = Math.random()*WIDTH;
				p.y = Math.random()*HEIGHT;
				
				p.vx = -accel/2 + Math.random()*accel;
				p.vy = -accel/2 + Math.random()*accel;
				
				_particle[i] = p;
			}
		}
		
		private function resize( e:Event = null ) : void
		{
			stage.removeEventListener( Event.ENTER_FRAME, update );
			stage.removeEventListener( MouseEvent.CLICK, reset );
			stage.removeEventListener( Event.RESIZE, resize );
			init();
		}
		
		
		private function update( e:Event=null ):void
		{
			updatePos();
			
		}
		
		private function updatePos():void
		{
			var p:Particle;
			var a:Attractor;
			var d:Number;
			
			for( var i:uint = 0; i < NUM_PARTICLE; i++ ){
				
				p = _particle[i];
				
				for( var k:uint = 0; k < NUM_ATTRACTOR; k++ ){
				  
				  a = _attractor[k];
				  d = (a.x-p.x)*(a.x-p.x)+(a.y-p.y)*(a.y-p.y);
				  
				  if (d > 0.1) {
					p.vx += accel * (a.x-p.x) / d;
					p.vy += accel * (a.y-p.y) / d;
				  }
				}
				
				p.x += p.vx;
				p.y += p.vy;
				p.vx *= damp;
				p.vy *= damp;
				
				_posX[i] = p.x;
				_posY[i] = p.y;
			}
			draw();
		}
		
		private function draw():void
		{
			var dr:Number = colB + Math.cos(rad*rnd1*3)*40;
			var dg:Number = colG + Math.cos(rad*rnd2*3)*40;
			var db:Number = colR + Math.cos(rad*rnd3*3)*40;
			dr /= 15;
			dg /= 15;
			db /= 15;
			
			// drawより汚いけど・・
			_screen.lock();
			for( var i:uint = 0; i < NUM_PARTICLE; i++ )
			{
				var c : uint = addColor(_screen.getPixel(_posX[i], _posY[i]), dr, dg, db);
				_screen.setPixel(_posX[i], _posY[i], c);
			}
			_screen.unlock();
		}
		
		private function addColor(c : uint, dr : uint, dg : uint, db : uint) : uint
		{
			var r : uint = (c >> 16) + dr; if(r > 255)r = 255;
			var g : uint = ((c >> 8) & 255) + dg; if(g > 255)g = 255;
			var b : uint = (c & 255) + db; if(b > 255)b = 255;
			return r << 16 | g << 8 | b;
		}
	}
}


class Particle {
	public var x:Number;
	public var y:Number;
	public var vx:Number;
	public var vy:Number;
	
	public function Particle()
	{
		x = 0;
		y = 0;
		vx = 0;
		vy = 0;
	}
}

class Attractor
{
	public var x:Number;
	public var y:Number;
	
	public function Attractor()
	{
		x = 0;y = 0;
	}
}