Beautifl - Flash Gallery

Thumbnail : Egg laying of Coral
Egg laying of Coral
yd_niku 2009-10-14 MIT License

再生するにはFlash Playerが必要です。デスクトップのブラウザでご覧ください。

// forked from yd_niku's forked from: forked from: flash on 2009-10-6
// forked from yd_niku's forked from: flash on 2009-10-6
// forked from yd_niku's flash on 2009-10-6
package {
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.net.*;
    import flash.geom.*;
    import flash.system.*;
    import flash.utils.*;
    [SWF(frameRate=60)]
    public class FlashTest extends Sprite {
        public function FlashTest() {
            if( stage ) init();
            else addEventListener( Event.ADDED_TO_STAGE, init );
        }
        
        private var _canvas:BitmapData;
        private var _source:BitmapData;
        private var _map:BitmapData;
        
        private function init(e:Event=null):void {
            removeEventListener( Event.ADDED_TO_STAGE, init );
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.addEventListener( Event.RESIZE, resize );
            resize();
            
            _source= new BitmapData(rect.width,rect.height,false,0);
            
            _canvas = _source.clone();
            addChild( new Bitmap( _canvas ) );
            
            _map = _source.clone();
            var mapdisplay:DisplayObject = addChild( new Bitmap(_map) );
            mapdisplay.alpha = 0.5;
            mapdisplay.blendMode = BlendMode.ADD;
            
            buildForceMap();
            
            addEventListener( Event.ENTER_FRAME, update );
            update();
            
            addEventListener( Event.ENTER_FRAME, emitter );
            emitter();
            
            var timer:Timer = new Timer(10000);
            timer.addEventListener( TimerEvent.TIMER, updateTimer );
            timer.start();
        }
        private const PARTICLE_LENGTH:int = 15000;
        private var _emitterCount:int = 0;
        private function emitter( e:Event=null ):void {
            if( _emitterCount++ % 3 != 0 ) return;
            
            var angle:Number, radius:Number;
            for( var i:int = 0; i<80; ++i ) {
                var p :Particle = new Particle;
                p.x = (rect.width/2) + ( Math.random() - 0.5 ) *6;
                p.y = rect.height - 20 * Math.random();
                angle =( Math.random() - 0.5 ) * 12- 60 - _emitterCount/10;
                radius = Math.random() * 2 + 1;
                p.vx = Math.cos(angle/180*Math.PI) * radius ;
                p.vy = Math.sin(angle/180*Math.PI) * radius ;
                _particles.push( p );
            }
             
            if( _particles.length >= PARTICLE_LENGTH )
                removeEventListener(  Event.ENTER_FRAME, emitter );
        }
        
        private function updateTimer( e:TimerEvent ):void {
            buildForceMap();
        }
        
        private function buildForceMap():void {
            _map.perlinNoise( 256, 256, 4, Math.random()*0xff>>0, true, true, 5 );
            for( var xx:int=0; xx<=rect.width; xx++ ) {
                _forceMap[xx] = new Vector.<Point>;
                for( var yy:int=0; yy<=rect.height; yy++ ) {
                    var c:uint = _map.getPixel( xx, yy );
                    var r:uint = (c>>16) & 0xff;
                    var g:uint = (c>>8) & 0xff;
                    var b:uint = c& 0xff;
                    _forceMap[xx][yy] = new Point( (r-128) / 256, (b-128) / 256 );
                }
            }    
        }
        
        private var _windDrag:Point = new Point( 0.998, 0.998 );
        private var _particles:Vector.<Particle> = new Vector.<Particle>();
        private var _forceMap:Vector.<Vector.<Point>> = new Vector.<Vector.<Point>>;
        
        private var _ctf:ColorTransform = new ColorTransform(0.98, 0.997, 0.995, 0.98 );
        private var _blur:BlurFilter= new BlurFilter(16,4,2);
        
        private function update(e:Event = null):void {
            _canvas.lock();
            _canvas.colorTransform( _canvas.rect, _ctf );
            //_blur.blurY = Math.pow( 2, int(Math.random()*3) +1 );
            _canvas.applyFilter( _canvas, rect, distPoint, _blur );
            for each( var p:Particle in _particles ) {
                if( p.life < 0 ) continue;
                var force:Point = _forceMap[p.x>>0][p.y>>0];
                //catch(e:Error) { continue; }
                
                p.ax *= 0.3;
                p.ay *= 0.3;
                
                p.ax += force.x * 0.02;
                p.ay += force.y * 0.02;
                
                p.vx += p.ax;
                p.vy += p.ay;
                
                p.vx *= _windDrag.x;
                p.vy *= _windDrag.y;
                
                p.x += p.vx;
                p.y += p.vy;
                
                p.life++;
                 
                if( p.x < 0 ) p.x = rect.width;
                else if( p.x > rect.width ) p.x = 0;
                if( p.y < 0 ) p.y = rect.height;
                else if( p.y > rect.height ) p.y = 0;
                
                _canvas.setPixel( p.x, p.y, 0xFFFFFF );
            }
            _canvas.unlock();
        }
        
        public var distPoint:Point = new Point;
        public var rect:Rectangle = new Rectangle();
        public var center:Point = new Point();
        private function resize(e:Event=null):void{
            rect.width = stage.stageWidth;
            rect.height= stage.stageHeight;
            
            center.x=  rect.width/2>>0;
            center.y = rect.height/2>>0;
        }
    }
}

class Particle {
    public var x:Number = 0;
    public var y:Number = 0;
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var ax:Number = 0;
    public var ay:Number = 0;
    public var life:Number = 100;
}