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

Mafioronoi

Clean up & Tweaks
/**
 * Copyright FLASHMAFIA ( http://wonderfl.net/user/FLASHMAFIA )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/77bi
 */

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.filters.ColorMatrixFilter;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    [SWF(width = '512', height = '512')]
    public class Mafiaronoi11 extends Sprite
    {
        private const NUM_ZONES : uint = 32;
        /* */
        private var cps : Vector.<ControlPoint> = new Vector.<ControlPoint>;
        public var bm : Bitmap;
        private var bmd : BitmapData;
        private var dbuff : BitmapData;
        private var dmap : BitmapData;
        private var cmtx1 : ColorMatrixFilter = new ColorMatrixFilter([1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 1, 0]);
        private var cmtx2 : ColorMatrixFilter = new ColorMatrixFilter([1, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0]);
        private var dstp : Point = new Point();

        function Mafiaronoi11()
        {
            stage.stageFocusRect = mouseEnabled = tabEnabled = tabChildren = false;
            stage.scaleMode = 'noScale';
            stage.align = 'TL';
            stage.quality = 'low';
            stage.frameRate = 32;
            opaqueBackground = 0x0;

            /* */

            var w : int = 512 >> 1;
            var h : int = 512 >> 1;

            bmd = new BitmapData(w, h, false);
            dbuff = new BitmapData(w, h, false);

            addChild(bm = new Bitmap(bmd));
            bm.scaleX = 512 / w;
            bm.scaleY = 512 / h;
            bm.x = (512 >> 1) - bm.scaleX * (w >> 1);
            bm.y = (512 >> 1) - bm.scaleY * (h >> 1);
            bm.opaqueBackground = 0x0;

            /* distance map */

            dmap = new BitmapData(w << 1, h << 1, false);

            var r : Number = 255 * 1.414 / w;
            for (var px : int = -w; px < w; px++)
            {
                for (var py : int = -w; py < w; py++)
                {
                    var d : int = int(Math.sqrt(px * px + py * py) * r);
                    dmap.setPixel(px + w, py + w, ((d < 0xFF) ? d : 0xFF));
                }
            }

            bm.x = 0;
            bm.y = 0;

            var cp : ControlPoint;

            for (var i : int = 0; i < NUM_ZONES; i++)
            {
                cps.push(cp = new ControlPoint());

                cp.color = (0xFFFFFFFF * (0.9 + 0.1 * Math.random())) >> 0;

                cp.x = 512 * Math.random();
                cp.y = 512 * Math.random();

                cp.vx = 1.0 + 3.0 * Math.random();
                if (Math.random() > 0.5) cp.vx = -cp.vx;

                cp.vy = 1.0 + 3.0 * Math.random();
                if (Math.random() > 0.5) cp.vy = -cp.vy;
            }

            stage.addEventListener(Event.ENTER_FRAME, oef);
        }

        private function oef(e : Event) : void
        {
            var isx : Number = 1 / bm.scaleX;
            var isy : Number = 1 / bm.scaleY;

            bmd.lock();

            dbuff.fillRect(dbuff.rect, 0xFFFFFF);

            for each (var cp : ControlPoint in cps)
            {
                /* draw */
                
                dstp.x = (cp.x - bm.x) * isx - bmd.width;
                dstp.y = (cp.y - bm.y) * isy - bmd.height;

                dbuff.copyChannel(dmap, dmap.rect, dstp, 4, 4);

                dstp.x = 0.0;
                dstp.y = 0.0;

                dbuff.applyFilter(dbuff, dbuff.rect, dstp, cmtx1);

                bmd.threshold(dbuff, dbuff.rect, dstp, '>', 0, cp.color, 0xFF);

                dbuff.applyFilter(dbuff, dbuff.rect, dstp, cmtx2);

                /* animate cps */

                cp.x += cp.vx;
                cp.y += cp.vy;

                if ((cp.x < 0) || (cp.x > 512)) cp.vx *= -1;
                if ((cp.y < 0) || (cp.y > 512)) cp.vy *= -1;
            }

            bmd.unlock();
        }
    }
}

internal class ControlPoint
{
    public var color : uint;
    public var x : Number = 0.0;
    public var y : Number = 0.0;
    public var sx : Number = 1.0;
    public var sy : Number = 1.0;
    public var vx : Number = 0.0;
    public var vy : Number = 0.0;
}