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

perlinNoise play with controls and movement

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

// forked from cpu_t's perlinNoise遊び
// クリックでリセット。
// 
// rが明るさ
// gがx方向の移動距離
// bがy方向の移動距離
// 
package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import net.hires.debug.Stats;
    [SWF(width = 465, height = 465)];
    public class PerlinNoiseFlashTest extends Sprite
    {
        public function PerlinNoiseFlashTest()
        {
            var widthL:Number = stage.stageWidth>>1;
            var heightL:Number  = stage.stageWidth>>1;
            vectormap = new BitmapData(widthL, heightL);
            
            bmpdata = new BitmapData(widthL, heightL, false, 0);
            bmpdataPerlinNoise = new BitmapData(widthL, heightL, false, 0);
            addChild(new Bitmap(bmpdata));
            var bitmap:Bitmap = Bitmap(addChild(new Bitmap(bmpdataPerlinNoise)));
            bitmap.bitmapData = vectormap;
            bitmap.x = widthL+1;
            //stage.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void
            //{
                //reset();
            //});
            model = new PerlinNoiseObject();
            model.baseX = widthL;
            model.baseY = heightL;
            
            reset();
            var a:DisplayObject = addChild(new Stats());
            a.y = stage.stageHeight - a.height;
            new Additions(this);
        }
        
        private var vectormap:BitmapData;
        private var bmpdata:BitmapData;
        private var vectormapPerlinNoise:BitmapData;
        private var bmpdataPerlinNoise:BitmapData;
        public var model:PerlinNoiseObject;
        
        public function reset(e:MouseEvent=null):void
        {
            //var color:uint = uint(0x010101 * model.colorRatio);
            var color:uint = 0x010101;
            //color = 0x010101;
            //trace( "color : " + color );
            //trace( "color : " + ((color-1)/256) );
            //trace( "model.colorRatio : " + model.colorRatio );
            var fractalNoise:Boolean = model.fractalNoise;
            var seed:Number = model.seed;
            //trace( "seed : " + seed );
            var stitch:Boolean = model.stitch;
            var channelOptions:uint = model.channelOptions;
            var grayScale:Boolean = model.grayScale;
            var offsets:Array = model.offsets;
            //trace( "offsets : " + offsets );
            var numOctaves:uint = model.numOctaves;
            var baseX:Number = model.baseX;
            var baseY:Number = model.baseY;
            //vectormap.perlinNoise(465 * .5, 465 * .5, 3, Math.random() * 0xFFFFFFFF, false, true);
            vectormap.perlinNoise(baseX, baseY, numOctaves, seed, stitch, fractalNoise, channelOptions, grayScale, offsets);
            //bmpdataPerlinNoise.perlinNoise(465 * .5, 465 * .5, 3, seed, stitch, fractalNoise, channelOptions, grayScale, offsets);
            // bmpdataをクリア
            bmpdata.fillRect(bmpdata.rect, 0);
            var tx:int;
            var ty:int;
           
            // vectormapの全ピクセルに対してループ
            for (var y:int = 0; y < vectormap.height; y++)
            {
                for (var x:int = 0; x < vectormap.width; x++)
                {
                    // rgb要素を取得
                    var px:uint = vectormap.getPixel(x, y);
                    var r:int = px >> 16 & 0xFF;
                    var g:int = px >> 8 & 0xFF;
                    var b:int = px & 0xFF;
                    
                    // x,yにg,bを足す
                    // x,y to g,b plus
                    //tx = x ;
                    tx = x + (g / 0xFF - .5) * 465;
                    //ty = y ;
                    ty = y + (b / 0xFF - .5) * 465;
                    
                    // bmpdataの範囲外になってしまったらcontinue
                    if (ty<0 || ty>=bmpdata.height || tx<0 || tx>=bmpdata.width && model.outOfRange) continue;
                    var col:uint = bmpdata.getPixel(tx, ty);
                    col += color * (int)(r * .2);
                    if (col > 0xFFFFFF) col = 0Xffffff;
                    bmpdata.setPixel(tx, ty, col);
                }
            }
            
            //trace( "tx : " + tx );
            //trace( "ty : " + ty );
            //trace( "y : " + y );
            //trace( "x : " + x );
        }
    }
}
import caurina.transitions.Tweener;
import caurina.transitions.TweenListObj;
import com.bit101.components.CheckBox;
import com.bit101.components.Component;
import com.bit101.components.HBox;
import com.bit101.components.HUISlider;
import com.bit101.components.Label;
import com.bit101.components.NumericStepper;
import com.bit101.components.PushButton;
import com.bit101.components.VBox;
import flash.display.BitmapData;
import flash.display.DisplayObjectContainer;
import flash.events.Event;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.ui.Keyboard;
import flash.utils.Timer;
class PerlinNoiseObject
{
    public var fractalNoise:Boolean = true;
    private var _seed:Number = Math.random() * 0xffffffff;
    public var stitch:Boolean = false;
    public var channelOptions:uint = 7;
    public var grayScale:Boolean = true;
    public var offsets:Array;
    public var baseX:Number;
    public var baseY:Number;
    public var autoReset:Boolean = true;
    public var randomizeSeed:Boolean;
    public var numOctaves:uint=3;
    public var outOfRange:Boolean = true;
    public var colorRatio:Number = 0.003;
    public function PerlinNoiseObject()
    {
    }
    public function perlinBitmapData():void
    {
        var bitmapData:BitmapData = new BitmapData(baseX, baseY, true, 0xffffffff);
        //bitmapData.perlinNoise(baseX,baseY,)
    }
    
