[ff]: Metaball

by bradsedito forked from forked from: Metaball (diff: 76)

♥1 | Line 128 | Modified 2013-01-17 05:18:37 | MIT License
play

ActionScript3 source code

/**
 * Copyright bradsedito ( http://wonderfl.net/user/bradsedito )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/6uJR
 */

  
//  
//  
//

package  
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BlendMode;
    import flash.display.GradientType;
    import flash.display.Graphics;
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.filters.BevelFilter;
    import flash.filters.BitmapFilterType;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.filters.BlurFilter;
    
    [SWF(backgroundColor= "0x000000", frameRate = "90")]
    
    
    public class Main extends Sprite  {
//      _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _      
        private const _NUM_CIRCLES:uint              =    15;
        private const _CIRCLE_MOVE_STEP:uint         =    06;        
        private const _CIRCLE_RADIUS:Number          =    40;
        private const _THRESHOLD:uint                =    0xffffffff;
        private const _BITMAP_SIZE_MARGIN:Number     =    100;

        private var   _circlesContainer:Sprite;           // Sprite
        private var   _circles:Vector.<Shape>;            // Vector
        private var   _circleParams:Vector.<Object>;      // Vector
        private var   _criclesBitmapData:BitmapData;      // BitmapData
        private var   _metaballsBitmap:Bitmap;            // Bitmap
        private var   _metaballsBitmapData:BitmapData;    // BitmapData
        private var   _thresholdDestPoint:Point;          // Point( 0,0 )
        private var   _thresholdSourceRect:Rectangle;     //
        private var   _bitmapDataWidth:Number;            //
        private var   _bitmapDataHeight:Number;           //
        private var   _blurr:BlurFilter;                  // 

                
        public function Main() 
        { 
            stage.quality  =  "high";
            _blurr  =  new BlurFilter( 4,4,2 );
            // ENTRY POINT:
            
            _init();
        }


        private function _init():void
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT
               
            _circlesContainer = new Sprite();
            _circlesContainer.blendMode = BlendMode.ADD;
            _circles = new Vector.<Shape>();
            _circleParams = new Vector.<Object>();
            var circle:Shape, radius:Number;
            
            for(var i:uint = 0; i< _NUM_CIRCLES; i++) {
                radius = Math.random() * _CIRCLE_RADIUS + _CIRCLE_RADIUS;
                circle = _createCircleShape(radius);
                _circles.push(circle);
                _circlesContainer.addChild(circle);
                
                circle.x = Math.random() * stage.stageWidth;
                circle.y = Math.random() * stage.stageHeight;
                _circleParams.push({ radius: radius, dx: Math.random() * _CIRCLE_MOVE_STEP - _CIRCLE_MOVE_STEP / 2, dy: Math.random() * _CIRCLE_MOVE_STEP - _CIRCLE_MOVE_STEP / 2 });
            }
            
            _thresholdDestPoint = new Point(0, 0);
            
            //BitmapData
            _resizeHandler();
            _createBitmapData();
            
            //addChild Bitmap
            _metaballsBitmap = new Bitmap(_metaballsBitmapData);
            _metaballsBitmap.filters = [ _blurr, new BevelFilter(4, 45, 0xffffff, 3, 0xcccccc, 1, 8, 8, 4, 16, BitmapFilterType.INNER, false) ]
            _metaballsBitmap.x = -(_BITMAP_SIZE_MARGIN >> 1);
            _metaballsBitmap.y = -(_BITMAP_SIZE_MARGIN >> 1);
            addChild(_metaballsBitmap);
            
            stage.addEventListener(Event.RESIZE, _resizeHandler);
            
            addEventListener(Event.ENTER_FRAME, _enterFrameHandler);
        }
        

        private function _createCircleShape(radius:Number):Shape 
        {
            var circle:Shape = new Shape();
            var circleGraphics:Graphics;
            var matrix:Matrix = new Matrix();
            matrix.createGradientBox(radius * 2, radius * 2, 0, -radius, -radius);
            circleGraphics = circle.graphics;
            circleGraphics.beginGradientFill(GradientType.RADIAL, [ 0xffffff, 0x000000 ], [ 1, 0 ], [ 127, 255 ], matrix);
            circleGraphics.drawCircle(0, 0, radius);
            circleGraphics.endFill();
            circle.blendMode = BlendMode.ADD;
            
            return circle;
        }
        
        //BitmapData
        private function _createBitmapData():void 
        {
            if(_criclesBitmapData) _criclesBitmapData.dispose();
            _criclesBitmapData = new BitmapData(_bitmapDataWidth, _bitmapDataHeight, true, 0x000000);
            _criclesBitmapData.draw(_circlesContainer);
            
            if(_metaballsBitmapData) _metaballsBitmapData.dispose();
            _metaballsBitmapData = new BitmapData(_bitmapDataWidth, _bitmapDataHeight, true, 0x000000);
            _metaballsBitmapData.fillRect(_thresholdSourceRect, 0xff000000);
            _metaballsBitmapData.threshold(_criclesBitmapData, _thresholdSourceRect, _thresholdDestPoint, "<", _THRESHOLD);
        }
        
        
        private function _enterFrameHandler(e:Event = null):void 
        {
            var circle:Shape; 
            var circleParam:Object;
            var radius:Number;

            for(  var i:uint=0;  i<_NUM_CIRCLES;  i++  ) 
            {
                circle = _circles[i];
                circleParam = _circleParams[i];
                radius = circleParam.radius;
                
                circle.x += circleParam.dx;
                if(circle.x > _bitmapDataWidth + radius || circle.x < -radius) 
                {
                    _circleParams[i].dx *= -1;                  
                }
                
                circle.y  +=  (circleParam.dy);// * (1.60);
                if(circle.y > _bitmapDataHeight + radius || circle.y < -radius)
                {
                    _circleParams[i].dy *= -1;
                }
            }
            
            _createBitmapData();
            _metaballsBitmap.bitmapData = _metaballsBitmapData;
        }
        
        
        private function _resizeHandler(e:Event = null):void 
        {
            _bitmapDataWidth = stage.stageWidth + _BITMAP_SIZE_MARGIN;
            _bitmapDataHeight = stage.stageHeight + _BITMAP_SIZE_MARGIN;
            _thresholdSourceRect = new Rectangle(0, 0, _bitmapDataWidth, _bitmapDataHeight);
        }
    }
}