Fl029_Main.as (Main)

/**
 * @date 20/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package {
    import flash.display.Sprite;
    import com.borealkiss.display.HatchedLine;

    [SWF(width="550",height="400",backgroundColor="0x0")]
    public class Fl029_Main extends Sprite{
        private const STAGE_WIDTH:Number = 550;
        private const STAGE_HEIGHT:Number = 400;
        private const PADDING_X:Number = 10;
        private const PADDING_Y:Number = 10;
        private const LIMIT_X:uint = 20;
        private const LIMIT_Y:uint = 20;
        private var _canvas:Sprite;
        
        public function Fl029_Main(){
            _canvas = new Sprite();
            this.addChild(_canvas);
            this.draw();
        }
        
        private function draw():void{
            var w:Number = (STAGE_WIDTH - 2*PADDING_X)/LIMIT_X;
            var h:Number = (STAGE_HEIGHT - 2*PADDING_Y)/LIMIT_Y;
            
            //Lines parallel to y-axis
            for (var i:int=0; i<LIMIT_X; i++){
                var xStart:Number = PADDING_X + (2*i + 1)*w/2;
                var yStart:Number = PADDING_Y + h/2;
                var xEnd:Number = xStart;
                var yEnd:Number = STAGE_HEIGHT - PADDING_Y - h/2;
                var verticalLine:HatchedLine = new HatchedLine(
                                                    xStart,yStart,xEnd,yEnd,200,getColorX(i));
                _canvas.addChild(verticalLine);
            }
            
            //Lines parallel to x-axis
            for (var j:int=0; j<LIMIT_Y; j++){
                xStart = PADDING_X + w/2;
                yStart = PADDING_Y + (2*j + 1)*h/2;
                xEnd = STAGE_WIDTH - PADDING_X - w/2;
                yEnd = yStart
                var horizontalLine:HatchedLine = new HatchedLine(
                                                    xStart,yStart,xEnd,yEnd,200,getColorY(j));
                _canvas.addChild(horizontalLine);
            }
        }
        
        private function getColorX(num:int):uint{
            var r:int = 255*(0.9*(LIMIT_X - 1 - num)/LIMIT_X + 0.1);
            var g:int = r;
            var b:int = r;
            return r << 16 | g << 8 | b;
        }
        
        private function getColorY(num:int):uint{
            var r:int = 255*(0.9*(LIMIT_Y - 1 - num)/LIMIT_Y + 0.1);
            var g:int = r
            var b:int = r;
            return r << 16 | g << 8 | b;
        }
    }
}

com.borealkiss.display.HatchedLine.as

/**
 * @date 20/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.Sprite;
    import flash.display.Graphics;
    import flash.geom.Point;
    import flash.events.MouseEvent;
    import flash.events.Event;
    
    public class HatchedLine extends Sprite{
        protected var _pStart:Point;
        protected var _pEnd:Point;
        protected var _random:uint;
        protected var _steps:int;
        protected var _stroke:uint;
        protected static const THRESHOULD:uint = 5;
        
        /**
         * Constructor
         */ 
        public function HatchedLine(xStart:Number,yStart:Number,xEnd:Number,yEnd:Number,
                                                            steps:int=100,stroke:uint=0x0){
            _pStart = new Point(xStart,yStart);
            _pEnd = new Point(xEnd,yEnd);
            _steps = steps;
            _stroke = stroke;
            this.init();
        }
        
        protected function init():void{
            _random = THRESHOULD;
            this.update();
            this.addEventListener(MouseEvent.ROLL_OVER,onBreak);
        }
        
        private function onBreak(e:MouseEvent=null):void{
            _random = int(5*Math.random()) + 10;
            this.addEventListener(Event.ENTER_FRAME,update);
        }
        
        protected function update(e:Event=null):void{
            var g:Graphics = this.graphics;
            g.clear();
            g.lineStyle(0,_stroke);
            for (var i:int=0; i<_steps; i++){
                var xstart:Number = _pStart.x + (_pEnd.x - _pStart.x)*i/_steps;
                var ystart:Number = _pStart.y + (_pEnd.y - _pStart.y)*i/_steps;
                var xend:Number = xstart + randomGen(_random);
                var yend:Number = ystart + randomGen(_random);
                g.moveTo(xstart,ystart);
                g.lineTo(xend,yend);
            }
            
            _random--;
            if (_random < THRESHOULD){
                this.removeEventListener(Event.ENTER_FRAME,update);
            }
        }
        
        /**
         * @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.