forked from: ff:x3 lightning

by jmbyh521 forked from ff:x3 lightning (diff: 1)
♥0 | Line 86 | Modified 2015-12-29 16:21:10 | MIT License
play

ActionScript3 source code

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

// forked from matacat's ff:x3 lightning
// forked from hashito's forked from: forked from: lightning
// forked from okoi's forked from: lightning
// forked from kiyobu's lightning
package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.filters.BlurFilter;
    import flash.filters.GlowFilter;
    import flash.geom.ColorTransform;
    import flash.geom.Point;
    
    [SWF(width = "465", height = "465", backgroundColor = "0x000000", frameRate = "30")]
    
    public class FlashTest extends Sprite
    {
        private const LIGHT_LENGTH:int = 6;
        private const LIGHT_NUM:int    = 25;
        private const PATH_LENGTH:int  = (LIGHT_LENGTH + 1) * LIGHT_NUM << 1;
        
        private var light:Bitmap;
        private var lines:Shape;
        private var cache:BitmapData;
        
        private var ct:ColorTransform;
        private var d:Function;
        private var a:Function;
        private var o:Point;
        private var f:BlurFilter;
        private var g:Graphics;
        
        private var coms:Vector.<int>;
        private var path:Vector.<Number>;
        private var sin:Vector.<Number>;
        private var cos:Vector.<Number>;
        
        public function FlashTest()
        {
            light = new Bitmap( new BitmapData(stage.stageWidth, stage.stageHeight, false, 0) );
            addChild( light );
            
            lines         = new Shape();
            lines.filters = [ new GlowFilter(0xC080FF, 1, 64, 64, 4) ];
            
            cache = light.bitmapData.clone();
            
            ct = new ColorTransform(1, 1, 1, 1, -30, -32, -28);
            d  = cache.draw;
            a  = light.bitmapData.applyFilter;
            o  = new Point();
            f  = new BlurFilter(2, 2);
            g  = lines.graphics;
            
            coms = new Vector.<int>(PATH_LENGTH >> 1);
            for (var i:int = 0, l:int = coms.length; i < l; ) {
                coms[i++] = 1;
                for (var j:int = 0; j < LIGHT_LENGTH; j++) coms[i++] = 2;
            }
            path = new Vector.<Number>(PATH_LENGTH);
            
            sin  = new Vector.<Number>(LIGHT_NUM);
            cos  = new Vector.<Number>(LIGHT_NUM);
            var pi2:Number = Math.PI * 2;
            for (i = 0; i < LIGHT_NUM; i++) {
                sin[i] = Math.sin( i / LIGHT_NUM * pi2 );
                cos[i] = Math.cos( i / LIGHT_NUM * pi2 );
            }
            
            addEventListener(Event.ENTER_FRAME, loop);
        }
        
        public function loop(e:Event):void
        {
            lightning();
            d(light, null, ct);
            d(lines);
            a(cache, cache.rect, o, f);
        }
        
        public function lightning():void
        {
            g.clear();
            g.lineStyle(Math.random() * 2, 0xE0D0FF);
            
            for (var i:int = 0, j:int = 0; i < PATH_LENGTH; j++) {
                var xx:Number = 0;
                var yy:Number = 0;
                
                path[i++] = mouseX;
                path[i++] = mouseY;
                
                for (var l:int = 0; l < LIGHT_LENGTH ; l++) {
                    xx += Math.random() * 20 * (Math.random() > 0.5 ? 1 : -1);
                    yy += Math.random() * 80 ;
                    
                    path[i++] = cos[j] * xx - sin[j] * yy + mouseX;
                    path[i++] = sin[j] * xx + cos[j] * yy + mouseY;
                }
                
            }
            
            g.drawPath(coms, path);
        }
    }
}