Sample Code for drawing lines with the mouse using JavaScript

Source: Internet
Author: User

Sample Code for drawing lines with the mouse using JavaScript

 

01 <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

02 <Html xmlns = "http://www.w3.org/1999/xhtml">

03

04 <Head>

05 <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

06 <Title> Untitled 1 </title>

07 <Style type = "text/css">

08 . Style1 {

09 Font-size: x-small;

10 }

11 </Style>

12 <Script type = "text/javascript">

13 /**

14 Painting point

15 */

16 Function makedot (x, y ){

17 PointDiv = "<div style = 'height: 1px; position: absolute; left:" + x +

18 "Px; top:" + y + "px; width: 1px; background: # f00; overflow: Den den '> </div> ";

19 Return pointDiv;

20 }

21 /**

22 Draw a straight line based on two-point coordinates.

23 */

24

25 Function line (x1, y1, x2, y2 ){

26 Var slope; // slope

27 Var direction; // Coordinate motion direction

28 Var tx = x2-x1;

29 Var ty = y2-y1;

30 If (tx = 0 & ty = 0) return;

31 Var points = "";

32 Var axis; // coordinates on the coordinate axis

33 If (Math. abs (tx)> = Math. abs (ty) {// move on the X axis

34 Direction = tx> 0? 1:-1;

35 Tx = Math. abs (tx );

36 Slope = ty/tx;

37 Axis = x1;

38 For (I = 0; I <tx; I ++ ){

39 Points + = makedot (axis, y1 + I * slope );

40 Axis + = direction;

41 }

42

43 } Else {// move on the Y axis

44 Direction = ty> 0? 1:-1;

45 Ty = Math. abs (ty );

46 Slope = tx/ty;

47 Axis = y1;

48 For (I = 0; I <ty; I ++ ){

49 Points + = makedot (x1 + I * slope, axis );

50 Axis + = direction;

51 }

52 }

53 Var container = document. getElementById ("container ");

54 Container. innerHTML + = points;

55 }

56 Var oldPoint = null;

57 // Obtain the mouse position

58 Function mousePosition (ev ){

59 Ev = ev | window. event;

60 If (ev. pageX | ev. pageY ){

61 Return {x: ev. pageX, y: ev. pageY };

62 }

63 Var doc = document.doc umentElement, body = document. body;

64 Var pageX = event. clientX + (doc & doc. scrollLeft | body & body. scrollLeft | 0)-(doc & doc. clientLeft | body & body. clientLeft | 0 );

65 Var pageY = event. clientY + (doc & doc. scrollTop | body & body. scrollTop | 0)-(doc & doc. clientTop | body & body. clientTop | 0 );

66 Return {x: pageX, y: pageY };

67 }

68

69 Function recordPoint (ev ){

70

71 Var point = mousePosition (ev );

72 If (oldPoint! = Null ){

73 Line (oldPoint. x, oldPoint. y, point. x, point. y );

74 }

75 OldPoint = point;

76 }

77 </Script>

78 </Head>

79

80 <Body>

81 <Div id = "container" style = "width: 1000px; height: 600px; border: 1px # bfbfbf solid;" onclick = "recordPoint (event);">

82

83 </Div>

84 <Script type = "text/javascript">

85 // Line (22,300 );

86 </Script>

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.