ActionScript 3 mouse painting

Source: Internet
Author: User
This article uses AS3 to describe the curve, straight line, circle, ellipse, and rectangle. I hope this will help you! (1): curve knowledge points: 1. Add the mouse to listen to the event MouseEvent; 2. Specify the canvas size; 3. moveTo and lineTo; 4. Draw the start and end judgment. Code: var huabu: Sprite = new Sprite (); huabu. graphics. beginFill (0 xFFFFFF); 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, m Oving); 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): line knowledge point: 1. Add the mouse to listen to the event MouseEvent; 2. Specify the canvas size; 3. moveTo and lineTo; 4. Draw the start and end judgment. Difficulty: Draw multiple lines of code: var huabu: Sprite = new Sprite (); huabu. graphics. beginFill (0 xFFFFFF); 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. addEvent Listener (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, _ col Or); 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); // you can draw only one straight line (_ drawing = false) without the preceding two sentences;} (3): Circle knowledge point: 1. Add the mouse to listen to the event MouseEvent; 2. display the radius (Point distance between two points) during painting. distance (coordinate 1, coordinate 2); 3. drawCircle, moveTo, and lineTo; 4. Draw start and end judgment. Difficulty: Draw multiple circles: 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. MOUS E_UP, stopDrawing); function startDrawing (event: MouseEvent): void {yuanxin_x = mouseX; // The cursor is 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); // the distance between two points is displayed as 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 when the painting ends. banjing = new Sprite (); // The radius is displayed during each circle painting. addChil D (banjing); yuan = new Sprite (); // you can draw multiple circles addChild (yuan); _ drawing = false;} (4): elliptical knowledge point: 1. Add the mouse listening event MouseEvent and keyboard listening event KeyboardEvent; 2. width and height of the ellipse; 3. drawEllipse; 4. Draw start and end judgment; 5. Press Shift to draw a positive circle. Difficulty: press Shift to draw the 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: MouseEven T): 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 (); addChi Ld (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 a mouse listener event MouseEvent and a keyboard listener event KeyboardEvent; 2. width and height of the ellipse; 3. drawRect; 4. start and end of the painting; 5. Press Shift to draw a square. Difficulty: press Shift 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 );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.