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

Particle Clock Optimized

This is an optimized version of my dot clock code. It is using setVector instead of setPixel along with a couple other tweaks to try and squeeze as much performance as I can out of it with pure AS.
/**
 * Copyright IStillLikeFlash ( http://wonderfl.net/user/IStillLikeFlash )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/vXRa
 */

package {
    import __AS3__.vec.Vector;
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.filters.GlowFilter;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.utils.Timer;
    
    import net.hires.debug.Stats;

    [SWF(width="270", height="90", backgroundColor="0x000000", frameRate="24")]
    public class DotTextOptimator extends Sprite
    {
        private static var WIDTH:int = 280;
        private static var HEIGHT:int = 90;
        private const MAX_DOTS:int = 256 * 16;
        //private static const MAX_DOTS:int = 4;
        private const ZERO_POINT:Point = new Point(0, 0);
        private const DOT_COLOR:uint = 0xFFFF0000;
        
        private var bitmapData:BitmapData;
        private var bitmapRect:Rectangle;
        private var bitmap:Bitmap;
        
        private var glow:GlowFilter;
        
        private var textBitmapData:BitmapData;
        private var textPos:Point;
        private var textVector:Vector.<uint>;
        
        private var dots:Vector.<Dot>;
        private var text:TextField;
        
        private var timer:Timer = new Timer(1000);
        
        public function DotTextOptimator()
        {
            this.addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private final function init(event:Event):void
        {
            this.removeEventListener(Event.ADDED_TO_STAGE, init);
            
            //this.scaleX = 0.5;
            //this.scaleY = 0.5;
            
            //WIDTH = stage.width;
            //HEIGHT = stage.height;
            
            glow = new GlowFilter(0xff0000, 1, 16, 16, 3);
            
            var backgroundBD:BitmapData = new BitmapData(WIDTH, HEIGHT, false, 0x000000);
            var background:Bitmap = new Bitmap(backgroundBD);
            backgroundBD.fillRect(backgroundBD.rect, 0x000000);
            addChild(background);
            
            bitmapData = new BitmapData(WIDTH, HEIGHT, true, 0x00000000);
            bitmapRect = bitmapData.rect;
            bitmap = new Bitmap(bitmapData);
            
            addChild(bitmap);
            
            dots = new Vector.<Dot>();
            for (var i:int = 0; i < MAX_DOTS; i++)
            {
                var dot:Dot = new Dot();
                dot.x = Math.round(Math.random() * (WIDTH - 5));
                dot.y = Math.round(Math.random() * HEIGHT);
                dot.color = 0xffff0000;
                dot.dx = 0;
                dot.dy = 0.5 + Math.random() * 3;
                
                dots.push(dot);
            }
            
            text = new TextField();
            text.x = 10;
            text.y = 3;
            
            var format:TextFormat = new TextFormat();
            //format.font = "Verdana";
            format.color = 0xFFFFFF;
            format.size = 70;
            
            text.width = WIDTH;
            text.height = HEIGHT;
            text.defaultTextFormat = format;
            text.setTextFormat(format);
            text.background = false;
 
            textPos = new Point(text.x, text.y);
            
            this.addEventListener(Event.ENTER_FRAME, firstTime);
            timer.addEventListener(TimerEvent.TIMER, timerHandler);
            timer.start();
            
            //addChild(new Stats());
        }
        
        private final function timerHandler(event:TimerEvent):void
        {
            var currentTime:Date = new Date();     
            var minutes:Number = currentTime.getMinutes();
            var seconds:Number = currentTime.getSeconds();
            var hours:Number = currentTime.getHours();
            
            
            var newText:String = "";
            if (hours < 10)
            {
                newText += "0" + hours.toString();
            }
            else
            {
                newText += hours.toString();
            }
            
            newText += ":";
            if (minutes < 10)
            {
                newText += "0" + minutes.toString();
            }
            else
            {
                newText += minutes.toString();
            }
            newText += ":";
            if (seconds < 10)
            {
                newText += "0" + seconds.toString();
            }
            else
            {
                newText += seconds.toString();
            }
            text.text = newText;
            
            textBitmapData.fillRect(textBitmapData.rect, 0x00000000);
            textBitmapData.draw(text);
            textVector = textBitmapData.getVector(textBitmapData.rect);
        }
        
        private final function firstTime(event:Event):void
        {
            this.removeEventListener(Event.ENTER_FRAME, firstTime);
            textBitmapData = new BitmapData(text.width, text.height, true, 0x00000000);
            textBitmapData.draw(text);
            textVector = textBitmapData.getVector(textBitmapData.rect);
            
            this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }
        
        private final function enterFrameHandler(event:Event):void
        {    
            //var start:uint = getTimer();
            bitmapData.lock();
            bitmapData.fillRect(bitmapRect, 0x00000000);
            
            var vec:Vector.<uint> = bitmapData.getVector(bitmapRect);
            var w:int = bitmapRect.width;
            var h:int = bitmapRect.height;
            
            var dot:Dot;
            for each (dot in dots)
            {
                dot.y += dot.dy;
                
                if (dot.y > HEIGHT)
                {
                    dot.y = 0;
                }
                
                var dotx:int = int(dot.x);
                var doty:int = int(dot.y);


                if (textVector[int(doty * WIDTH + dotx)] != 0)
                {
                    if (Math.random() < 0.5)
                    {
                        vec[int(w * doty + int(dotx+1))] = DOT_COLOR;
                    }
                    else
                    {
                        vec[int(w * doty + int(dotx-1))] = DOT_COLOR;
                    }
                    
                    vec[int(w * doty + dotx)] = DOT_COLOR;
                    
                    if (Math.random() < 0.5)
                    {
                        vec[int(w * int(doty+1) + dotx)] = DOT_COLOR;
                    }
                    else
                    {
                        vec[int(w * int(doty-1) + dotx)] = DOT_COLOR;
                    }
                    
                    
                }
                else
                {
                    if (Math.random() < 0.006)
                    {
                        dot.color = 0xFFFFFFFF;
                    }
                    else
                    {
                        dot.color = 0x66FF0000;
                    }

                    vec[int(w * int(doty) + int(dotx))] = dot.color;
                }    
            }
            
            bitmapData.setVector(bitmapRect, vec);
            bitmapData.applyFilter(bitmapData, bitmapRect, ZERO_POINT, glow);
            
            bitmapData.unlock();
            //trace((getTimer() - start), "ms");
        }
        
    }
}


final class Dot
{
    public var x:Number;
    public var y:Number;
    public var dx:Number;
    public var dy:Number;
    public var color:uint;
}