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: Flare 燃えろよ♪燃えろよ♪

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

// forked from ProjectNya's Flare 燃えろよ♪燃えろよ♪
////////////////////////////////////////////////////////////////////////////////
// [AS3.0] Flareクラスに挑戦! (2)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1128
// 画面クリックで炎が出る範囲が変わったりします。
////////////////////////////////////////////////////////////////////////////////

package {

    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.geom.Rectangle;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import net.hires.debug.Stats;

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

    public class Main extends Sprite {
        private var noise:PerlinNoise;
        private var bitmap:Bitmap;
        private var flare:FlareMap;
        private var detection:DetectPixels;
        private static var cx:uint = 232;
        private static var cy:uint = 300;
        private static var fw:uint = 400;
        private static var fh:uint = 300;
        private static var bw:uint = 380;
        private static var bh:uint = 120;
        private var area:Rectangle;

        public function Main() {
            Wonderfl.capture_delay(4);
            addChild(new Stats());
            init();
        }

        private function init():void {
            graphics.beginFill(0x000000);
            graphics.drawRect(0, 0, 465, 465);
            graphics.endFill();
            noise = new PerlinNoise(new Rectangle(0, 0, bw, bh), 60, 4, 0);
            noise.colorize({r: 1.5, g: 1.5, b: 1.5}, {r: -0x66, g: -0x66, b: -0x66});
            bitmap = new Bitmap(noise);
            addChild(bitmap);
            bitmap.x = cx - bw*0.5;
            bitmap.y = cy + 20;
            var rect:Rectangle = new Rectangle(0, 0, fw, fh);
            flare = new FlareMap(rect);
            addChild(flare);
            flare.x = cx;
            flare.y = cy;
            area = new Rectangle(0, 0, bw, bh);
            detection = new DetectPixels(4);
            detection.search(noise, area, 0xFF666666);
            flare.map = detection.pixels();
            flare.offset = {x: 10, y: cy - bh};
            flare.setup(6, 4, 60);
            flare.start();
            stage.addEventListener(MouseEvent.CLICK, click, false, 0, true);
        }
        private function click(evt:MouseEvent):void {
            noise.create(60, 4, 0);
            noise.colorize({r: 1.5, g: 1.5, b: 1.5}, {r: -0x66, g: -0x66, b: -0x66});
            detection.search(noise, area, 0xFF666666);
            flare.map = detection.pixels();
        }

    }

}


import flash.display.Sprite;
import flash.display.Shape;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.filters.BlurFilter;
import flash.display.BlendMode;
//import sketchbook.colors.ColorUtil;

class FlareMap extends Sprite {
    private var rect:Rectangle;
    private var fire:Rectangle;
    private var flare:BitmapData;
    private var bitmapData:BitmapData;
    private var bitmap:Bitmap;
    private var rPalette:Array;
    private var gPalette:Array;
    private var bPalette:Array;
    private static var point:Point = new Point(0, 0);
    private var speed:Point = new Point(0, -6);
    private var unit:uint = 8;
    private var segments:uint = 8;
    private var blur:BlurFilter;
    private var mapList:Array;
    public var offset:Object = {x: 0, y: 0};
    private var faded:uint = 0;
    public static const COMPLETE:String = Event.COMPLETE;

    public function FlareMap(r:Rectangle) {
        rect = r;
        initialize();
        draw();
    }

