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

candle + fire + TrackLight [Christmas]

candle + fire + TrackLight [Christmas]

[AS3.0] TrackLightクラスだ! (7)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1292
Get Adobe Flash player
by ProjectNya 19 Dec 2010
/**
 * Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9qso
 */

////////////////////////////////////////////////////////////////////////////////
// candle + fire + TrackLight [Christmas]
//
// [AS3.0] TrackLightクラスだ! (7)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1292
////////////////////////////////////////////////////////////////////////////////

package {

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

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

    public class Main extends Sprite {
        private var loader:PhotoLoader;
        private static var basePath:String = "http://assets.wonderfl.net/images/related_images/";
        private static var candlePath:String = "e/ed/edac/edacc14dd5fdd2d007f924c08230d2f2360ce50c";
        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/flash/xmas.mp3";

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

        private function init():void {
            graphics.beginFill(0x000000);
            graphics.drawRect(0, 0, 465, 465);
            graphics.endFill();
            //
            draw();
            Security.allowDomain(domain);
            Security.loadPolicyFile(policyPath);
            effector = new SoundEffector();
            effector.addEventListener(Event.COMPLETE, complete, false, 0, true);
            effector.load(soundPath);
            var rect:Rectangle = new Rectangle(0, 0, 465, 300);
            light = new TrackLight(rect);
            addChild(light);
        }
        private function complete(evt:Event):void {
            effector.removeEventListener(Event.COMPLETE, complete);
            effector.play();
            effector.setup(25);
            effector.effect(light);
            light.start();
        }
        private function draw():void {
            loader = new PhotoLoader();
            loader.addEventListener(PhotoLoader.INIT, loaded, false, 0, true);
            loader.load(basePath + candlePath);
        }
        private function loaded(evt:Event):void {
            evt.target.removeEventListener(PhotoLoader.INIT, loaded);
            var candle:Bitmap = Bitmap(evt.target.content);
            addChild(candle);
            candle.x = 172;
            candle.y = 345;
            //
            var fire:Fire = new Fire();
            addChild(fire);
            fire.x = 232;
            fire.y = 390;
        }

    }

}


//////////////////////////////////////////////////
// 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();
    }
    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);
    }

}


//////////////////////////////////////////////////
// Fireクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.filters.GradientGlowFilter;
import flash.filters.BitmapFilterType;
import flash.display.BlendMode;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;

class Fire extends Sprite {
    private var fire:Shape;
    private static var colors:Array = [0xFF0000, 0xFFFF00, 0xFFFFFF];
    private static var alphas:Array = [0, 0.75, 1];
    private static var ratios:Array = [0, 192, 255];
    private var light:Shape;

    public function Fire() {
        init();
    }

    private function init():void {
        draw();
        addEventListener(Event.ENTER_FRAME, update, false, 0, true);
    }
    private function update(evt:Event):void {
        var angle:Number = 265 + Math.floor(Math.random()*10);
        var blurX:Number = 16 + Math.floor(Math.random()*4);
        var blurY:Number = 48 + Math.floor(Math.random()*8);
        var gradientGlowFilter:GradientGlowFilter = new GradientGlowFilter(16, angle, colors, alphas, ratios, blurX, blurY, 2, 3, BitmapFilterType.FULL, true);
        fire.filters = [gradientGlowFilter];
    }
    private function draw():void {
        createLight();
        createFire();
    }
    private function createLight():void {
        light = new Shape();
        addChild(light);
        light.y = -60;
        var _colors:Array = [0xFFFFFF, 0xFFFF00];
        var _alphas:Array = [0.6, 0];
        var _ratios:Array = [0, 255];
        var radius:uint = 200;
        var matrix:Matrix = new Matrix();
        matrix.createGradientBox(radius*2, radius*2, 0, -radius, -radius);
        light.graphics.beginGradientFill(GradientType.RADIAL, _colors, _alphas, _ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
        light.graphics.drawCircle(0, 0, radius);
        light.graphics.endFill();
    }
    private function createFire():void {
        fire = new Shape();
        addChild(fire);
        var w:uint = 30;
        var h:uint = 90;
        fire.graphics.beginFill(0xFFFFFF);
        fire.graphics.moveTo(-w*0.5, -h*0.2);
        fire.graphics.curveTo(-w*0.4, -h, 0, -h);
        fire.graphics.curveTo(w*0.4, -h, w*0.5, -h*0.2);
        fire.graphics.curveTo(w*0.5, 0, 0, 0);
        fire.graphics.curveTo(-w*0.5, 0, -w*0.5, -h*0.2);
        fire.graphics.endFill();
        fire.blendMode = BlendMode.SCREEN;
        var gradientGlowFilter:GradientGlowFilter = new GradientGlowFilter(16, 270, colors, alphas, ratios, 18, 52, 2, 3, BitmapFilterType.FULL, true);
        fire.filters = [gradientGlowFilter];
    }

}


//////////////////////////////////////////////////
// PhotoLoaderクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.system.LoaderContext;

class PhotoLoader extends Sprite {
    private var loader:Loader;
    private var info:LoaderInfo;
    public var content:*;
    private var smoothing:Boolean;
    public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
    public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
    public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
    public static const INIT:String = Event.INIT;
    public static const COMPLETE:String = Event.COMPLETE;

    public function PhotoLoader() {
        loader = new Loader();
        info = loader.contentLoaderInfo;
    }

    public function load(file:String, s:Boolean = false):void {
        smoothing = s;
        info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
        info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
        info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
        info.addEventListener(Event.INIT, initialize, false, 0, true);
        info.addEventListener(Event.COMPLETE, complete, false, 0, true);
        try {
            loader.load(new URLRequest(file), new LoaderContext(true));
        } catch (err:Error) {
            trace(err.message);
        }
    }
    public function unload():void {
        loader.unload();
    }
    private function ioerror(evt:IOErrorEvent):void {
        loader.unload();
        dispatchEvent(new Event(PhotoLoader.IO_ERROR));
    }
    private function httpstatus(evt:HTTPStatusEvent):void {
        dispatchEvent(new Event(PhotoLoader.HTTP_STATUS));
    }
    private function securityerror(evt:SecurityErrorEvent):void {
        dispatchEvent(new Event(PhotoLoader.SECURITY_ERROR));
    }
    private function initialize(evt:Event):void {
        if (smoothing) {
            content = Bitmap(info.content);
            content.smoothing = true;
        } else {
            content = info.content;
        }
        dispatchEvent(new Event(PhotoLoader.INIT));
    }
    private function complete(evt:Event):void {
        info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
        info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
        info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
        info.removeEventListener(Event.INIT, initialize);
        info.removeEventListener(Event.COMPLETE, complete);
        addChild(loader);
        dispatchEvent(new Event(PhotoLoader.COMPLETE));
    }

}