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

Spiral monster

Just having fun and headaches with logarithmic spirals...

Click on the image to generate a new one. Better in fullscreen!
Get Adobe Flash player
by Kodiak 27 Apr 2011
/**
 * Copyright Kodiak ( http://wonderfl.net/user/Kodiak )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/894N
 */

package {
    import flash.events.MouseEvent;
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    
    
    public class Main extends Sprite 
    {
        private var m__background:Sprite;
        private var m__view:BitmapData;
        private var m__display:Bitmap;
        private var m__tracers:/*Tracer*/Array;
        
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            m__background = new Sprite();
            m__background.graphics.beginFill(0xFFFFFF);
            m__background.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
            addChild(m__background);
            m__background.buttonMode = true;
            m__background.addEventListener(MouseEvent.CLICK, restart);
            
            m__display = new Bitmap();
            addChild(m__display);
            
            restart();
        }
        
        private function restart(e:Event = null):void
        {            
            m__tracers = new Array();
            var l__tracer:Tracer;
            for (var l__iTracer:int = 0; l__iTracer < 12; l__iTracer++)
            {
                l__tracer = new Tracer(stage.stageWidth/2, stage.stageHeight/2,
                                        (.5 * Math.random() + .25) * stage.stageWidth, (.5 * Math.random() + .25) * stage.stageHeight,
                                        Math.random()*4+1);
                l__tracer.addEventListener(Event.COMPLETE, onTracerComplete);
                m__tracers.push(l__tracer);
            }
            
            m__view = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        
        private function onTracerComplete(e:Event):void 
        {
            (e.currentTarget as Tracer).removeEventListener(Event.COMPLETE, onTracerComplete);
            m__tracers.splice(m__tracers.indexOf(e.currentTarget), 1);
        }
        
        private function onEnterFrame(e:Event):void 
        {
            for each(var l__tracer:Tracer in m__tracers)
            {
                l__tracer.step();
                m__view.draw(l__tracer);
            }
            m__display.bitmapData = m__view;
        }
        
    }
 }
 
 import flash.events.Event;
import flash.display.Shape;
import flash.geom.Point;
 
class Tracer extends Shape 
{
    private var m__shapeX:int;
    private var m__shapeY:int;
    private var m__t:uint = 1;
    private var m__centerX:int;
    private var m__centerY:int;
    private var m__startX:int;
    private var m__startY:int;
    private var m__direction:int;
    private var m__yFactor:int;
    
    private const STEPS:uint = 200;
    private var m__spins:uint;
    private var m__step:Number;
    
    
    private var m__distance:Number;
    private var m__phi:Number;
    private var m__angle:Number;
    private var m__cosAngle:Number;
    private var m__sinAngle:Number;
    
    public function Tracer(i__startX:Number, i__startY:Number,i__centerX:Number,i__centerY:Number, i__spins:uint=3) 
    {
        m__startX = i__startX;
        m__startY = i__startY;
        m__centerX = i__centerX;
        m__centerY = i__centerY;
        
        m__spins = i__spins;
        m__step = (m__spins * Math.PI * 2)/STEPS;
        
        var l__start:Point = new Point(m__startX, m__startY);
        var l__center:Point = new Point(m__centerX, m__centerY);
        var l__virtualStart:Point = new Point(m__centerX, m__centerY);
        
        m__distance = Point.distance(l__start, l__center);
        m__phi = Math.pow(m__distance, 1 / (2 * m__spins));
        
        l__virtualStart.x -= m__distance;
        var l__h:Number = Point.distance(l__start, l__virtualStart);
        
        m__angle = 2 * Math.asin(l__h / (2 * m__distance));
        m__cosAngle = Math.cos(m__angle);
        m__sinAngle = Math.sin(m__angle);
        m__yFactor = m__startY > m__centerY ? -1 : 1;
        
        m__shapeX = m__startX;
        m__shapeY = m__startY;
        
        m__direction = Math.random() > .5? -1:1;
    }
    
    public function step():void
    {
        graphics.clear();
        graphics.moveTo(m__shapeX, m__shapeY);
        
        var l__theta:Number = m__spins*Math.PI*2 - m__t * m__step;
        
        var l__pow:Number = Math.pow(m__phi, (l__theta / Math.PI));
        
        var l__tempX:Number = l__pow * Math.cos(l__theta);
        var l__tempY:Number = l__pow * Math.sin(l__theta * m__direction);
        
        m__shapeX = m__centerX - (l__tempX * m__cosAngle + l__tempY * m__sinAngle);
        m__shapeY = m__centerY + m__yFactor * (-l__tempX * m__sinAngle + l__tempY * m__cosAngle);
        
        graphics.lineStyle(l__theta * l__theta / (m__spins*10), 0, .5);
        
        graphics.lineTo(m__shapeX, m__shapeY);
        
        m__t ++;
        
        /*graphics.lineStyle(0);
        graphics.beginFill(0xFF0000);
        graphics.drawCircle(m__startX, m__startY, 5);
        graphics.endFill();
        graphics.beginFill(0x00FF00);
        graphics.drawCircle(m__centerX, m__centerY, 5);
        graphics.endFill();*/
        
        if (l__theta <= 0)
        {
            dispatchEvent(new Event(Event.COMPLETE));
        }
    }
}