Beautifl - Flash Gallery

Thumbnail : Happy Birthday ! [TrackLight + Wonderfl Color]
Happy Birthday ! [TrackLight + Wonderfl Color]
ProjectNya 2011-02-10 MIT License

再生するにはFlash Playerが必要です。デスクトップのブラウザでご覧ください。

////////////////////////////////////////////////////////////////////////////////
// Happy Birthday ! [TrackLight + Wonderfl Color]
////////////////////////////////////////////////////////////////////////////////

package {

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.system.Security;

    [SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="60")]

    public class Main extends Sprite {
        private var light:TrackLight;
        private var effector:SoundEffector;
        private static var domain:String = "www.project-nya.jp";
        private static var policyPath:String = "http://www.project-nya.jp/crossdomain.xml";
        private static var soundPath:String = "http://www.project-nya.jp/images/bgm/bgm_spring.mp3";

        public function Main() {
            //Wonderfl.capture_delay(1);
            init();
        }

        private function init():void {
            var wondflcolor:WonderflColor = new WonderflColor(465, 465);
            addChild(wondflcolor);
            //
            var rect:Rectangle = new Rectangle(0, 0, 465, 465);
            light = new TrackLight(rect);
            addChild(light);
            //
            var title:Label = new Label(200, 40, 36, Label.CENTER);
            addChild(title);
            title.x = 132;
            title.y  =192;
            title.textColor = 0xFFFFFF;
            title.text = "Happy Birthday !";
            var subtitle:Label = new Label(200, 20, 14, Label.CENTER);
            addChild(subtitle);
            subtitle.x = 132;
            subtitle.y  =242;
            subtitle.textColor = 0xFFFFFF;
            subtitle.text = "2011.2.9. for naruto baby";
            //
            Security.allowDomain(domain);
            Security.loadPolicyFile(policyPath);
            effector = new SoundEffector();
            effector.addEventListener(Event.COMPLETE, complete, false, 0, true);
            effector.load(soundPath);
        }
        private function complete(evt:Event):void {
            effector.removeEventListener(Event.COMPLETE, complete);
            effector.play();
            effector.setup(25);
            effector.effect(light);
            light.start();
        }
        
    }

}


//////////////////////////////////////////////////
// TrackLightクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.geom.ColorTransform;
import flash.display.BlendMode;
import flash.filters.BlurFilter;
import __AS3__.vec.Vector;
import frocessing.color.ColorHSV;

class TrackLight extends Sprite {
    private var rect:Rectangle;
    private var cx:uint = 0;
    private var cy:uint = 0;
    private var bitmapData:BitmapData;
    private var bitmap:Bitmap;
    private var container:Sprite;
    private var color:ColorHSV;
    private var colorTrans:ColorTransform;
    private static var blur:BlurFilter;
    private static var point:Point = new Point();
    private var id:uint = 0;
    private var leader:TrackLeader;
    private static var max:uint = 3;
    private var followers:Array;
    private var tracks:Vector.<Point>;
    private static var interval:uint = 10;
    private var particles:Array;
    private var units:Array = [0, 0];
    private static var radian:Number = Math.PI/180;

    public function TrackLight(r:Rectangle) {
        rect = r;
        cx = rect.x + rect.width/2;
        cy = rect.y + rect.height/2;
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
    }