    public function setup(s:uint = 6, u:uint = 8, seg:uint = 8):void {
        speed.y = - s;
        unit = u;
        segments = seg;
    }
    public function set map(list:Array):void {
        mapList = list;
    }
    private function initialize():void {
        rPalette = new Array();
        gPalette = new Array();
        bPalette = new Array();
        for (var n:uint = 0; n < 256; n++) {
            var luminance:uint = (n < 128) ? n*2 : 0;
            //var rgb:Object = ColorUtil.HLS2RGB(n*360/256, luminance, 100);
            var rgb:Object = HLS2RGB(n*360/256, luminance, 100);
            var color:uint = rgb.r << 16 | rgb.g << 8 | rgb.b;
            rPalette[n] = color;
            gPalette[n] = 0;
            bPalette[n] = 0;
        }
        blur = new BlurFilter(2, 8, 3);
        blendMode = BlendMode.ADD;
    }
    private function draw():void {
        fire = new Rectangle(0, 0, rect.width, rect.height + 10);
        flare = new BitmapData(fire.width, fire.height, false, 0xFF000000);
        bitmapData = new BitmapData(rect.width, rect.height, false, 0xFF000000);
        bitmap = new Bitmap(bitmapData);
        addChild(bitmap);
        bitmap.x = - rect.width*0.5;
        bitmap.y = - rect.height;
    }
    public function start():void {
        addEventListener(Event.ENTER_FRAME, apply, false, 0, true);
    }
    private function apply(evt:Event):void {
        if (!mapList) return;
        flare.lock();
        bitmapData.lock();
        for (var n:uint = 0; n < segments; n++) {
            var id:uint = Math.random()*mapList.length;
            var px:int = mapList[id].x + offset.x;
            var py:int = mapList[id].y + offset.y;
            var range:Rectangle = new Rectangle(px, py, unit, 2)
            flare.fillRect(range, 0xFFFFFF);
        }
        flare.applyFilter(flare, fire, speed, blur);
        bitmapData.paletteMap(flare, rect, point, rPalette, gPalette, bPalette);
        flare.unlock();
        bitmapData.unlock();
    }
    private function clear(evt:Event):void {
        faded ++;
        flare.lock();
        bitmapData.lock();
        flare.applyFilter(flare, fire, speed, blur);
        bitmapData.paletteMap(flare, rect, point, rPalette, gPalette, bPalette);
        if (faded > 20) {
            bitmapData.fillRect(rect, 0x000000);
            removeEventListener(Event.ENTER_FRAME, clear);
            dispatchEvent(new Event(FlareMap.COMPLETE));
        }
        flare.unlock();
        bitmapData.unlock();
    }
    private function createEggMask(target:Shape):void {
        var w:Number = rect.width;
        var h:Number = rect.height*1.5;
        target.graphics.beginFill(0xFFFFFF);
        target.graphics.moveTo(-w*0.5, -h*0.2);
        target.graphics.curveTo(-w*0.4, -h, 0, -h);
        target.graphics.curveTo(w*0.4, -h, w*0.5, -h*0.2);
        target.graphics.curveTo(w*0.5, 0, 0, 0);
        target.graphics.curveTo(-w*0.5, 0, -w*0.5, -h*0.2);
        target.graphics.endFill();
    }
    private function HLS2RGB(h:Number, l:Number, s:Number):Object{
        var max:Number;
        var min:Number;
        h = (h < 0)? h % 360+360 : (h>=360)? h%360: h;
        l = (l < 0)? 0 : (l > 100)? 100 : l;
        s = (s < 0)? 0 : (s > 100)? 100 : s;
        l *= 0.01;
        s *= 0.01;
        if (s == 0) {
            var val:Number = l*255;
            return {r:val, g:val, b:val};
        }
        if (l < 0.5) {
            max = l*(1 + s)*255;
        } else {
            max = (l*(1 - s) + s)*255;
        }
        min = (2*l)*255 - max;
        return _hMinMax2RGB(h, min, max);
    }
    private function _hMinMax2RGB(h:Number, min:Number, max:Number):Object{
        var r:Number;
        var g:Number;
        var b:Number;
        var area:Number = Math.floor(h/60);
        switch(area){
            case 0:
                r = max;
                g = min + h * (max - min)/60;
                b = min;
                break;
            case 1:
                r = max - (h - 60)*(max - min)/60;
                g = max;
                b = min;
                break;
            case 2:
                r = min ;
                g = max;
                b = min + (h - 120)*(max - min)/60;
                break;
            case 3:
                r = min;
                g = max - (h - 180)*(max - min)/60;
                b =max;
                break;
            case 4:
                r = min + (h - 240)*(max - min)/60;
                g = min;
                b = max;
                break;
            case 5:
                r = max;
                g = min;
                b = max - (h - 300)*(max - min)/60;
                break;
            case 6:
                r = max;
                g = min + h*(max - min)/60;
                b = min;
                break;
        }
        r = Math.min(255, Math.max(0, Math.round(r)));
        g = Math.min(255, Math.max(0, Math.round(g)));
        b = Math.min(255, Math.max(0, Math.round(b)));
        return {r:r, g:g, b:b};
    }

}


