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

BitmapDataでお絵かき

BitmapDataにお絵かきします。
絵は勝手に消えていきますが、Cキーを押すことで全消去することもできます。
Get Adobe Flash player
by cpu_t 17 Feb 2010
    Embed
/**
 * Copyright cpu_t ( http://wonderfl.net/user/cpu_t )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/sGv8
 */

// BitmapDataにお絵かきします。
// 絵は勝手に消えていきますが、Cキーを押すことで全消去することもできます。
// 
package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
    import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.ui.Keyboard;
    public class FlashTest extends Sprite {
		private var bmpdata:BitmapData;
		private var isMousedown:Boolean=false;
		
        public function FlashTest() {
			bmpdata = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0);
            addChild(new Bitmap(bmpdata));
			stage.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void
			{
				isMousedown = true;
			});
			stage.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent):void
			{
				isMousedown = false;
			});
			stage.addEventListener(Event.ENTER_FRAME, loop);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
        }
		
		private function keyDownHandler(e:KeyboardEvent):void 
		{
			if (e.keyCode == 67) // C
			{
				bmpdata.fillRect(bmpdata.rect, 0x000000);
			}
		}
		
		private var lastMousePos:Point = new Point();
		private var cnt:Number = 0;
		private var colortrans:ColorTransform = new ColorTransform(.99, .99, .99);
		
		private function loop(e:Event):void
		{
			bmpdata.colorTransform(bmpdata.rect, colortrans);
			if (isMousedown)
			{
				if (++cnt % 360 == 0) cnt = 0;
				var c:uint;
				if (cnt <= 60 || cnt >= 300)
				{
					c = 0xFF0000;
					if (cnt <= 60) c += 0x00FF00 * cnt / 60 & 0x00FF00;
					else c += 0x0000FF * (360 - cnt) / 60 & 0x0000FF;
				}
				else if (cnt >= 60 && cnt <= 180)
				{
					c = 0x00FF00;
					if (cnt <= 120) c += 0xFF0000 * (120 - cnt) / 60 & 0xFF0000;
					else c += 0x0000FF * (cnt - 120) / 60 & 0x0000FF;
				}
				else if (cnt >= 180 && cnt <= 300)
				{
					c = 0x0000FF;
					if (cnt <= 240) c += 0x00FF00 * (240 - cnt) / 60 & 0x00FF00;
					else c += 0xFF0000 * (cnt - 240) / 60 & 0xFF0000;
				}
				
				
				drawLine(bmpdata, lastMousePos, new Point(mouseX, mouseY), c);
			}
			lastMousePos.x = mouseX;
			lastMousePos.y = mouseY;
		}
		private function drawLine(bmpdata:BitmapData, startPos:Point, endPos:Point, color:uint):void
		{
			var x:Number = endPos.x - startPos.x;
			var y:Number = endPos.y - startPos.y;
			var m:Number = (y == 0 || Math.abs(x / y) >= 1) ? x : y;
			for (var i:Number = 0; i / m <= 1; i += sign(m))
			{
				bmpdata.setPixel(startPos.x + x / m * i, startPos.y + y / m * i, color);
			}
		}
		private function sign(n:Number):Number
		{
			return n == 0 ? 0 : (n > 0 ? 1 : -1);
		}
    }
}