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

ドット (1) [60fps]

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

// forked from ProjectNya's ドット (1)
////////////////////////////////////////////////////////////////////////////////
// ドット (1)
////////////////////////////////////////////////////////////////////////////////

package {

    import flash.display.Sprite;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.system.LoaderContext;
    import flash.system.Security;
    import net.hires.debug.Stats;

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

    public class Main extends Sprite {
        private var base:Sprite;
        private var canvas:BitmapData;
        private var dots:Array;
        private var filePath:String = "http://www.project-nya.jp/images/flash/piyo.png";

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

        private function init():void {
            Security.allowDomain("www.project-nya.jp");
            Security.loadPolicyFile("http://www.project-nya.jp/crossdomain.xml");
            base = new Sprite();
            base.graphics.beginFill(0x000000);
            base.graphics.drawRect(0, 0, 465, 465);
            base.graphics.endFill();
            addChild(base);
            canvas = new BitmapData(465, 465, false, 0xFF000000);
            var bitmap:Bitmap = new Bitmap(canvas);
            addChild(bitmap);
            var loader:Loader=new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete, false, 0, true);
            loader.load(new URLRequest(filePath), new LoaderContext(true));
        }
        private function complete(evt:Event):void {
            var content:Bitmap = evt.target.content;
            content.x = int((465 - content.width)/2);
            content.y = int((465 - content.height)/2);
            initialize(content);
        }
        private function initialize(bitmap:Bitmap):void {
            var bitmapData:BitmapData = bitmap.bitmapData;
            dots = new Array();
            for (var y:uint = 0; y < bitmapData.height; y++) {
                for (var x:uint = 0; x < bitmapData.width; x++) {
                    var tx:uint = x + bitmap.x;
                    var ty:uint = y + bitmap.y;
                    var dot:Dot = new Dot(tx, ty);
                    var color:uint = bitmapData.getPixel(x, y);
                    dot.color = color;
                    dots.push(dot);
                }
            }
            base.buttonMode = true;
            base.addEventListener(MouseEvent.CLICK, click, false, 0, true);
        }
        private function click(evt:MouseEvent):void {
            base.mouseEnabled = false;
            base.useHandCursor = false;
            start();
        }
        private function start():void {
            for (var n:uint = 0; n < dots.length; n++) {
                var dot:Dot = dots[n];
                dot.x = uint(Math.random()*800) - 400 + 232;
                dot.y = uint(Math.random()*800) - 400 + 232;
                dot.completed = false;
            }
            addEventListener(Event.ENTER_FRAME, update, false, 0, true);
        }
        private function stop():void {
            removeEventListener(Event.ENTER_FRAME, update);
            base.mouseEnabled = true;
            base.useHandCursor = true;
        }
        private function update(evt:Event):void {
            canvas.lock();
            canvas.fillRect(canvas.rect, 0xFF000000);
            var completed:uint = 0;
            for (var n:uint = 0; n < dots.length; n++) {
                var dot:Dot = dots[n];
                dot.x += (dot.tx - dot.x)*0.2;
                dot.y += (dot.ty - dot.y)*0.2;
                if (!dot.completed) {
                    canvas.setPixel(dot.x, dot.y, dot.color);
                } else {
                    completed ++;
                    if (completed > dots.length - 1) {
                        stop();
                    }
                }
                if (Math.abs(dot.tx - dot.x) < 0.5 && Math.abs(dot.ty - dot.y) < 0.5) {
                    dot.x = dot.tx;
                    dot.y = dot.ty;
                    dot.completed = true;
                    canvas.setPixel(dot.x, dot.y, dot.color);
                }
            }
            canvas.unlock();
        }

    }

}


//////////////////////////////////////////////////
// Dotクラス
//////////////////////////////////////////////////

class Dot {
    public var x:Number = 0;
    public var y:Number = 0;
    public var tx:int = 0;
    public var ty:int = 0;
    public var color:uint;
    public var completed:Boolean = false;

    public function Dot(x:int, y:int) {
        tx = x;
        ty = y;
    }

}