Fl032_Main.as (Main)

Fl032_Main.as
package {
    import flash.display.Sprite;
    import com.borealkiss.controls.ViewController;
    import com.borealkiss.controls.HairyEllipseController;

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

com.borealkiss.controls.HiaryEllipseController.as

HairyEllipseController.as
/**
 * @date 22/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.controls{
    import flash.display.DisplayObjectContainer;
    import com.borealkiss.display.HairyEllipse;
    import com.borealkiss.controls.ViewController;

    public class HairyEllipseController extends ViewController{
        protected const STAGE_WIDTH:Number = 550;
        protected const STAGE_HEIGHT:Number = 400;
        protected const PADDING_X:Number = 10;
        protected const PADDING_Y:Number = 10;
        protected const SPAN_X:Number = 10;
        protected const SPAN_Y:Number = 10;
        protected const LIMIT_X:uint = 100;
        protected const LIMIT_Y:uint = 3;
        protected const WIDTH:Number = (STAGE_WIDTH - 2*PADDING_X)/Number(LIMIT_X) - SPAN_X;
        protected const HEIGHT:Number = (STAGE_HEIGHT - 2*PADDING_Y)/Number(LIMIT_Y) - SPAN_Y;
        
        /**
         * Constructor
         */ 
        public function HairyEllipseController(target:DisplayObjectContainer){
            super(target);
        }
        
        override public function draw():void{
            for (var intY:int=0; intY<LIMIT_Y; intY++){
                var thickness:Number = 0.5;
                var radius:Number = 0;
                var amp:Number = 5;
                var angle:Number = 360*Math.random();
                var hairLength:uint = (intY + 1)*5;
                
                for (var intX:int=0; intX<LIMIT_X; intX++){
                    var circle:HairyEllipse = new HairyEllipse(radius/2,radius,hairLength,0xFFFFFF);
                    circle.x = PADDING_X + (2*intX + 1)*(WIDTH/2 + SPAN_X/2);
                    circle.y = PADDING_Y + (2*intY + 1)*(HEIGHT/2 + SPAN_Y/2) + 
                                                            amp*Math.sin(angle*Math.PI/180);
                    _target.addChild(circle);
                    
                    if (intX == int(LIMIT_X/2)){
                        thickness *= -1;
                    }
                    
                    radius += thickness;
                    angle += 15;
                }
            }
        }
    }
}

com.borealkiss.display.HiaryEllipse.as

HairyEllipse.as
/**
 * @date 22/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.Sprite;
    import flash.display.Graphics;
    
    public class HairyEllipse extends Sprite{
        protected var _radiusX:Number;
        protected var _radiusY:Number;
        protected var _random:uint;
        protected var _stroke:uint;
        protected const T_MIN:int = 90;
        protected const T_MAX:int = 270;
        protected const ALPHA:Number = 0.1;
        
        /**
         * Constructor
         */ 
        public function HairyEllipse(radiusX:Number,radiusY:Number,random:uint,stroke:uint=0x0){
            _radiusX = radiusX;
            _radiusY = radiusY;
            _random = random;
            _stroke = stroke;
            this.init();
        }
        
        protected function init():void{
            this.update();
        }
        
        protected function update():void{
            var g:Graphics = this.graphics;
            g.clear();
            
            g.lineStyle(0,_stroke,ALPHA);
            for (var t:int=T_MIN; t<T_MAX; t++){
                
                //Start point
                var theta:Number = Math.PI*t/180;
                var startX:Number = _radiusX*Math.cos(theta);
                var startY:Number = _radiusY*Math.sin(theta);
                
                //Anchor point so that the hair goes outer.
                var hairRad:Number = _random*Math.random();
                var hairTheta:Number = theta + randomGen(90)*Math.PI/180;
                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 = theta + randomGen(90)*Math.PI/180;
                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);
            }
        }
        
        /**
         * @return Random values between -random and random.
         */ 
        protected function randomGen(random:uint):Number{
            return 2*random*(Math.random() - 1/2);
        }
    }
}
Powered by boreal-kiss.com 2008.