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

Version of particle

Get Adobe Flash player
by bennett.yeates 16 Apr 2014
    Embed
/**
 * Copyright bennett.yeates ( http://wonderfl.net/user/bennett.yeates )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/fcZ4
 */

package {
    import flash.events.MouseEvent;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Point;
    
    [SWF(backgroundColor=0)]
    public class PixelRings extends Sprite {
        
        private var buffer:BitmapData;
        private var pixels:Vector.<Object>;
        private var ref:Point;
        private var currentRefDegree:int;
        
        private const BURST_MULTIPLIER:int = 2;
        private const SPEED:int = 3;
        private const NUM_PIXELS:int = 3000;
        private const COLORS:Array = [ 0xFFFFFF, 0xFF0000, 0x00FFFF ];
        private const RADIUS:int = 150;
        
        public function PixelRings() {
            ref = new Point( stage.stageWidth/2, stage.stageHeight/2 );
            pixels = new Vector.<Object>;
            
            buffer = new BitmapData( 465, 465, false, 0 );
            addChild( new Bitmap( buffer ) );
            
            var i:int;
            var idx:int;
            var color:uint = 0xFFFFFF;
            for ( ; i < NUM_PIXELS; ++i ) {
                var pixel:Object = {
                    x: Math.random() * 465,
                    y: Math.random() * 465,
                    vx:0,
                    vy:0,
                    color: color
                };
                if ( i > 0 && i % 1000 == 0 ) {
                    ++idx;
                    color = COLORS[ idx ];
                }

                pixels.push( pixel );
            }
            stage.addEventListener( MouseEvent.CLICK, onClick );
            addEventListener( Event.ENTER_FRAME, oef );
        }
        
        private function onClick( e:MouseEvent ):void {
            var i:int;
            var degree:Number = NUM_PIXELS / 360;
            var inc:Number = degree;
            var burstInc:int = 1;
            for ( ; i < NUM_PIXELS; ++i ) {
                var burst:Number = BURST_MULTIPLIER * ( burstInc * 8 );
                var pixel:Object = pixels[ i ];
                pixel.vx += burst * Math.cos( degree );
                pixel.vy += burst * Math.sin( degree );
                degree += inc;
                burstInc = i % 1000 == 0 ? burstInc + 1 : burstInc;
            }
        }

        
        private function oef( e:Event ):void {
            buffer.lock();
            buffer.fillRect( buffer.rect, 0 );
            currentRefDegree += SPEED;    
            ref.x = stage.stageWidth/2 + RADIUS * Math.cos( currentRefDegree * Math.PI / 180 );
            ref.y = stage.stageHeight/2 + RADIUS * Math.sin( currentRefDegree * Math.PI / 180 );
            
            var i:int;
            var burstInc:Number = 1;
            for ( ; i < NUM_PIXELS; ++i ) {
                var pixel:Object = pixels[ i ];
                var angle:Number = Math.atan2( pixel.y - ref.y, pixel.x - ref.x );
                pixel.vx -= ( BURST_MULTIPLIER * burstInc ) * Math.cos( angle );
                pixel.vy -= ( BURST_MULTIPLIER * burstInc ) * Math.sin( angle );
                
                pixel.x += pixel.vx;
                pixel.y += pixel.vy;
                
                pixel.vx *= 0.95;
                pixel.vy *= 0.95;
                
                buffer.setPixel( pixel.x, pixel.y, pixel.color );
                burstInc = i % 1000 == 0 ? burstInc + 0.25 : burstInc;
            }
            buffer.unlock();
        }

    }
}