In this paper, 5 kinds of drawing methods of curve, straight line, circle, ellipse and rectangle are depicted by AS3, hoping to bring help to everyone.
(1): Curve
Knowledge Points:
1, add mouse Monitor event mouseevent;
2, the provision of the canvas size;
3, MoveTo and LineTo;
4, draw the beginning and end judgment.
Code:
var huabu:sprite=new Sprite ();
Huabu.graphics.beginFill (0xFFFFFF);
Huabu.graphics.drawRect (20,20,510,360);
Huabu.graphics.endFill ();
AddChild (Huabu);
var _drawing:boolean;
var quxian:sprite=new Sprite ();
Quxian.graphics.lineStyle (2, 0xff0000);
Huabu.addchild (Quxian);
_drawing = false;
Huabu.addeventlistener (Mouseevent.mouse_down, startdrawing);
Huabu.addeventlistener (mouseevent.mouse_move,moving);
Huabu.addeventlistener (mouseevent.mouse_up, stopdrawing);
Stage.addeventlistener (mouseevent.mouse_up,stopdrawing);
function startdrawing (event:mouseevent): void {
quxian.graphics.moveTo (mousex, mousey);
_drawing = true;
}
function Moving (event:mouseevent): void {
if (_drawing) {
quxian.graphics.lineTo (mousex, Mousey);
}
}
function stopdrawing (event:mouseevent): void {
_drawing = false;
}
(2): Straight line
Knowledge Points:
1, add mouse Monitor event mouseevent;
2, the provision of the canvas size;
3, MoveTo and LineTo;
4, draw the beginning and end judgment.
Difficulties:
Draw more than one line
Code:
var huabu:sprite=new Sprite ();
Huabu.graphics.beginFill (0xFFFFFF);
Huabu.graphics.drawRect (20,20,510,360);
Huabu.graphics.endFill ();
AddChild (Huabu);
var _drawing:boolean=false;
var zhixian:sprite=new Sprite ();
Huabu.addchild (Zhixian);
var _color:number=0xff0000;
var zx:int=1;
var Ys_x:number;
var Ys_y:number;
Huabu.addeventlistener (Mouseevent.mouse_down, startdrawing);
Huabu.addeventlistener (mouseevent.mouse_move,moving);
Huabu.addeventlistener (mouseevent.mouse_up, stopdrawing);
Stage.addeventlistener (mouseevent.mouse_up, stopdrawing);
function startdrawing (event:mouseevent): void {ys_x=mousex;
Ys_y=mousey;
_drawing = true;
} function Moving (event:mouseevent): void {if (_drawing) {huabu.removechild (Zhixian);
Zhixian=new Sprite ();
Zhixian.graphics.lineStyle (ZX, _color);
Zhixian.graphics.moveTo (ys_x,ys_y);
Zhixian.graphics.lineTo (Mousex,mousey);
Huabu.addchild (Zhixian); }} function Stopdrawing (event:mouseevent): void {zhixian=New Sprite ();
Huabu.addchild (Zhixian);
No more than two sentences, can only draw a line _drawing = false;
}
(3): Round
Knowledge Points:
1, add mouse Monitor event mouseevent;
2, the display radius (two points between the distance point.distance (coordinates 1, coordinates 2));
3, Drawcircle, MoveTo and LineTo;
4, draw the beginning and end judgment.
Difficulties:
Draw multiple Circles
Code:
var _drawing:boolean;
var yuan:sprite=new Sprite ();
AddChild (yuan);
var banjing:sprite=new Sprite ();
AddChild (banjing);
var Yuanxin_x:number;
var Yuanxin_y:number;
var zuobiao1:point;
var zuobiao2:point;
var Bj:number;
_drawing = false;
Stage.addeventlistener (Mouseevent.mouse_down, startdrawing);
Stage.addeventlistener (Mouseevent.mouse_move,yd);
Stage.addeventlistener (mouseevent.mouse_up, stopdrawing);
function startdrawing (event:mouseevent): void {yuanxin_x=mousex;//mouse position is the position of the center of the circle Yuanxin_y=mousey;
_drawing = true;
} function yd (event:mouseevent): void {if (_drawing) {zuobiao1 = new point (yuanxin_x,yuanxin_y);
Zuobiao2 = new Point (MouseX, mousey);
bj= point.distance (Zuobiao1, ZUOBIAO2);//show the distance between two points is the radius removechild (yuan);
Yuan=new Sprite ();
Yuan.graphics.lineStyle (2, 0xff0000);
Yuan.graphics.drawCircle (YUANXIN_X,YUANXIN_Y,BJ);
AddChild (yuan);
RemoveChild (banjing);
Banjing=new Sprite (); Banjing.graphics.lineStyle (2,0xff0000,0.5);
Banjing.graphics.moveTo (yuanxin_x,yuanxin_y);
Banjing.graphics.lineTo (Mousex,mousey);
AddChild (banjing); } function Stopdrawing (event:mouseevent): void {removechild (banjing);//The RADIUS line disappears at the end of painting banjing=new Sprite ();
Radius addChild (banjing);
Yuan=new Sprite ()//can draw multiple round addChild (yuan);
_drawing = false; }
(4): Oval
Knowledge Points:
1, add mouse Monitor event MouseEvent and keyboard monitoring event keyboardevent;
2, the width and height of the ellipse;
3, DrawEllipse;
4, draw the beginning and end judgment;
5. Press the SHIFT key to draw the positive circle.
Difficulties:
Press the SHIFT key to draw a positive circle
Code:
var _drawing:boolean=false;
var ellipse:sprite=new Sprite ();
AddChild (ellipse);
var Kaishi_x:number;
var Kaishi_y:number;
var W:number;
var H:number;
var _max:number;
Stage.addeventlistener (Mouseevent.mouse_down, startdrawing);
Stage.addeventlistener (mouseevent.mouse_move,moving);
Stage.addeventlistener (mouseevent.mouse_up, stopdrawing);
function startdrawing (event:mouseevent): void {kaishi_x=mousex;
Kaishi_y=mousey;
_drawing = true;
} function Moving (event:mouseevent): void {if (_drawing) {w=mousex-kaishi_x;
h=mousey-kaishi_y;
RemoveChild (ellipse);
Ellipse=new Sprite ();
Ellipse.graphics.lineStyle (2, 0xff0000);
Ellipse.graphics.drawEllipse (KAISHI_X,KAISHI_Y,W,H);
AddChild (ellipse);
} function Stopdrawing (event:mouseevent): void {ellipse=new Sprite ();
AddChild (ellipse);
_drawing = false;
} stage.focus=this;
Stage.addeventlistener (Keyboardevent.key_down, shift); function Shift (Event:keyboardevent): void {if (event.charcode==0) {if (_drawing) {w=mousex-kaishi_x;
h=mousey-kaishi_y;
_max=math.max (Math.Abs (w), Math.Abs (h));
if (w<0 && h<0) {W=-_max;
H=-_max;
} if (w>0 && h>0) {W=_max;
H=_max;
} if (w<0 && h>0) {W=-_max;
H=_max;
} if (w>0 && h<0) {W=_max;
H=-_max;
} removechild (ellipse);
Ellipse=new Sprite ();
Ellipse.graphics.lineStyle (2, 0xff0000);
Ellipse.graphics.drawEllipse (KAISHI_X,KAISHI_Y,W,H);
AddChild (ellipse); }
}
}
(5): Rectangular
Knowledge Points:
1, add mouse Monitor event MouseEvent and keyboard monitoring event keyboardevent;
2, the width and height of the ellipse;
3, DrawRect;
4, draw the beginning and end judgment;
5, press the SHIFT key to draw the square.
Difficulties:
Press the SHIFT key to draw the square
Code:
var _drawing:boolean=false;
var _drawrect:sprite=new Sprite ();
AddChild (_drawrect);
var Yx_x:number;
var Yx_y:number;
var W:number;
var H:number;
var _max:number;
Stage.addeventlistener (Mouseevent.mouse_down, startdrawing);
Stage.addeventlistener (mouseevent.mouse_move,moving);
Stage.addeventlistener (mouseevent.mouse_up, stopdrawing);
function startdrawing (event:mouseevent): void {yx_x=mousex;
Yx_y=mousey;
_drawing = true;
} function Moving (event:mouseevent): void {if (_drawing) {w=mousex-yx_x;
h=mousey-yx_y;
RemoveChild (_drawrect);
_drawrect=new Sprite ();
_drawrect.graphics.linestyle (2, 0xff0000);
_drawrect.graphics.drawrect (YX_X,YX_Y,W,H);
AddChild (_drawrect);
} function Stopdrawing (event:mouseevent): void {_drawrect=new Sprite ();
AddChild (_drawrect);
_drawing = false;
} stage.focus=this;
Stage.addeventlistener (Keyboardevent.key_down, shift); function Shift (Event:keyboardevent): void {if (event.charcode==0) {if (_draWing) {_max=math.max (Math.Abs (w), Math.Abs (h));
if (w<0 && h<0) {W=-_max;
H=-_max;
} if (w>0 && h>0) {W=_max;
H=_max;
} if (w<0 && h>0) {W=-_max;
H=_max;
} if (w>0 && h<0) {W=_max;
H=-_max;
} removechild (_drawrect);
_drawrect=new Sprite ();
_drawrect.graphics.linestyle (2, 0xff0000);
_drawrect.graphics.drawrect (YX_X,YX_Y,W,H);
AddChild (_drawrect); }
}
}
Original address: http://www.68design.net/Web-Guide/Flash/53258-1.html