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

お絵かきアプリ作りたいな。

title

@author 
@version

[SWF(width="600", height="600", backgroundColor="0xFFFFFF", frameRate="30")]
Get Adobe Flash player
by syunsuke 25 Dec 2008
    Embed
// write as3 code here..
package 
{
	import flash.display.*;
	import flash.filters.*;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	/**
	 * title
	 * 
	 *
	 * @author 
	 * @version
	 */
	//[SWF(width="600", height="600", backgroundColor="0xFFFFFF", frameRate="30")]  
	public class DrawingApp extends Sprite 
	{
		//メンバ変数 
		private var clearBtn:TextField;
		private var kesigomuBtn:TextField;
		private var x_Array:Array;
		private var y_Array:Array;
		private var line:Shape;
		private var line_width:int;
		private var mX:uint ;
		private var mY:uint ;
		
		private var begin_p:Point;
		private var end_p:Point
		
		private var bmp:Bitmap;
		private var canvas:BitmapData;
		
		private var pen_Flag:Boolean = false;
		
		private var size:int = 50;
		private var density:int = 300;
		
		
		
		//コンストラクタ
		public function DrawingApp():void
		{
			trace("コンストラクタ : DrawingApp")
			addEventListener( Event.ADDED_TO_STAGE, init );
		}
		
		private function init(e:Event):void
		{
			removeEventListener( Event.ADDED_TO_STAGE, init );
			
			stage.frameRate = 100;
			stage.quality = "BEST";
			//stage.quality = StageQuality.LOW;
			stage.scaleMode = StageScaleMode.NO_SCALE
			stage.align = StageAlign.TOP_LEFT;
			
			clearBtn = makeTextField('Clear');
			clearBtn.x = 30;
			kesigomuBtn = makeTextField('消しゴム');
			kesigomuBtn.x = clearBtn.x + clearBtn.width + 10 ;
			
			x_Array = new Array();
			y_Array = new Array();
			
			//framerate = new FrameRateCounter(0xFF0000)
			
			begin_p = new Point;
			end_p = new Point;
			line = new Shape();
			
			canvas = new BitmapData(1000 , 1000 , true , 0xFFFFFFFF);
			bmp = new Bitmap(canvas);
			
			addChild(line);
			addChild(bmp);
			addChild(clearBtn);
			addChild(kesigomuBtn);
			
			line.graphics.lineStyle(1 , 0 , 1);
			
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN , onKey_Down);
			stage.addEventListener(MouseEvent.MOUSE_DOWN , onMouse_Down);
			stage.addEventListener(MouseEvent.MOUSE_UP , onMouse_Up);

			//btn.addEventListener(MouseEvent.MOUSE_DOWN , on_btnMouse_Down);
			clearBtn.addEventListener(MouseEvent.MOUSE_DOWN , on_clearMouse_Down);
			kesigomuBtn.addEventListener(MouseEvent.MOUSE_DOWN , on_btnMouse_Down);
		}
		
		private function onKey_Down(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case Keyboard.LEFT:
				//vr = -5;
				if (line_width>= 0)
				{
					line_width -= 1
				}
				break;
				
				case Keyboard.RIGHT:
				
					line_width += 1
				

				break;
				
				case Keyboard.UP:
	
				break;
				
				case Keyboard.DOWN:

				break;
				
				default:
				break;
			}
		}
		
		private function on_btnMouse_Down(e:MouseEvent):void
		{
			if (pen_Flag == false)
			{
				pen_Flag = true

			}else{
				pen_Flag = false

			}
			
		}
		
		private function on_clearMouse_Down(e:MouseEvent):void
		{
			pen_Flag = false
			canvas.dispose();
			init(null);
		}
		
		private function onMouse_Down(e:MouseEvent):void
		{
			trace("pen_Flag : "+pen_Flag)
			
			begin_p.x = stage.mouseX;
            begin_p.y = stage.mouseY;
			//stage.addEventListener(Event.ENTER_FRAME , drawLine);
			stage.addEventListener(MouseEvent.MOUSE_MOVE , on_EnterFrame)
		}
		
		private function onMouse_Up(event:MouseEvent):void
		{
			//stage.removeEventListener(Event.ENTER_FRAME , drawLine);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE , on_EnterFrame)
		}
		
		private function on_EnterFrame(e:Event):void
		{
			drawLine();
		}
		
		public function drawLine():void
		{

			
			end_p.x = mouseX;
			end_p.y = mouseY;
			
			x_Array.push(stage.mouseX)
			y_Array.push(stage.mouseY)
			var control_point_x = x_Array[x_Array.length - 3]
			var control_point_y = x_Array[y_Array.length - 3]

			//trace(x_Array[x_Array.length])
			
			if (pen_Flag == true)
			{
				/*
				for (var i:int = 0 ;  i < density ; i++ )
				{
					var angle:Number = Math.random() * Math.PI * 2
					var radius:Number = Math.random() * size;
					var xpos:Number = mouseX + Math.cos(angle) * radius;
					var ypos:Number = mouseY + Math.sin(angle) * radius;
					canvas.setPixel32(xpos , ypos , 0x50000099);
				}
				*/
				line.graphics.lineStyle(line_width , 0xFFFFFF , 1);
				line.graphics.moveTo(begin_p.x , begin_p.y);
				line.graphics.lineTo(end_p.x , end_p.y );
				/*
				line.graphics.curveTo(control_point_x , control_point_y , x_Array[x_Array.length - 2] , x_Array[y_Array.length - 2])
				//*/
				canvas.draw(line);
				line.graphics.clear();
				begin_p = end_p.clone();
			}else{
				line.graphics.lineStyle(line_width , 0 , 1);
				line.graphics.moveTo(begin_p.x , begin_p.y);
				line.graphics.lineTo(end_p.x , end_p.y );
				/*
				line.graphics.curveTo(control_point_x , control_point_y , x_Array[x_Array.length - 2] , x_Array[y_Array.length - 2])
				//*/
				canvas.draw(line);
				line.graphics.clear();
				begin_p = end_p.clone();
			}

		}
		
		private function makeTextField(str:String):TextField {
			var tf:TextField = new TextField();
			tf.text = str;

			var tf1:TextFormat = new TextFormat();
			tf1.color = 0xffffff;
			tf1.size = 14;
			tf1.font = 'Arial';
			tf1.align = TextFormatAlign.CENTER;
			tf.setTextFormat(tf1);

			tf.background = true;
			tf.backgroundColor = 0x666666;
			tf.border = true;
			tf.borderColor = 0x000000;
			tf.width = 50;
			tf.height = 20;
			return tf;
		}
		
	}
	
}