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

forked from: [ff]: ビリビリ(Lightning Effect)

/**
 * Copyright h_sakurai ( http://wonderfl.net/user/h_sakurai )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/nrlM
 */

// forked from bradsedito's [ff]: ビリビリ(Lightning Effect)




package 
{    
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.display.Sprite;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix;
    import flash.filters.GlowFilter;
    import flash.events.MouseEvent;
    
    [SWF(width="465", height="465", backgroundColor="0", frameRate="30")]
    
    public class Main extends Sprite{
        private const W:Number = 465;
        private const H:Number = 465;
        private const RANGE:int = 31; // 1,3,5,15,31,93,155,465
        private var _pn:int = 0;
        private var _ps:Array = [];
        private var _sp:Sprite;
        private var _ctf:ColorTransform;
        private var _canvas:BitmapData;
        private var _glow:BitmapData;
        
        private var _rad:Number = 0.0;

        public function Main() {
            init();
            addEventListener(Event.ENTER_FRAME, update)    ;
            stage.addEventListener(MouseEvent.CLICK, onDown);
        }
        
        private function init():void
        {
            _ps[0] = new Point( W,H-H );
            _ps[1] = new Point(0, H);
            //_p = new Point( W/2,H/2 );
            _sp = new Sprite();
            _sp.filters = [new GlowFilter(0xC9E6FC, 1, 10, 10, 4, 3, false, false)];
            _ctf = new ColorTransform(0.9, 0.96, 1, 0.9);
            _canvas = new BitmapData(W,H,false,0);
            
            var bm:Bitmap = new Bitmap(_canvas, "auto", true);
            _glow = new BitmapData(W / RANGE, H / RANGE, false, 0);
            
            var glowBm:Bitmap = new Bitmap(_glow, "never", true);
            glowBm.blendMode = "add";
            glowBm.scaleX = RANGE;
            glowBm.scaleY = RANGE;
            
            addChild(bm);
            addChild(glowBm);
        }
        
        private function onDown(e:MouseEvent):void{
            _pn = (_pn + 1) % 2;
            _ps[_pn] = new Point(mouseX, mouseY);
        }
                
        private function update(e:Event):void{
            var p:Point = new Point();
            var num:int = Math.random() * 5;
            p.x = _ps[0].x;
            p.y = _ps[0].y;
            //p.x = mouseX;
            //p.y = mouseY;
            _sp.graphics.clear();
            _sp.graphics.lineStyle(num, 0xFFFFFF, 1-(num / 10));
            _sp.graphics.moveTo(p.x, p.y);
            
            _rad += ( Math.random() * 90.0) * Math.PI / 180.0 * (Math.random() * 2.0 - 1.0);
            var rad:Number = _rad;
            var max:Number = 20.0;
            var speed:int;
            var noise:int;
            var l1:int = len2(_ps[0], _ps[1]);
            var ps:Array = [];
            
            while( l1 > len2(p, _ps[0])){
                speed = Math.random() * (Math.sqrt(l1)/40+10)/2;
                rad += Math.random() * max * Math.PI / 180.0 * (Math.random() * 2.0 - 1.0);
                noise = Math.random() * 8 - 4;
                p.x += Math.cos(rad) * speed + noise;
                p.y += Math.sin(rad) * speed + noise;
                ps.push(new Point(p.x, p.y));
            }
            var rad2:Number = Math.atan2(p.y-_ps[0].y, p.x-_ps[0].x);
            var rad1:Number = Math.atan2(_ps[1].y-_ps[0].y, _ps[1].x - _ps[0].x);
            rad2 = normalRad(rad2-rad1)
            
//rad2 = Math.PI / 2;
            for each (p in ps) {
                var x:Number = p.x - _ps[0].x;
                var y:Number = p.y - _ps[0].y;
                p.x = Math.cos(rad2)*x + Math.sin(rad2)*y + _ps[0].x;
                p.y = -Math.sin(rad2)*x + Math.cos(rad2)*y + _ps[0].y;
                _sp.graphics.lineTo(p.x, p.y);
                
            }

            
            _canvas.colorTransform(_canvas.rect, _ctf);
            _canvas.draw(_sp);
            _glow.draw(_canvas, new Matrix(1 / RANGE, 0, 0, 1 / RANGE));
        }
        private function len2(p:Point, p2:Point):int {
            var x2:int = (p.x - p2.x);
            var y2:int = (p.y - p2.y);
            return x2*x2+y2*y2;
        }
        private function normalRad(v:Number):Number {
            return v + 2 * Math.PI * (
                (v >  Math.PI) ? -1 :
                (v < -Math.PI) ? 1  : 0
            );
        }
    }
}