    private function init(evt:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        bitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
        bitmap = new Bitmap(bitmapData);
        addChild(bitmap);
        container = new Sprite();
        addChild(container);
        color = new ColorHSV(0);
        colorTrans = new ColorTransform();
        blur = new BlurFilter(8, 8, 3);
        leader = new TrackLeader();
        leader.x = cx;
        leader.y = cy;
        followers = new Array();
        tracks = new Vector.<Point>();
        for (var n:uint = 0; n < max; n++) {
            var follower:TrackFollower = new TrackFollower();
            follower.x = cx;
            follower.y = cy;
            followers.push(follower);
            for (var t:uint = 0; t < interval; t++) {
                tracks.push(new Point(cx, cy));
            }
        }
        particles = new Array();
        blendMode = BlendMode.SCREEN;
    }
    public function start():void {
        addEventListener(Event.ENTER_FRAME, update, false, 0, true);
    }
    public function stop():void {
        removeEventListener(Event.ENTER_FRAME, update);
    }
    public function create(u:Array):void {
        units = u;
        var particle:ParticleLight = new ParticleLight(5, 0xFFFFFF);
        particle.track(leader);
        particle.angle = Math.random()*360;
        particle.speed = units[0]*10 + 5;
        particle.power = 1;
        particle.setup();
        particle.blendMode = BlendMode.ADD;
        container.addChild(particle);
        particles.push(particle);
        for (var n:uint = 0; n < max; n++) {
            particle = new ParticleLight(5, 0xFFFFFF);
            var follower:TrackFollower = followers[n];
            particle.track(follower);
            particle.angle = Math.random()*360;
            particle.speed = units[n+1]*10 + 5;
            particle.power = 1;
            particle.setup();
            particle.blendMode = BlendMode.ADD;
            container.addChild(particle);
            particles.push(particle);
        }
    }
    private function update(evt:Event):void {
        setup();
        bitmapData.lock();
        for (var n:uint = 0; n < particles.length; n++) {
            var particle:ParticleLight = particles[n];
            particle.update();
            particle.scale = particle.alpha = particle.power;
            if (particle.power < 0) {
                container.removeChild(particle);
                particles.splice(n, 1);
                particle = null;
            }
        }
        color.h = id;
        colorTrans.color = color.value;
        bitmapData.draw(container, null, colorTrans, BlendMode.SCREEN, null, true);
        bitmapData.applyFilter(bitmapData, rect, point, blur);
        bitmapData.unlock();
        id ++;
    }
    private function setup():void {
        var px:Number = cx + 250*Math.sin(id*units[0]*3*radian)*Math.cos(id*radian);
        var py:Number = cy + 100*Math.sin(id*2*radian)*Math.sin(id*radian);
        leader.update(new Point(px, py));
        for (var n:uint = 0; n < max; n++) {
            var follower:Object = followers[n];
            follower.update(tracks[interval*(n + 1) - 1]);
        }
        tracks.unshift(new Point(leader.x, leader.y));
        tracks.pop();            
    }

}


//////////////////////////////////////////////////
// TrackLeaderクラス
//////////////////////////////////////////////////

import flash.geom.Point;

class TrackLeader {
    public var x:Number = 0;
    public var y:Number = 0;
    private static var deceleration:Number = 0.16;

    public function TrackLeader() {
    }

    public function update(track:Point):void {
        x += (track.x - x)*deceleration;
        y += (track.y - y)*deceleration;
    }

}


//////////////////////////////////////////////////
// TrackFollowerクラス
//////////////////////////////////////////////////

import flash.geom.Point;

class TrackFollower {
    public var x:Number = 0;
    public var y:Number = 0;
    private static var deceleration:Number = 0.16;

    public function TrackFollower() {
    }

    public function update(track:Point):void {
        x = track.x;
        y = track.y;
    }

}


//////////////////////////////////////////////////
// ParticleLightクラス
//////////////////////////////////////////////////

import flash.display.Shape;

class ParticleLight extends Shape {
    private var radius:uint = 10;
    private var color:uint = 0xFFFFFF;
    private var target:Object;
    public var angle:Number = 0;
    public var speed:Number = 0;
    private var tx:Number = 0;
    private var ty:Number = 0;
    private var vx:Number = 0;
    private var vy:Number = 0;
    public var power:Number = 0;
    private static var radian:Number = Math.PI/180;
    private static var friction:Number = 0.96;
    private static var deceleration:Number = 0.02;
    private static var acceleration:Number = 0.05;
    private var _scale:Number = 1;

    public function ParticleLight(r:uint = 10, c:uint = 0xFFFFFF) {
        radius = r;
        color = c;
        draw();
    }

    private function draw():void {
        graphics.beginFill(color);
        graphics.drawCircle(0, 0, radius);
        graphics.endFill();
    }
    public function track(t:Object):void {
        target = t;
    }
    public function setup():void {
        x = target.x;
        y = target.y;
        vx = speed*Math.cos(angle*radian);
        vy = speed*Math.sin(angle*radian);
    }
    public function update():void {
        tx += vx;
        ty += vy;
        x += (target.x + tx - x)*acceleration;
        y += (target.y + ty - y)*acceleration;
        vx *= friction;
        vy *= friction;
        power -= deceleration;
    }
    public function get scale():Number {
        return _scale;
    }
    public function set scale(param:Number):void {
        _scale = param;
        scaleX = scaleY = _scale;
    }

}


