Fl033_Main.as (Main)

Fl033_Main.as
package {
    import flash.display.Sprite;
    import com.borealkiss.controls.ViewController;
    import com.borealkiss.controls.HairyFontController;

    [SWF(width="550",height="400",backgroundColor="0x0")]
    public class Fl033_Main extends Sprite{
        public function Fl033_Main(){
            var canvas:Sprite = new Sprite();
            this.addChild(canvas);
            
            var controller:ViewController = new HairyFontController(canvas);
            controller.draw();
        }
    }
}

com.borealkiss.controls.ViewController.as (Abstract)

ViewController.as
/**
 * @date 21/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.controls{
    import flash.display.DisplayObjectContainer;
    import flash.errors.IllegalOperationError;
    
    /**
     * Abstract class
     */ 
    public class ViewController{
        protected var _target:DisplayObjectContainer;
        
        /**
         * Constructor
         */     
        public function ViewController(target:DisplayObjectContainer){
            _target = target;
            
            if (Object(this) == ViewController){
                throw new IllegalOperationError(
                    "Abstract class must be inherited in a subclass");
            }
        }
        
        public function draw():void{
            //Must be overridden in a subclass.
        }
    }
}

com.borealkiss.controls.HairyFontController.as

HairyFontController.as
/**
 * @date 23/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.controls{
    import flash.display.DisplayObjectContainer;
    import com.borealkiss.display.HairyFont;
    import com.borealkiss.display.FontBitmapData;
    import com.borealkiss.controls.ViewController;
    
    public class HairyFontController extends ViewController{
        protected const STAGE_WIDTH:Number = 550;
        protected const STAGE_HEIGHT:Number = 400;
        protected const LIMIT:uint = 100000;
        
        /**
         * Constructor
         */ 
        public function HairyFontController(target:DisplayObjectContainer){
            super(target);
        }
        
        override public function draw():void{
            var bitmapData:FontBitmapData = new FontBitmapData("Happy \nBirthday!!",
                                    STAGE_WIDTH,STAGE_HEIGHT,"Times New Roman",130,0xFFFFFF);
            var font:HairyFont = new HairyFont(bitmapData,LIMIT,10,0xFFFFFF);
            _target.addChild(font);
            font.y = STAGE_HEIGHT/2 - font.height/2;
        }
    }
}

com.borealkiss.display.HairyFont.as

HairyFont.as
/**
 * @date 23/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.Sprite;
    import flash.display.Graphics;
    import flash.display.BitmapData;
    import com.borealkiss.display.FontBitmapData;
    
    public class HairyFont extends Sprite{
        protected var _sourceBitmapData:FontBitmapData;
        protected var _random:uint;
        protected var _stroke:uint;
        protected var _limit:uint;
        protected const ALPHA:Number = 0.1;
        
        /**
         * Constructor
         */ 
        public function HairyFont(sourceBitmapData:FontBitmapData,
                                limit:uint,random:uint,stroke:uint=0x0){
                                    
            _sourceBitmapData = sourceBitmapData;
            _limit = limit;
            _random = random;
            _stroke = stroke;
            this.init();
        }
        
        protected function init():void{
            this.update();
        }
        
        protected function update():void{
            var rectWidth:int = _sourceBitmapData.width;
            var rectHeight:int = _sourceBitmapData.height;
            
            var g:Graphics = this.graphics;
            g.clear();
            g.lineStyle(0,_stroke,ALPHA);
            
            for (var i:int=0; i<_limit; i++){
                var x:int = int(Math.random()*rectWidth);
                var y:int = int(Math.random()*rectHeight);
                var color32:uint = _sourceBitmapData.getPixel32(x,y);
                
                if (isInFont(color32)){
                    //Start point
                    var startX:Number = x;
                    var startY:Number = y;
                    
                    //Anchor point so that the hair goes outer.
                    var hairRad:Number = _random*Math.random();
                    var hairTheta:Number = Math.random()*2*Math.PI;
                    var anchorX:Number = startX + hairRad*Math.cos(hairTheta);
                    var anchorY:Number = startY + hairRad*Math.sin(hairTheta);
                    
                    //Control point for the Bezier curve.
                    var controlRad:Number = _random*Math.random();
                    var controlTheta:Number = Math.random()*2*Math.PI;
                    var controlX:Number = startX + controlRad*Math.cos(controlTheta);
                    var controlY:Number = startY + controlRad*Math.sin(controlTheta);
                    
                    g.moveTo(startX,startY);
                    g.curveTo(controlX,controlY,anchorX,anchorY);
                }
            }
        }
        
        /**
         * Check the pixel color.
         * 
         * @param color32 Pixel value in a bitmap data including the alpha channel.
         * @return true if the pixel color is the same as that of the font color, otherwise false.
         */ 
        protected function isInFont(color32:uint):Boolean{
            var color24:uint = color32 & 0xFFFFFF;
            if (color24 == _sourceBitmapData.fontColor){
                return true;
            }
            return false;
        }
        
        /**
         * @return A random value between -random and random.
         */ 
        protected function randomGen(random:uint):Number{
            return 2*random*(Math.random() - 1/2);
        }
    }
}

com.borealkiss.display.FontBitmapData.as

FontBitmapData.as
/**
 * @date 23/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.BitmapData;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    
    public class FontBitmapData extends BitmapData{
        protected var _textField:TextField;
        protected var _textFormat:TextFormat;
        
        protected var _fontColor:uint;
        public function get fontColor():uint{
            return _fontColor;
        }
        
        /**
         * Constructor
         */ 
        public function FontBitmapData(text:String,width:int,height:int,
                                        font:String="Times New Roman",
                                        size:Object=12,
                                        color:Object=0x0,
                                        bold:Object=null,
                                        italic:Object=null){
                                    
            super(width,height,true,0x00FFFFFF);
            
            _textField = new TextField();
            _textField.text = text;
            _textField.width = width;
            _textField.height = height;
            _fontColor = uint(color);
            
            _textFormat = new TextFormat(font,size,color,bold,italic);
            _textFormat.align = TextFormatAlign.CENTER;
            _textField.setTextFormat(_textFormat);
            this.init();                        
        }
        
        protected function init():void{
            this.draw(_textField);
        }
    }
}
Powered by boreal-kiss.com 2008.