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: WaveEffect + PerspectiveMap

WaveEffect + PerspectiveMap

置き換えマップ効果 (3)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=480
BitmapDataでノイズ生成 (3)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=481
[AS3.0] PerspectiveMapクラスに挑戦!
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1105
[AS3.0] PerlinNoiseクラスに挑戦!
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1114
[AS3.0] WaterEffectクラスに挑戦! (6)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1690

動作を軽くするための方法 (東京てらこ7 @trick7)
Bitmap.filters を使わず、BitmapData.applyFilter() を用いる
/**
 * Copyright aobyrne ( http://wonderfl.net/user/aobyrne )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/zdiP
 */

// forked from ProjectNya's WaveEffect + PerspectiveMap
////////////////////////////////////////////////////////////////////////////////
// WaveEffect + PerspectiveMap
//
// 置き換えマップ効果 (3)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=480
// BitmapDataでノイズ生成 (3)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=481
// [AS3.0] PerspectiveMapクラスに挑戦!
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1105
// [AS3.0] PerlinNoiseクラスに挑戦!
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1114
// [AS3.0] WaterEffectクラスに挑戦! (6)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1690
//
// 動作を軽くするための方法 (東京てらこ7 @trick7)
// Bitmap.filters を使わず、BitmapData.applyFilter() を用いる
////////////////////////////////////////////////////////////////////////////////

package {

    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.display.Bitmap;
    import flash.display.BitmapData;

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

    public class ProyectNyaFlag extends Sprite {
        private var loader:ImageLoader;
        private var filePath:String = "http://www.project-nya.jp/images/wonderfl/seminarpiyo.png";
        private var water:WaterEffect;
        private var perspective:PerspectiveMap;

        public function ProyectNyaFlag() {
            //Wonderfl.capture_delay(1);
            init();
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
        }

        private function init():void {
            loader = new ImageLoader();
            loader.addEventListener(Event.COMPLETE, complete, false, 0, true);
            //loader.load(filePath, true);
            complete(null);
        }
        private function complete(evt:Event):void {
            loader.removeEventListener(Event.COMPLETE, complete);
            //
            var gri:Sprite = new Sprite;
            var gra:Graphics = gri.graphics;
            gra.lineStyle(0);
            for (var i:int = 0; i < 46; i++) 
            {
                gra.moveTo(i * 10, 0);
                gra.lineTo(i * 10, 465);
            }
            for (var j:int = 0; j < 46; j++) 
            {
                gra.moveTo(0,j * 10);
                gra.lineTo(465,j * 10);
            }
            var bitmapData:BitmapData = new BitmapData(465, 465);
            bitmapData.draw(gri);
            
            //var bitmap:Bitmap = Bitmap(evt.target.content);
            var bitmap:Bitmap = new Bitmap(bitmapData);
            //
            var rect:Rectangle = new Rectangle(0, 0, bitmap.width, bitmap.height);
            water = new WaterEffect(rect);
            addChild(water);
            //water.x = 32;
            //water.y = 32;
            water.initialize(bitmap);
            water.setup(20, 3);
            water.update();
            //
            perspective = new PerspectiveMap(water.bitmapData.rect, 2);
            perspective.transform(water.bitmapData);
            //
            addEventListener(Event.ENTER_FRAME, update, false, 0, true);
            //
            loader = null;
        }
        private function update(evt:Event):void {
            water.update();
            perspective.transform(water.bitmapData);
        }

    }

}


//////////////////////////////////////////////////
// WaveEffectクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.filters.DisplacementMapFilter;
import flash.display.BitmapDataChannel;
import flash.filters.DisplacementMapFilterMode;
import flash.filters.BlurFilter;

class WaterEffect extends Sprite {
    private var rect:Rectangle;
    private var noise:PerlinNoise;
    private static var octaves:uint = 2;
    private static var channel:uint = BitmapDataChannel.RED;
    private var speeds:Array;
    private static var point:Point = new Point();
    private var scale:Number = 10;
    private var amplitude:Number = 3;
    private var map:DisplacementMapFilter;
    private var target:DisplayObject;
    private var source:BitmapData;
    public var bitmapData:BitmapData;

    public function WaterEffect(r:Rectangle) {
        rect = r;
        init();
    }

    private function init():void {
        noise = new PerlinNoise(rect, 32, 32, octaves, false, channel);
        bitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
        var bitmap:Bitmap = new Bitmap(bitmapData);
        addChild(bitmap);
        setup(scale, amplitude);
    }
    public function initialize(t:DisplayObject, matrix:Matrix = null):void {
        target = t;
        source = new BitmapData(rect.width, rect.height, true, 0x00000000);
        source.lock();
        source.draw(target, matrix, null, null, null, true);
        source.unlock();
        bitmapData.lock();
        bitmapData.draw(source);
        bitmapData.unlock();
    }
    public function setup(s:Number, a:Number):void {
        scale = s;
        amplitude = a;
        speeds = new Array();
        for (var n:uint = 0; n < octaves; n++) {
            var sx:Number = (Math.random() - 0.5)*amplitude;
            var sy:Number = (Math.random() - 0.5)*amplitude + 2.5;
            speeds.push(new Point(sx, sy));
        }
        map = new DisplacementMapFilter(noise, point, channel, channel, scale, scale, DisplacementMapFilterMode.CLAMP);
    }
    public function update():void {
        noise.update(speeds);
        bitmapData.lock();
        bitmapData.applyFilter(source, rect, point, map);
        bitmapData.unlock();
    }
    public function blur(size:Number = NaN, quality:uint = 3):void {
        var _blur:BlurFilter = new BlurFilter(size, size, quality);
        bitmapData.lock();
        bitmapData.applyFilter(bitmapData, rect, point, _blur);
        bitmapData.unlock();
    }

}


