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: Particle flow

Particle flow
by Mr.doob (http://mrdoob.com)

Click for a new one

@author mrdoob
// forked from Altschuler's forked from: Particle flow
// forked from mrdoob's Particle flow
/*
 * Particle flow
 * by Mr.doob (http://mrdoob.com)
 * 
 * Click for a new one
 */

package  
{
    import flash.display.StageAlign;    
    import flash.display.StageScaleMode;    
    import flash.display.StageQuality;    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.ColorTransform;    

    /**
     * @author mrdoob
     */
    [SWF(width="512", height="512", backgroundColor="#ffffff", frameRate="60")]
    public class Main extends Sprite 
    {
        private var map : BitmapData;
        private var canvas : BitmapData;
        private var particles :Vector.<Particle>;
        
        private var particles_amount : int = 5000;

        public function Main()
        {
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }
        
        private function onAddedToStage(e : Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.quality = StageQuality.LOW;
            
            map = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0x000000);

            canvas = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0x000000);
            
            addChild( new Bitmap(canvas) );

            particles = new Vector.<Particle>();
            
            for (var i : int = 0; i < particles_amount; i++)
                particles[i] = new Particle();
            
            onClick(null);
            
            stage.addEventListener( MouseEvent.CLICK, onClick );
            addEventListener( Event.ENTER_FRAME, onEnterFrame );
        }
        
        private function onEnterFrame( e : Event ) : void
        {
            canvas.lock();
            
            //canvas.fillRect(canvas.rect, 0x000000);
            
            for (var i : uint = 0; i < particles_amount; i++)
            {
                var direction : uint = map.getPixel(particles[i].x, particles[i].y);

                particles[i].x += ((direction & 0xff) * 0.05625) - 2;
                particles[i].y += ((direction >> 8 & 0xff) * 0.05625) - 2;
                
                particles[i].x < 0 ? particles[i].x += canvas.width : particles[i].x > canvas.width ? particles[i].x -= canvas.width : null;
                particles[i].y < 0 ? particles[i].y += canvas.height : particles[i].y > canvas.height ? particles[i].y -= canvas.height : null;
                
                canvas.setPixel(particles[i].x, particles[i].y, particles[i].color );
            }
            
            canvas.unlock();
            canvas.colorTransform( canvas.rect, new ColorTransform( 1, 1, 1, 1, -3, -3, -3 ) );
        }
        
        private function onClick( e : MouseEvent ) : void
        {
            for (var i : int = 0; i < particles_amount; i++)
            {
                particles[i].x = Math.random() * canvas.width;
                particles[i].y = Math.random() * canvas.height;
                particles[i].color = Math.random() * 0xffff + 0xff0000;
            }
            
            map.perlinNoise(Math.random() * 200, Math.random() * 200, 5, Math.random() * 1000, true, true);
        }
    }
}

class Particle 
{
    public var x : Number = 0;
    public var y : Number = 0;
    public var color : int = 0;
}