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: frocessing test 2

Get Adobe Flash player
by uranodai 19 Dec 2008
// forked from nutsu's frocessing test 2
// forked from nutsu's forked from: code on 2008-12-17
// forked from nutsu's code on 2008-12-17
// write as3 code here..
package 
{
	import flash.display.*;
	import flash.events.*;
        import flash.filters.*;
        import flash.geom.*;
        import frocessing.core.*;
        import frocessing.math.*;
	
	[SWF(frameRate="30", backgroundColor="#000000")] 
	public class WaveSimp extends Sprite
	{
		
		private var _w:Number;
		private var _h:Number;
		private var px:Number;
		private var py:Number;
		
		private var _P:Array;
		private var _n:int = 30;

                private var fg:F5BitmapData2D;
                private var blurFilter:BlurFilter = new BlurFilter( 2 );
                private var zp:Point = new Point();

		public function WaveSimp() 
		{
			_w = stage.stageWidth;
			_h = stage.stageHeight;
			
			_P = [];
			var delta:Number = Math.PI*2/_n;
                        for( var i:int=0; i<3; i++ )
                        {
                            var pp:Array = [];
			    for ( var n:int = 0; n<_n; n++ )
				pp[n] = new WavePoint( _w/4, Math.cos(delta * n), Math.sin(delta * n) );
			    _P[i] = pp;
                        }
                        
			px = mouseX;
			py = mouseY;
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove );
			stage.addEventListener(Event.ENTER_FRAME, draw );
                        
			fg = new F5BitmapData2D(_w, _h, false, 0xff000000);
                        addChild( new Bitmap( fg.bitmapData ) );
                        fg.curveDetail(5);
                        
                        this.blendMode = BlendMode.HARDLIGHT;
		}
		
		
                
		public function draw(e:Event):void
		{
			cal(_P[0]);
                        cal(_P[1]);
                        cal(_P[2]);
                        
			fg.beginDraw();
                        fg.noStroke();
			fg.fill( 0, 0.01 );
			fg.rect( 0, 0, _w, _h );
                        
                        fg.translate(_w/2, _h/2);
                        fg.stroke(255);
                        
                        fg.rotate(-Math.PI*0.5);
                        fg.fill(255,0,0,0.2);
                        drawWave(_P[0]);
                        
                        fg.rotate(Math.PI*2/3);
                        fg.fill(0,255,0,0.2);
                        drawWave(_P[1]);
                        
                        fg.rotate( Math.PI*2/3);
                        fg.fill(0,0,255,0.2);
                        drawWave(_P[2]);
                        
                        fg.endDraw();
                        
                        fg.bitmapData.applyFilter( fg.bitmapData, fg.bitmapData.rect, zp, blurFilter );
		}
                
                private function drawWave(ps:Array):void
                {
                    fg.beginShape();
	            var wp:WavePoint = WavePoint( ps[_n - 1] );
		    fg.curveVertex( wp.x, wp.y );
		    for ( var i:int = 0; i<_n; i++ )
		    {
			wp = WavePoint( ps[i] );
			fg.curveVertex( wp.x, wp.y );
		    }
                    wp = WavePoint( ps[0] );
                    fg.curveVertex( wp.x, wp.y );
		    fg.endShape();
                }

                private function cal(ps:Array):void
		{
			var i:int;
			
			var wp0:WavePoint = WavePoint(ps[_n-1]);
			var wp:WavePoint  = WavePoint(ps[0]);
			var wp1:WavePoint = WavePoint(ps[1]);
			calAccel( wp0, wp, wp1 );
			
			wp0 = wp;
			wp  = wp1;
			for ( i = 1; i < _n-1; i++ )
			{
				wp1 = WavePoint(ps[i+1]);
				calAccel( wp0, wp, wp1 );
				wp0 = wp;
				wp  = wp1;
			}
			calAccel( wp0, wp, WavePoint(ps[0]) );
			
			for ( i = 0; i < _n; i++ )
			{
				wp = WavePoint(ps[i]);
				wp.vr += wp.a;
				wp.a *= 0.98;
			}
		}
		
		private function calAccel( wp0:WavePoint, wp:WavePoint, wp1:WavePoint ):void
		{
			var pv:Number = ( wp0.vr + wp1.vr - 4*wp.vr )*0.1;
			wp.a += pv;
			wp1.a += -pv*0.4;
			wp0.a += -pv*0.4;
		}

                private function mouseMove( e:MouseEvent ):void
		{
			px = ( mouseX - px ) * 0.15;
			py = ( mouseY - py ) * 0.15;
                        var wa:Number = FMath.mag( px , py );
			_P[0][0].a += wa;
                        _P[1][0].a += wa;
                        _P[2][0].a += wa;
			px = mouseX;
			py = mouseY;
		}
	}
}

class WavePoint 
{
	public var r:Number;
	public var vr:Number;
	public var vx:Number;
	public var vy:Number;
	public var a:Number;
	
	public function WavePoint( r:Number, vx:Number, vy:Number ) 
	{
		this.r   = r;
		this.vr  = 0;
		this.vx  = vx;
		this.vy  = vy;
		a = 0;
	}
	
	public function get x():Number
	{
		return (vr + r) * vx;
	}
	
	public function get y():Number
	{
		return (vr + r) * vy;
	}
}