//////////////////////////////////////////////////
// PerlinNoiseクラス
//////////////////////////////////////////////////

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

class PerlinNoise extends BitmapData {
    private var bx:uint;
    private var by:uint;
    private var octaves:uint;
    private var seed:uint;
    private var stitch:Boolean = true;
    private var fractalNoise:Boolean = true;
    private var channel:uint = 0;
    private var grayScale:Boolean = true;
    private var offsets:Array = new Array();

    public function PerlinNoise(rect:Rectangle, x:uint, y:uint, o:uint = 1, g:Boolean = true, c:uint = 0, s:uint = 1, st:Boolean = false, f:Boolean = true) {
        super(rect.width, rect.height, false, 0xFF000000);
        bx = x;
        by = y;
        octaves = o;
        grayScale = g;
        channel = c;
        if (grayScale) channel = 0;
        for (var n:uint = 0; n < octaves; n++) {
            var point:Point = new Point();
            offsets.push(point);
        }
        stitch = st;
        fractalNoise = f;
        create(s, offsets);
    }

    private function create(s:uint, o:Array = null):void {
        seed = s;
        offsets = o;
        if (offsets == null) offsets = [new Point()];
        perlin();
    }
    public function update(speeds:Array):void {
        for (var n:uint = 0; n < octaves; n++) {
            var offset:Point = offsets[n];
            var speed:Point = speeds[n];
            offset.x += speed.x;
            offset.y += speed.y;
        }
        perlin();
    }
    private function perlin():void {
        lock();
        perlinNoise(bx, by, octaves, seed, stitch, fractalNoise, channel, grayScale, offsets);
        draw(this);
        unlock();
    }

}


//////////////////////////////////////////////////
// PerspectiveMapクラス
//////////////////////////////////////////////////

import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.filters.DisplacementMapFilter;
import flash.filters.DisplacementMapFilterMode;

class PerspectiveMap {
    private var rect:Rectangle;
    private var scale:Number;
    private var perspective:DisplacementMapFilter;

    public function PerspectiveMap(r:Rectangle, s:Number) {
        rect = r;
        scale = s;
        if (scale > 1) init();
    }

    private function init():void {
        var map:BitmapData = new BitmapData(rect.width, rect.height, false);
        var n:Number = 1/(scale - 1);
        var t:Number = n + 1;
        var mx:Number = 0;
        var my:Number = 0;
        for (var py:uint = 0; py < rect.height; py++) {
            var pr:Number = (py + 1)/rect.height;
            var pf:Number = pr/(pr + n);
            var pdx:Number = pf/2;
            var pdy:Number = pf*t - pr;
            mx = Math.max(mx, pdx);
            my = Math.max(my, pdy);
        }
        var xscale:Number = 127/256/mx;
        var yscale:Number = 127/256/ my;
        for (var y:uint = 0; y < rect.height; y++) {
            var r:Number = (y + 1)/rect.height;
            var f:Number = r/(r + n);
            var dy:Number = f*t - r;
            var cy:uint = Math.round(256*dy*yscale + 128) << 8;
            for (var x:uint = 0; x < rect.width; x++) {
                var dx:Number = (0.5 - x/rect.width)*f;
                var cx:uint = Math.round(256*dx*xscale + 128) | cy;
                map.setPixel(x, y, cx);
            }
        }
        perspective = new DisplacementMapFilter(map, null, 4, 2, rect.width/xscale, rect.height/yscale, DisplacementMapFilterMode.IGNORE);
    }
    public function transform(bd:BitmapData):void {
        if (scale == 1) return;
        bd.applyFilter(bd, rect, new Point(), perspective);
    }

}


//////////////////////////////////////////////////
// ImageLoaderクラス
//////////////////////////////////////////////////

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

class ImageLoader extends Sprite {
    private var loader:Loader;
    private var info:LoaderInfo;
    public var content:Bitmap;
    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 ImageLoader() {
        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(ImageLoader.IO_ERROR));
    }
    private function httpstatus(evt:HTTPStatusEvent):void {
        dispatchEvent(new Event(ImageLoader.HTTP_STATUS));
    }
    private function securityerror(evt:SecurityErrorEvent):void {
        dispatchEvent(new Event(ImageLoader.SECURITY_ERROR));
    }
    private function initialize(evt:Event):void {
        content = Bitmap(info.content);
        if (smoothing) content.smoothing = true;
        dispatchEvent(new Event(ImageLoader.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(ImageLoader.COMPLETE));
    }

}