forked from: forked from: visualizer

by jokehn9 forked from forked from: visualizer (diff: 115)
♥0 | Line 124 | Modified 2012-07-08 09:24:33 | MIT License
play

ActionScript3 source code

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

// forked from jokehn9's forked from: visualizer
// forked from codeonwort's visualizer
package {
    import flash.geom.ColorTransform;
    import flash.display.Shape;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.text.TextField;
    import flash.display.Graphics;
    import flash.utils.ByteArray;
    import flash.media.SoundChannel;
    
    import flash.media.Sound;
    import flash.net.URLRequest
    import flash.media.SoundLoaderContext
    import flash.media.SoundChannel
    import flash.media.SoundMixer;
    import flash.display.Sprite
    import flash.events.Event
    
    public class FlashTest extends Sprite {
        private var s:Sound
        private var channel:SoundChannel;
        private const PARTICLE_NUM:int = 400;
        private const cx:int = 228;
        private const cy:int = 228;
        private var bytes:ByteArray = new ByteArray()
        private var txt:TextField = new TextField();
        private var arr:Array = []; 
        private var speed:Number = 0;
        private var d:Number = 0;
        private var bd:BitmapData = new BitmapData(465,465,true,0x0);
        private var g:Shape = new Shape();
        private var backGround:Shape = new Shape();
        public function FlashTest() {
            // write as3 code here..
            stage.addEventListener("rightClick", function($:Event):void{})
            addChild(new Bitmap(bd));
            txt.text = "";
            txt.x = 200;
            txt.y = 50;
            addChild(txt);
            s = new Sound(new URLRequest('http://clug.kr/~codeonwort/file_pool/old_elvengard.mp3'), new SoundLoaderContext(1000, true));
            s.addEventListener(Event.COMPLETE, comp)
            s.addEventListener(Event.SOUND_COMPLETE, onPbc);
            var cou:int = 0;
            
            for(cou = 0; cou < PARTICLE_NUM;cou++) {
                arr[cou] = new particle;
                arr[cou].x = Math.random() * 465;
                arr[cou].y = Math.random() * 465;
                arr[cou].color = (Math.random() * 0xffff);
                if(arr[cou].x < 306 && arr[cou].x > 150) {
                    if(arr[cou].y < 306 && arr[cou].y > 150) {
                        arr[cou].x = Math.random() * 465;
                        arr[cou].y = Math.random() * 465;
                       
                    }

                }
            }

            
            
        }
        
        private function comp(e:Event):void {
            channel = s.play();
            
            backGround.graphics.beginFill(0x0);
            backGround.graphics.drawRect(0,0,465,465);
            backGround.graphics.endFill();
            
            addEventListener("enterFrame", loop)
        }
        
        private function loop(event:Event):void {
            SoundMixer.computeSpectrum(bytes, false, 0);
            
            
            
            speed++;
            

            var n:Number = 0;
           var b:Number = 0;
           var tempx:Number = 0;
           var tempy:Number = 0;
             g.graphics.clear();
             g.graphics.beginFill(0x0);
            g.graphics.drawRect(0,0,456,456);
            g.graphics.endFill();
             n = bytes.readFloat()
             
             for(var i:int = 0; i<PARTICLE_NUM; i++) {
                 g.graphics.lineStyle(1, arr[i].color);
                 d = Math.sqrt(Math.pow(arr[i].x - cx , 2) + Math.pow(cy - arr[i].y,2)); 
                 arr[i].vx += (cx - arr[i].x)/d;
                 arr[i].vy += ( cy - arr[i].y)/d;
                 arr[i].vx *= 0.95;
                 arr[i].vy *= 0.95;
                 if(n > 0.1 ) {
                     if(arr[i].x - cx > 0) {
                         arr[i].vx += Math.abs(n*22);
                     }
                     else {
                         arr[i].vx -= Math.abs(n*22);
                     }
                     if(arr[i].y - cy > 0) {
                         arr[i].vy += Math.abs(b*22);
                     }
                     else {
                         arr[i].vy -= Math.abs(b*22);
                     }
                 }
                 /*else if(n > 0.7) {
                     tempx = arr[i].x;
                     tempy = arr[i].y;
                     arr[i].vx += (rotatation(tempx,tempy,cx,cy,90,0) - tempx)/d;
                     arr[i].vy += (rotatation(tempx,tempy,cx,cy,90,1) - tempy)/d;
                 }
*/
                 g.graphics.moveTo(arr[i].x, arr[i].y);
                 arr[i].x += arr[i].vx;
                 arr[i].y += arr[i].vy;
                 g.graphics.lineTo(arr[i].x, arr[i].y);    
                 b = bytes.readFloat();            
                
             }
             
             bd.draw(g);
             bd.colorTransform(bd.rect, new ColorTransform(1,1,1,0.9,0,0,0,0));
             

           

        }
        
        private function onPbc(event:Event):void
        {
            removeEventListener("enterFrame", loop);
        }
        
        private function rotatation(targetx:Number,targety:Number,cx:Number, cy:Number, rotation:Number, swi:int = 0):Number {
            
            if(swi == 0) {
                return cx + (Math.cos(rotation) * ( targetx - cx) - Math.sin(rotation) * ( targety - cy));
            }
            else {
                return cy + (Math.sin(rotation) * ( targetx - cx) + Math.cos(rotation) * ( targety - cy));
            }


        }



    }
    

}
class particle{
    public var x:Number = 0;
    public var y:Number = 0;
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var color:uint = 0xff;
}

Forked