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] 6日目:フォースマップに挑戦

package {
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import com.flashdynamix.utils.*;
    [SWF(frameRate=60)]
    public class FlashTest extends Sprite {
        public var particles :Vector.<Particle> = new Vector.<Particle>;
        public var pNext:Particle;
        public var canvas:BitmapData;
        public var forceMap:BitmapData;
        public var forceX:Vector.<Number> = new Vector.<Number>();
        public var forceY:Vector.<Number> = new Vector.<Number>();
        
        public function FlashTest() {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.addEventListener(MouseEvent.CLICK, onClick);
            
            W = stage.stageWidth;
            H = stage.stageHeight;
            mW = W>>1;
            mH = H>>1;

            canvas = new BitmapData( W, H, false, 0x00 );
            addChild( new Bitmap(canvas) );

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

            var forceTimer:Timer= new Timer( 4000 );
            forceTimer.addEventListener( TimerEvent.TIMER, onTimerForce );
            forceTimer.start();
            
            var timer:Timer= new Timer( 3000 );
            timer.addEventListener( TimerEvent.TIMER, onTimerSpeed );
            timer.start();
            
            addEventListener( Event.ENTER_FRAME, update );
            
            SWFProfiler.init( this );
        }
        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.1 + 0.003;
            
            colorTr.redOffset = Math.random()*40-20;
            colorTr.greenOffset = Math.random()*10-5;
            colorTr.blueOffset = Math.random()*10-5;
            
        }
        public function changeForce():void {
            var seed:Number = Math.random() * 0xFF;
            forceMap .perlinNoise(128, 128, 12, seed, false, true, channels, false );
            for( var iy:int =0; iy<mH; ++iy ) {
                for( var ix:int =0; ix<mW; ++ix ) {
                    var c:uint = forceMap.getPixel( ix, iy );
                    var pos:int = iy*mW+ix;
                    forceX[ pos ] = (c>>8&0xff)-128;
                    forceY[ pos ] = (c>>0&0xff)-128;
                }
            }
        }

        public var W:Number;
        public var H:Number;
        public var speed:Number = 0.005;
        public var drawColor:uint = 0x999933;
        public var channels:uint = BitmapDataChannel.GREEN | BitmapDataChannel.BLUE;
        public var mW:int;
        public var mH:int;
        public const colorTr:ColorTransform = new ColorTransform(1, 1, 1, 1, 2, 2, 3);
        public const OP:Point = new Point;
        public function update(e:Event):void {
            var i:int, c:uint = drawColor, l :Number = mW, w:Number =W, h:Number =H;
            var fX:Vector.<Number>=forceX, fY:Vector.<Number>=forceY;
            canvas.lock();
            canvas.colorTransform(canvas.rect, colorTr);
            for each( var p:Particle in particles ) {
                i = (p.y>>2) * l + ( p.x>>2 );
                p.vx = p.vx*0.86 + fX[i] * p.rad * speed;
                p.vy = p.vy*0.86 + 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, c );
            }
            canvas.unlock();
        }
    }
}

class Particle {
    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 var next:Particle;
    public function Particle( x:Number=0,y:Number=0 ){
        this.x = x;
        this.y = y;
    }
}