import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.display.IBitmapDrawable;

class DetectPixels {
    private var bd:IBitmapDrawable;
    private var rect:Rectangle;
    private var map:BitmapData;
    private var mapList:Array;
    private var accuracy:uint;
    private var threshold:uint = 0x80FFFFFF;

    public function DetectPixels(a:uint = 1) {
        accuracy = a;
    }

    public function search(t:IBitmapDrawable, r:Rectangle, th:uint = 0x80FFFFFF):void {
        bd = t;
        rect = r;
        threshold = th;
        var w:uint = rect.width/accuracy;
        var h:uint = rect.height/accuracy;
        detect(w, h);
    }
    private function detect(w:uint, h:uint):void {
        map = new BitmapData(w, h, true, 0x00000000);
        var matrix:Matrix = new Matrix();
        matrix.translate(-rect.x, -rect.y);
        matrix.scale(1/accuracy, 1/accuracy);
        map.lock();
        map.draw(bd, matrix);
        map.unlock();
        mapList = new Array();
        for (var x:uint = 0; x < w; x++) {
            for (var y:uint = 0; y < h; y++) {
                var color:uint = map.getPixel32(x, y);
                if (color >= threshold) {
                    var px:int = x*accuracy + rect.x;
                    var py:int = y*accuracy + rect.y;
                    var point:Point = new Point(px, py);
                    mapList.push(point);
                }
            }
        }
    }
    public function pixels():Array {
        return mapList;
    }

}


import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;

class Threshold extends BitmapData {
    private var source:BitmapData;
    private static var point:Point = new Point();
    private var operation:String;
    private var _threshold:uint;
    private var color:uint;
    private var mask:uint;
    public static const OPERATION_LOW:String = "<=";
    public static const OPERATION_HIGH:String = ">=";

    public function Threshold(bd:BitmapData, o:String = OPERATION_LOW, t:uint = 0x000000, c:uint = 0x00000000, m:uint = 0xFFFFFFFF) {
        super(bd.rect.width, bd.rect.height, true, 0xFFFFFFFF);
        source = bd;
        apply(o, t, c, m);
    }

    public function apply(o:String, t:uint, c:uint, m:uint):void {
        operation = o;
        _threshold = t;
        color = c;
        mask = m;
        lock();
        reset();
        threshold(source, rect, point, operation, _threshold, color, mask, false);
        unlock();
    }
    public function reset(fill:uint = 0xFFFFFFFF):void {
        fillRect(rect, fill);
    }

}


import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.geom.ColorTransform;

class PerlinNoise extends BitmapData {
    private var source:BitmapData;
    private var base:uint;
    private var octaves:uint;
    private var seed:uint;
    private static var point:Point = new Point();
    private var offsets:Array = [point, point];
    private var color:ColorTransform;
    private var multiplier:Object = {r: 1, g: 1, b: 1};
    private var offset:Object = {r: 0x00, g: 0x00, b: 0x00};

    public function PerlinNoise(rect:Rectangle, b:uint = 20, o:uint = 2, s:uint = 1) {
        super(rect.width, rect.height, false, 0xFF000000);
        source = new BitmapData(rect.width, rect.height, false, 0xFF000000);
        create(b, o, s);
    }

    public function create(b:uint, o:uint, s:uint):void {
        base = b;
        octaves = o;
        seed = s;
        if (seed == 0) seed = Math.floor(Math.random()*1000);
        lock();
        source.perlinNoise(base, base, octaves, seed, false, true, 0, true, offsets);
        draw(source);
        unlock();
    }
    public function colorize(m:Object, o:Object):void {
        multiplier = m;
        offset = o;
        color = new ColorTransform(multiplier.r, multiplier.g, multiplier.b, 1, offset.r, offset.g, offset.b, 0);
        lock();
        draw(source, null, color);
        unlock();
    }

}