Fl035_Main.as (Main)

Fl035_Main.as
package {
    import flash.display.Sprite;
    import com.borealkiss.controls.ViewController;
    import com.borealkiss.controls.FontController;

    [SWF(width="550",height="400",backgroundColor="0x0")]
    public class Fl035_Main extends Sprite{
        public function Fl035_Main(){
            var canvas:Sprite = new Sprite();
            this.addChild(canvas);
            var controller:ViewController = new FontController(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.FontController.as

FontController.as
/**
 * @date 24/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.controls{
    import flash.display.DisplayObjectContainer;
    import com.borealkiss.controls.ViewController;
    import com.borealkiss.controls.FontController;
    import com.borealkiss.display.FontBitmapData;
    import com.borealkiss.display.Font;
    import com.borealkiss.display.CircleFont;
    import com.borealkiss.display.FilledCircleFont;
    import com.borealkiss.display.ShadowedCircleFont;
    
    public class FontController extends ViewController{
        protected const STAGE_WIDTH:Number = 550;
        protected const STAGE_HEIGHT:Number = 400;
        protected const LIMIT:uint = 10000;
        protected const FONT_STYLE:String = "Times New Roman";
        
        /**
         * Constructor
         */ 
        public function FontController(target:DisplayObjectContainer){
            super(target);
        }
        
        override public function draw():void{
            var text:String = "ActionScript";
            var sourceBitmapData:FontBitmapData = new FontBitmapData(text,
                                    STAGE_WIDTH,200,FONT_STYLE,100,0xFFFFFF);
            
            //1st image
            var font1:Font = new CircleFont(sourceBitmapData,LIMIT,5,0xFFFFFF,0.1);
            _target.addChild(font1);
            font1.y = 0;
            
            //2nd image
            var font2:Font = new FilledCircleFont(sourceBitmapData,LIMIT,5,0x0,0.5);
            _target.addChild(font2);
            font2.y = 130;
            
            //3rd image
            var font3:Font = new ShadowedCircleFont(sourceBitmapData,LIMIT,5,0xFFFFFF,0.9);
            _target.addChild(font3);
            font3.y = 260;
        }
    }
}

com.borealkiss.display.Font.as (Abstract)

Font.as
/**
 * @date 24/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.errors.IllegalOperationError;
    import flash.display.Sprite;
    import com.borealkiss.display.FontBitmapData;
    
    /**
     * Abstract class.
     */ 
    public class Font extends Sprite{
        protected var _sourceBitmapData:FontBitmapData;
        protected var _limit:uint;
        protected var _random:uint;
        protected var _stroke:uint;
        protected var _alpha:Number;
        
        /**
         * Constructor
         */ 
        public function Font(sourceBitmapData:FontBitmapData,
                                limit:uint=100,random:uint=10,stroke:uint=0x0,alpha:Number=0.1){
            
            if (Object(this) == Font){
                throw new IllegalOperationError(
                    "Abstract class must be inherited in a subclass");
            }
            
            _sourceBitmapData = sourceBitmapData;
            _limit = limit;
            _random = random;
            _stroke = stroke;
            _alpha = alpha;                    
            this.draw();
        }
        
        protected function draw():void{
            //Must be overridden in a subclass.
        }
        
        /**
         * @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.CircleFont.as

CircleFont.as
/**
 * @date 24/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.Graphics;
    import com.borealkiss.display.FontBitmapData;
    import com.borealkiss.display.Font;
    import com.borealkiss.display.shapes.Circle;
    
    /**
     * Fill letters with circles.
     */ 
    public class CircleFont extends Font{
        
        /**
         * Constructor
         */ 
        public function CircleFont(sourceBitmapData:FontBitmapData, 
                    limit:uint=100,random:uint=10,stroke:uint=0x0,alpha:Number=0.1){
                        
            super(sourceBitmapData,limit,random,stroke,alpha);
        }
        
        override protected function draw():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 (this.isInFont(color32)){
                    var circle:Circle = new Circle(_random,_stroke,_alpha);
                    circle.x = x;
                    circle.y = y;
                    this.addChild(circle);
                }
            }
        }
    }
}

com.borealkiss.display.FilledCircleFont.as

FilledCircleFont.as
/**
 * @date 24/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.Graphics;
    import com.borealkiss.display.FontBitmapData;
    import com.borealkiss.display.Font;
    import com.borealkiss.display.shapes.Circle;
    
    /**
     * Fill letters with filled-circles.
     */
    public class FilledCircleFont extends Font{
        
        /**
         * Constructor
         */
        public function FilledCircleFont(sourceBitmapData:FontBitmapData, 
            limit:uint=100,random:uint=10,stroke:uint=0x0,alpha:Number=0.1){
            
            super(sourceBitmapData,limit,random,stroke,alpha);
        }
        
        override protected function draw():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 (this.isInFont(color32)){
                    var circle:Circle = new Circle(_random,_stroke,_alpha,
                                                true,0xFFFFFF*Math.random());
                    circle.x = x;
                    circle.y = y;
                    this.addChild(circle);
                }
            }
        }
    }
}

com.borealkiss.display.ShadowedCircleFont.as

ShadowedCircleFont.as
/**
 * @date 24/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.Graphics;
    import flash.filters.DropShadowFilter;
    import com.borealkiss.display.FontBitmapData;
    import com.borealkiss.display.Font;
    import com.borealkiss.display.shapes.Circle;
    
    /**
     * Fill letters with shadowed circles.
     */
    public class ShadowedCircleFont extends Font{
        
        /**
         * Constructor
         */
        public function ShadowedCircleFont(sourceBitmapData:FontBitmapData, 
            limit:uint=100,random:uint=10,stroke:uint=0x0,alpha:Number=0.1){
            
            super(sourceBitmapData,limit,random,stroke,alpha);
        }
        
        override protected function draw():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 (this.isInFont(color32)){
                    var circle:Circle = new Circle(_random,_stroke,_alpha,true,0xFFFFFF);
                    circle.x = x;
                    circle.y = y;
                    this.addChild(circle);
                    circle.filters = [this.dropShadowFilter()];
                }
            }
        }
        
        protected function dropShadowFilter():DropShadowFilter{
            var shadow:DropShadowFilter = new DropShadowFilter();
            shadow.alpha = 0.75;
            shadow.blurX = 10;
            shadow.blurY = 10;
            shadow.color = 0xFFFFFF;
            shadow.distance = 0;
            return shadow;
        }
    }
}

com.borealkiss.display.shapes.Circle.as

Circle.as
/**
 * @date 24/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display.shapes{
    import flash.display.Sprite;
    import flash.display.Graphics;
    
    public class Circle extends Sprite{
        public function Circle(radius:Number,stroke:uint=0x0,alpha:Number=0.1,
                                filling:Boolean=false,fillColor:uint=0x0){
                                    
            var g:Graphics = this.graphics;
            g.lineStyle(0,stroke,alpha);
            
            if (filling){
                g.beginFill(fillColor,alpha);
            }
            
            g.drawCircle(0,0,radius);
        }
    }
}

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.