    public function get seed():Number 
    {
        return randomizeSeed?Math.random()*0xffffffff:_seed;
    }
    
    public function setRandomSeed():void 
    {
        _seed =  Math.random() * 0xffffffff;
    }
    
}
class Additions
{
    static public const MAX_NUM_OCTAVES:uint=10;
    private var controls1:VBox;
    private var hidden:Boolean;
    private var main:PerlinNoiseFlashTest;
    private var m:PerlinNoiseObject;
    private var sliders:Array;
    private var offsets:Array;
    private var angle:Number;
    private var radian:Number=Math.PI / 180;
    private var radiusX:Number;
    private var radiusY:Number;
    private var freqX:Number;
    private var freqY:Number;
    private var octave:int;
    private var aKey:Number;
    public function Additions(mainArg:PerlinNoiseFlashTest)
    {
        main = mainArg;
        m = main.model;
        Sprite(main.addChild(new Sprite));
        manageListeners(true);
        controls1 = new VBox(main, 0, 0);
        hidden = false;
        addComponent(new PushButton(null, 0, 0, "reset", onSopas));
        
        var fracNois:HBox = HBox(addComponent(new HBox(null, 0, 0)));
        addToComponent(fracNois, new Label(null, 0, 0, "fractal Noise"));
        var cFractalNoise:CheckBox = CheckBox(addToComponent(fracNois, new CheckBox(null, 0, 0, "", onFractalNoise)));
        cFractalNoise.selected = m.fractalNoise;
        
        var stitchin:HBox = HBox(addComponent(new HBox(null, 0, 0)));
        addToComponent(stitchin, new Label(null, 0, 0, "stitch"));
        var cStitch:CheckBox = CheckBox(addToComponent(stitchin, new CheckBox(null, 0, 0, "", onStichin)));
        cStitch.selected = m.stitch;
        
        var grayScalin:HBox = HBox(addComponent(new HBox(null, 0, 0)));
        addToComponent(grayScalin, new Label(null, 0, 0, "gray scale"));
        var cGrayScale:CheckBox = CheckBox(addToComponent(grayScalin, new CheckBox(null, 0, 0, "", onGrayScale)));
        cGrayScale.selected = m.grayScale;
        
        var channelOpts:HBox = HBox(addComponent(new HBox(null, 0, 0)));
        addToComponent(channelOpts, new Label(null, 0, 0, "Channel options"));
        var cChannelOptions:NumericStepper = NumericStepper(addToComponent(channelOpts, new NumericStepper(null, 0, 0, onChannelOption)));
        cChannelOptions.minimum = 0;
        cChannelOptions.maximum = 7;
        cChannelOptions.value = m.channelOptions;
        
        var numOct:HBox = HBox(addComponent(new HBox(null, 0, 0)));
        addToComponent(numOct, new Label(null, 0, 0, "Octaves"));
        var cNumOctaves:NumericStepper = NumericStepper(addToComponent(numOct, new NumericStepper(null, 0, 0, onNumOctaves)));
        cNumOctaves.minimum = 0;
        cNumOctaves.maximum = MAX_NUM_OCTAVES;
        cNumOctaves.value = m.numOctaves;
        
        var outOfRanger:HBox = HBox(addComponent(new HBox(null, 0, 0)));
        addToComponent(outOfRanger, new Label(null, 0, 0, "Out of Range"));
        var cOutOfRange:CheckBox = CheckBox(addToComponent(outOfRanger, new CheckBox(null, 0, 0, "", onOutOfRange)));
        cOutOfRange.selected = m.outOfRange;
        
        var autoResettin:HBox = HBox(addComponent(new HBox(null, 0, 0)));
        addToComponent(autoResettin, new Label(null, 0, 0, "Auto reset"));
        var cAutoReset:CheckBox = CheckBox(addToComponent(autoResettin, new CheckBox(null, 0, 0, "", onAutoReset)));
        cAutoReset.selected = m.autoReset;
        
        var randomizin:HBox = HBox(addComponent(new HBox(null, 0, 0)));
        addToComponent(randomizin, new Label(null, 0, 0, "Randomize seed"));
        var cRandomize:CheckBox = CheckBox(addToComponent(randomizin, new CheckBox(null, 0, 0, "", onRandomize)));
        cRandomize.selected = m.randomizeSeed;
        
        m.offsets= [];
        sliders = [];
        
        setOffsets();
        
        populateOffsets();
        showOffsetsSliders();
        
        /*
            var coloring:HBox = HBox(addComponent(new HBox(null, 0, 0)));
            addToComponent(coloring, new Label(null, 0, 0, "Color"));
            var sCHUISlider:SCHUISlider = SCHUISlider(addToComponent(coloring,new SCHUISlider(null, 0, 0, "color", onColor)));
            sCHUISlider.labelPrecision = 6;
            sCHUISlider.value = m.colorRatio;
            trace( "sCHUISlider.value : " + sCHUISlider.value );
        */
        angle = 0;
        radiusX = 200+Math.random() * 40;
        radiusY = 200+Math.random() * 40;
        freqX = Number( ( Math.random() * 7 ).toFixed(1) );
        freqY = Number( ( Math.random() * 7 ).toFixed(1) );
        octave = 1;
        aKey = String("A").charCodeAt();
        var timer:Timer = new Timer(100, 1);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, hideControls, false, 0, true);
        timer.start();
        main.addEventListener(Event.ENTER_FRAME, loop);
    }
    
    private function hideControls(e:TimerEvent):void 
    {
        onKeyBoardDown(new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, false, 0,aKey ));
    }
    
    private function loop(e:Event):void 
    {
        Point(m.offsets[octave]).x = radiusX * Math.sin(freqX * angle);
        Point(m.offsets[octave]).y = radiusY * Math.sin(freqY * angle);
        angle += 0.5* radian;
        doAutoReset();
    }
    
    private function onColor(e:Event):void 
    {
        m.colorRatio = SCHUISlider(e.target).value;
        doAutoReset();
    }
    
    private function onOutOfRange(e:Event):void 
    {
        m.outOfRange = CheckBox(e.target).selected;
        doAutoReset();
    }
    
    private function populateOffsets():void 
    {
        var sliderX:SCHUISlider;
        var sliderY:SCHUISlider;
        var hBox:HBox;
        sliders.length = 0;
        for (var i:int = 0; i < MAX_NUM_OCTAVES; i++) 
        {
            hBox = HBox(addComponent(new HBox(null, 0, 0)));
            hBox.addEventListener(MouseEvent.MOUSE_DOWN, offsetMouseDownHandle);
            sliderX = SCHUISlider(addToComponent(hBox,new SCHUISlider(null, 0, 0, "x offSet of " + i, onOffsetsHandle)));
            sliderY = SCHUISlider(addToComponent(hBox,new SCHUISlider(null, 0, 0, "y offSet of " + i, onOffsetsHandle)));
            sliderX.name = "x,"+i.toString();
            sliderY.name = "y," + i.toString();
            sliderX.minimum = sliderY.minimum = -1000;
            sliderX.maximum = sliderY.maximum = 1000;
            sliders[sliders.length] = hBox;
        }
    
    }
    
    private function offsetMouseDownHandle(e:Event):void 
    {
        main.stage.addEventListener(MouseEvent.MOUSE_UP, offsetMouseUpHandle);
    }
    
    private function offsetMouseUpHandle(e:MouseEvent):void 
    {
        
        main.stage.removeEventListener(MouseEvent.MOUSE_UP, offsetMouseUpHandle);
        //doAutoReset();
    }
    
    
    private function setOffsets():void 
    {
        var output:Array = [];
        for (var i:int = 0; i < MAX_NUM_OCTAVES; i++) 
        {
            m.offsets[i] = new Point;
        }
        //trace( "m.offsets : " + m.offsets );
    }
    
    private function onOffsetsHandle(e:Event):void 
    {
        var sCHUISlider:SCHUISlider = SCHUISlider(e.target);
        //trace( "SCHUISlider(e.target).name : " + SCHUISlider(e.target).name );
        var s:Array = sCHUISlider.name.split(",");
        var coordIndex:String = String(s[0]);
        var index2:Number = int(s[1]);
        Point(m.offsets[index2])[coordIndex] = sCHUISlider.value;
        //trace( "Point(m.offsets[index2]) : " + Point(m.offsets[index2]) );
        //trace( "m.offsets : " + m.offsets );
        doAutoReset();
    }
    
    private function onNumOctaves(e:Event):void 
    {
        m.numOctaves = NumericStepper(e.target).value;
        showOffsetsSliders();
        doAutoReset();
    }
    
    private function showOffsetsSliders():void 
    {
        for (var i:int = 0; i < MAX_NUM_OCTAVES; i++) 
        {
            HBox(sliders[i]).visible = i < m.numOctaves?true:false;
        }
    }
    
    private function onChannelOption(e:Event):void 
    {
        m.channelOptions = NumericStepper(e.target).value;
        doAutoReset();
    }
    
    private function onRandomize(e:Event):void 
    {
        m.randomizeSeed = CheckBox(e.target).selected;
        if (CheckBox(e.target).selected) 
        {
            m.setRandomSeed();
        }
        doAutoReset();
    }
    
    private function onGrayScale(e:Event):void 
    {
        m.grayScale = CheckBox(e.target).selected;
        doAutoReset();
    }
    
    private function onStichin(e:Event):void 
    {
        m.stitch = CheckBox(e.target).selected;
        doAutoReset();
    }
    
    private function onAutoReset(e:Event):void 
    {
        m.autoReset = CheckBox(e.target).selected;
        doAutoReset();
    }
    
    private function onFractalNoise(e:Event):void 
    {
        m.fractalNoise = CheckBox(e.target).selected;
        doAutoReset();
    }
    
    private function doAutoReset():void 
    {
        if (m.autoReset) main.reset();
    }
    
    private function manageListeners(bAdd:Boolean):void 
    {
        trace( "Additions.addKeyBoardListener > bAdd : " + bAdd );
        if (bAdd) 
        {
            
            main.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyBoardDown);
            main.stage.removeEventListener(MouseEvent.MOUSE_DOWN, main.reset);
        }
        else
        {
            main.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyBoardDown);
            main.stage.addEventListener(MouseEvent.MOUSE_DOWN, main.reset);
            
        }
        if (hidden && bAdd)
        {
            main.stage.addEventListener(MouseEvent.MOUSE_DOWN, main.reset);
        }
        else
        {
            main.stage.removeEventListener(MouseEvent.MOUSE_DOWN, main.reset);
        }
        
    }
    private function onKeyBoardDown(e:KeyboardEvent):void 
    {
        manageListeners(false)
        if (e.keyCode==aKey) 
        {
            if (hidden) 
            {
                Tweener.addTween(controls1, { x:0, y:0, time:1, transition:"easeOutQuint", onCompleteParams:[true],onComplete:manageListeners } )
            }
            else
            {
                Tweener.addTween(controls1, { x:-controls1.width-5, y:0, time:1, transition:"easeOutQuint", onCompleteParams:[true],onComplete:manageListeners } )
            }
        }
        hidden = !hidden;
    }
    
    private function addComponent(arg1:Component):Component 
    {
        return Component(controls1.addChildAt(arg1, controls1.numChildren));
    }
    
    private function addToComponent(container:Component, content:Component):Component 
    {
        return Component(container.addChildAt(content, container.numChildren));
    }
    
    private function onSopas(e:Event):void 
    {
        main.reset();
    }
    
}

class SCHUISlider extends HUISlider 
{
    
    public function SCHUISlider(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, label:String = "", defaultHandler:Function = null) 
    {
        super(parent, xpos, ypos, label, defaultHandler);
        
    }
    override public function draw():void 
    {
        super.draw();
        _slider.x = _label.width + 5;
        _slider.y = height / 2 - _slider.height / 2;
        //_slider.width = width - _label.width - 50 - 10;
        
        _valueLabel.x = _slider.x + _slider.width + 5;
    }
}