//////////////////////////////////////////////////
// SoundEffectorクラス
//////////////////////////////////////////////////

import flash.events.EventDispatcher;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.net.URLRequest;
import flash.utils.Timer;
import flash.events.TimerEvent;

class SoundEffector extends EventDispatcher {
    private var sound:Sound;
    private var channel:SoundChannel;
    private var byteArray:ByteArray;
    private static var channels:uint = 256;
    private var factors:uint = 0;
    private var timer:Timer;
    private var interval:uint = 100;
    private var light:TrackLight;

    public function SoundEffector() {
        init();
    }

    private function init():void {
        sound = new Sound();
        sound.addEventListener(Event.COMPLETE, complete, false, 0, true);
        byteArray = new ByteArray();
    }
    public function load(soundPath:String):void {
        sound.load(new URLRequest(soundPath), new SoundLoaderContext(5, true));
    }
    private function complete(evt:Event):void {
        dispatchEvent(new Event(Event.COMPLETE));
    }
    public function effect(l:TrackLight):void {
        light = l;
    }
    public function play(loop:uint = 1000):void {
        channel = sound.play(0, loop);
    }
    public function setup(i:uint = 100):void {
        interval = i;
        timer = new Timer(interval);
        timer.addEventListener(TimerEvent.TIMER, update, false, 0, true);
        timer.start();
    }
    private function update(evt:TimerEvent):void {
        SoundMixer.computeSpectrum(byteArray, true, factors);
        var units:Array = new Array();
        for (var n:uint = 0; n < channels; n++) {
            var p:Number = byteArray.readFloat();
            if (n%32 == 0) units.push(p);
        }
        light.create(units);
    }

}


//////////////////////////////////////////////////
// WonderflColorクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;

class WonderflColor extends Sprite {
    private var color1:uint = 0x00AAE4;
    private var color2:uint = 0x0069A0;

    public function WonderflColor(w:uint, h:uint) {
        draw(w, h);
    }

    private function draw(w:uint, h:uint):void {
        var colors:Array = [color1, color2];
        var alphas:Array = [1, 1];
        var ratios:Array = [0, 255];
        var matrix:Matrix = new Matrix();
        matrix.createGradientBox(w*1.5, h*1.5, 0, -w*0.25, -h*0.25);
        graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
        graphics.drawRect(0, 0, w, h);
        graphics.endFill();
    }

}


//////////////////////////////////////////////////
// Labelクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.BlendMode;
import flash.filters.DropShadowFilter;

class Label extends Sprite {
    private var txt:TextField;
    private static var fontType:String = "_ゴシック";
    private var _width:uint = 20;
    private var _height:uint = 20;
    private var size:uint = 12;
    public static const LEFT:String = TextFormatAlign.LEFT;
    public static const CENTER:String = TextFormatAlign.CENTER;
    public static const RIGHT:String = TextFormatAlign.RIGHT;

    public function Label(w:uint, h:uint, s:uint = 12, align:String = LEFT) {
        _width = w;
        _height = h;
        size = s;
        draw(align);
        blendMode = BlendMode.LAYER;
        var shade:DropShadowFilter = new DropShadowFilter(4, 90, 0x000000, 0.3, 8, 8, 2, 3, false, false);
        filters = [shade];
    }

    private function draw(align:String):void {
        txt = new TextField();
        addChild(txt);
        txt.width = _width;
        txt.height = _height;
        txt.autoSize = align;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        //txt.embedFonts = true;
        //txt.antiAliasType = AntiAliasType.ADVANCED;
        var tf:TextFormat = new TextFormat();
        tf.font = fontType;
        tf.size = size;
        tf.align = align;
        txt.defaultTextFormat = tf;
        textColor = 0x000000;
    }
    public function set text(param:String):void {
        txt.text = param;
    }
    public function set textColor(param:uint):void {
        txt.textColor = param;
    }

}