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: forked from: Liquid10000

// forked from nutsu's forked from: forked from: Liquid10000
// forked from nutsu's forked from: Liquid10000
// forked from zin0086's Liquid10000
package {

    import flash.display.Stage;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.ColorTransform;
    import flash.geom.Rectangle;
    import flash.events.Event;
    import flash.events.MouseEvent;

    [SWF(width="465", height="465", backgroundColor="0x000000", frameRate="60")];
    public class Liquid extends Sprite {

        private const nums:uint = 10000;
        private var image:BitmapData;
        private var field:BitmapData;
        private var randomSeed:uint = Math.floor( Math.random() * 0xFFFF );
        private var bitmap:Bitmap;
        private var vectorList:Array;
        private var rect:Rectangle;
        private var cTra:ColorTransform;
        	
        public function Liquid() {
            initialize();
        }
        private function initialize():void {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.frameRate = 60;
            image = new BitmapData( 465, 465, false, 0x000000 );
            field = new BitmapData( 465, 465, false, 0x000000 );
            randomSeed = Math.floor( Math.random() * 0xFFFF );
            field.perlinNoise( 230, 230, 4, randomSeed, false, true, 1|2|0|0 );

            bitmap = new Bitmap( image );
            addChild( bitmap );
			
            rect = new Rectangle( 0, 0, 465, 465 )
            cTra = new ColorTransform( 0, .95, .999, .999 );
			
            vectorList = [];
            for (var i:uint = 0; i < nums; i++) {
                var px:Number = Math.random()*465;
        	var py:Number = Math.random()*465;
        	vectorList.push( new Body(px,py) );
            }
            addEventListener( Event.ENTER_FRAME, loop );
            stage.addEventListener( MouseEvent.CLICK, resetFunc );
        }
        private function loop( e:Event ):void {
            image.lock();
            image.colorTransform( rect, cTra );
            var len:uint = vectorList.length;
            for (var i:uint = 0; i < len; i++) {
                var body:Body = vectorList[i];
        	var col:Number = field.getPixel( body.px, body.py );
        	body.ax += ( (col >> 16 & 0xff)-128 ) * .0001;
        	body.ay += ( (col >> 8  & 0xff)-128 ) * .0001;
                var dx:Number = stage.mouseX - body.px;
                var dy:Number = stage.mouseY - body.py;
                var d:Number = 1.0 / (1.0 + dx * dx + dy * dy);
                body.ax += dx * d;
                body.ay += dy * d;
        	body.vx += body.ax;
        	body.vy += body.ay;
        	body.px += body.vx;
        	body.py += body.vy;

        	var _posX:Number = body.px;
        	var _posY:Number = body.py;

        	body.ax *= .975;
        	body.ay *= .975;
        	body.vx *= .92;
        	body.vy *= .92;

        	( _posX > 465 )?body.px = 0:
        	( _posX < 0 )?body.px = 465:0;
        	( _posY > 465 )?body.py = 0:
        	( _posY < 0 )?body.py = 465:0;
		
        	image.setPixel( body.px, body.py, 0x40c0ff );
            }
            image.unlock();
        }

        private function resetFunc(e:MouseEvent):void{
            randomSeed = Math.floor( Math.random() * 0xFFFF );
            field.perlinNoise( 230, 230, 4, randomSeed, false, true, 1|2|0|0 );
            vectorList = [];
        		
            for (var i:uint = 0; i < nums; i++) {
                var px:Number = Math.random()*465;
        	var py:Number = Math.random()*465;
        	vectorList.push( new Body( px, py ) );
            }
        }
    }
}

class Body {
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var ax:Number = 0;
    public var ay:Number = 0;
    public var px:Number;
    public var py:Number;
    
    function Body( px:Number, py:Number ) {
        this.px = px;
        this.py = py;
    }
}