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

[1日1Wonderfl]7日目:DrawingArtぽくしてみる

// forked from yd_niku's forked from: forked from: [1日1Wonderfl] 6日目:フォースマップに挑戦
// forked from yd_niku's forked from: [1日1Wonderfl] 6日目:フォースマップに挑戦
// forked from yd_niku's [1日1Wonderfl] 6日目:フォースマップに挑戦
package {
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import com.flashdynamix.utils.*;
    [SWF(frameRate=180)]
    public class FlashTest extends Sprite {
        public var particles :Vector.<Particle> = new Vector.<Particle>;
        public var canvas:BitmapData;
        public var forceMap:BitmapData;
        public var forceX:Vector.<Number>;
        public var forceY:Vector.<Number>;
        
        public function FlashTest() {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.addEventListener(MouseEvent.CLICK, onClick);
            
            bmp = addChild( new Bitmap() ) as Bitmap;
            getSize();

            var length:int = 8000;
            var p:Particle, sx:Number, sy:Number;
            while( length-->0 ) {
                sx = Math.random() *W;
                sy = Math.random() *H;
                p = new Particle( sx , sy );
                particles .push( p );
            }
            forceMap = new BitmapData( mW, mH, false );
            changeForce();
            //changeSpeed();

            var forceTimer:Timer= new Timer( 5000 );
            forceTimer.addEventListener( TimerEvent.TIMER, onTimerForce );
            forceTimer.start();
            
            var timer:Timer= new Timer( 2500 );
            timer.addEventListener( TimerEvent.TIMER, onTimerSpeed );
            timer.start();
            
            addEventListener( Event.ENTER_FRAME, update );
            stage.addEventListener( Event.RESIZE, function(e:Event):void {getSize();} );

            SWFProfiler.init( this );
        }
        public var bmp:Bitmap;
        public function getSize():void {
            W = stage.stageWidth;
            H = stage.stageHeight;
            mW = W>>1;
            mH = H>>1;
            if( canvas !=null ) canvas.dispose();
            canvas = new BitmapData( W, H, false, 0x00 );
            bmp.bitmapData = canvas;
        }
        public function onClick( e:Event ):void {
           changeForce(); 
        }
        public function onTimerForce ( e:Event ):void {
           changeForce(); 
        }
        public function onTimerSpeed ( e:Event ):void {
           changeSpeed(); 
        }
        public function changeSpeed():void{
            speed = Math.random()* 0.0001 + 0.0008;
            
            colorTr.redOffset = -Math.random()*12-6;
            colorTr.greenOffset = -Math.random()*3;
            colorTr.blueOffset = -Math.random()*6;
            
        }
        public function changeForce():void {
            var c:uint, pos:int;
            forceX = null; forceX = new Vector.<Number>();
            forceY = null; forceY = new Vector.<Number>();
            var seed:Number = Math.random() * 0xFF;
            forceMap .perlinNoise( 128, 128, 4, seed, true, true, channels );
            for( var iy:int =0; iy<mH; ++iy ) {
                for( var ix:int =0; ix<mW; ++ix ) {
                    c = forceMap.getPixel( ix, iy );
                    pos = iy*mW+ix;
                    forceX[ pos ] = (c>>8&0xff)-128;
                    forceY[ pos ] = (c>>0&0xff)-128;
                }
            }
            for each( var p:Particle in particles ) {
                p.vx *= p.rad*8;
                p.vy *= p.rad*8;
                p.color = p.rad*0x99+0x66<<16 | p.rad*0x99+0x33<<8 | p.rad*0x66+0x99>>0;
            }
        }

        public var W:Number;
        public var H:Number;
        public var mW:int;
        public var mH:int;
        public var speed:Number = 0.001;
        public var channels:uint = BitmapDataChannel.GREEN | BitmapDataChannel.BLUE;
        public const colorTr:ColorTransform = new ColorTransform(1.001, 1.0, 1.002, 1, -12, -12, -1 );
        public const OP:Point = new Point;
        public var cnt:int=0;
        public function update(e:Event):void {
            var i:int, l :Number = mW, w:Number =W, h:Number =H;
            var fX:Vector.<Number>=forceX, fY:Vector.<Number>=forceY;
            var friction:Number = 0.99;
            canvas.lock();
            if( cnt++ % 4 == 0 ) canvas.colorTransform(canvas.rect, colorTr);
            for each( var p:Particle in particles ) {
                i = (p.y>>2) * l + ( p.x>>2 ) || 0;
                p.vx = p.vx*friction+ fX[i] * p.rad * speed;
                p.vy = p.vy*friction+ fY[i] * p.rad * speed;
 
                p.x += p.vx;
                p.y += p.vy;
                
                if ( p.x > w ) p.x -= w;
                else if ( p.x < 0 ) p.x += w;
                if (  p.y > h  ) p.y -= h;
                else if ( p.y < 0 ) p.y += h;
                canvas.setPixel( p.x>>0, p.y>>0, p.color );
            }
            canvas.unlock();
        }
    }
}

class Particle {
    public var color:uint=0xFFFFFF;
    public var x:Number =0;
    public var y:Number =0;
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var rad:Number = Math.random();
    public function Particle( x:Number=0,y:Number=0 ){
        this.x = x;
        this.y = y;
    }
}