BitmapData.lock()/unlock()

by ProjectNya
////////////////////////////////////////////////////////////////////////////////
// BitmapData.lock()/unlock()
//
// [AS3.0] BitmapData.lock()/unlock()
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1085
////////////////////////////////////////////////////////////////////////////////
♥0 | Line 56 | Modified 2010-10-07 15:24:25 | MIT License
play

ActionScript3 source code

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

////////////////////////////////////////////////////////////////////////////////
// BitmapData.lock()/unlock()
//
// [AS3.0] BitmapData.lock()/unlock()
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1085
////////////////////////////////////////////////////////////////////////////////

package {

    import flash.display.Sprite;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.geom.Rectangle;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import frocessing.color.ColorHSV;
    import net.hires.debug.Stats;

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

    public class Main extends Sprite {
        private var bitmapData:BitmapData;
        private var bitmap:Bitmap;
        private var locked:Boolean = false;
        private var color:ColorHSV;
        private var id:uint = 0;
        private var txt:TextField;

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

        private function init():void {
            graphics.beginFill(0x000000);
            graphics.drawRect(0, 0, 465, 465);
            graphics.endFill();
            //
            bitmapData = new BitmapData(465, 465, true, 0x00000000);
            bitmap = new Bitmap(bitmapData);
            addChild(bitmap);
            color = new ColorHSV();
            txt = new TextField();
            txt.x = 385;
            txt.y = 445;
            addChild(txt);
            txt.textColor = 0xFFFFFF;
            txt.text = "locked: " + String(locked);
            //
            stage.addEventListener(MouseEvent.CLICK, click, false, 0, true);
            addEventListener(Event.ENTER_FRAME, update, false, 0, true);
        }
        private function click(evt:MouseEvent):void {
            locked = !locked;
            txt.text = "locked: " + String(locked);
        }
        private function update(evt:Event):void {
            if (locked) bitmapData.lock();
            color.h = id;
            for (var n:uint = 0; n < 465; n++) {
                for (var t:uint = 0; t < 465; t++) {
                    bitmapData.setPixel32(t, n, 0xFF << 24 | color.value);
                }
            }
            if (locked) bitmapData.unlock();
            id ++;
        }

    }

}