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

flood-fill with bitmap

flood-fill example
/**
 * Copyright ChangHun.Kim ( http://wonderfl.net/user/ChangHun.Kim )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9QDq
 */

package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.Stage;
    import flash.display.Sprite;
    
    /**
     * Flood Fill Example
     * @author CHKim
     */    
    public class Main extends Sprite 
    {
        private var _bitmapData:BitmapData;
        private var _bitmap:Bitmap;
        private var _mc:MovieClip;
        private var CLEAR_COLOR:uint     = 0xFFFFFFFF;
        private var FILL_COLOR:uint     = 0xFF0000FF;
        
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            stage.scaleMode = "noScale";
            removeEventListener(Event.ADDED_TO_STAGE, init);

            // entry point
            _bitmapData = new BitmapData(32, 32, false, CLEAR_COLOR);
            _bitmap = new Bitmap(_bitmapData, "auto", false);
            _bitmap.width = stage.stageWidth;
            _bitmap.height = stage.stageHeight;
            
            setRandomPixels(_bitmapData);
            
            _mc = new MovieClip();
            _mc.addChild(_bitmap);            
            _mc.addEventListener(MouseEvent.CLICK, onMouseClick);        
            
            this.addChild(_mc);
        }
                
        private function setRandomPixels(bd:BitmapData):void
        {
            var s:int = bd.width * bd.height;
            var c:int = int(s * 0.5);

            for (var i:int = 0; i < c; ++i)
            {
                var x:int = Math.random() * bd.width;
                var y:int = Math.random() * bd.height;
                
                bd.setPixel32(x, y, 0);
            }
        }
        
        private function onMouseClick(e:MouseEvent):void
        {
            var dx:Number = _mc.width / _bitmapData.width;
            var dy:Number = _mc.height / _bitmapData.height;

            floodFill(e.localX / dx, e.localY / dy, FILL_COLOR);
        }
        
        private function floodFill(x:int, y:int, color:uint):void
        {    
            if ( x < 0 || y < 0 ) return;
            if ( x >= _bitmapData.width || y >= _bitmapData.height ) return;
            if (_bitmapData.getPixel32(x, y) != CLEAR_COLOR ) return;
            
            _bitmapData.setPixel32(x, y, color);

            floodFill(x + 1, y, color);
            floodFill(x - 1, y, color);
            floodFill(x, y + 1, color);
            floodFill(x, y - 1, color);    
        }
    }
}