How to draw lines between two points in js

Source: Internet
Author: User

How to draw lines between two points in js

This article describes how to draw lines between two points in JavaScript. Share it with you for your reference. The specific analysis is as follows:

I have been bored recently. I have been thinking about it for a long time and thought of the idea of killing time, that is, the js version of continuous viewing.

Draw a line between two points is only part of the most basic function of the link, so the line I draw is only a line, but also can only fold to the left, the line direction will be determined based on the position of the picture in the continuous view.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"

Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>

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

<Head>

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

<Title> draw a line between two points </title>

<Style type = "text/css">

Body {

Font-size: 12px;

}

</Style>

</Head>

<Script type = "text/javascript">

<! --

Var dot = new Array ();

Document. onmousedown = function ()

{

// Delete a div if it exists

If (document. getElementById ('line _ div ')){

Var clearDiv = document. getElementById ("line_div ");

ClearDiv. parentNode. removeChild (clearDiv );

}

// Create an event when you press it.

If (! A) a = window. event;

// Obtain the X and Y axes

Var x = a. clientX;

Var y = a. clientY;

// When the array length is greater than or equal to 4, clear the Array

If (dot. length> = 4) dot. splice (0, 4 );

// Add x and y to the array. The array stores two sets of coordinate values, which are equivalent to two points on the page: A (x1, y1) and B (x2, y2 ).

Dot. push (x, y );

// Draw a line when the array length is 4.

If (dot. length = 4 ){

// Calculate the length and width of the div

Var width = Math. abs (dot [0]-dot [2]);

Var height = Math. abs (dot [1]-dot [3]);

// Locate the specific position in the upper left corner of the div on the page

Var left = dot [0]-dot [2] <0? Dot [0]: dot [2];

Var top = dot [1]-dot [3] <0? Dot [1]: dot [3];

// Create a div

Var div = document. createElement ("div ");

Div. innerHTML = '<div id = "line_div" style = "width:' + width + 'px; height: '+ height + 'px; position: absolute; visibility: visible; left: '+ left + 'px; top:' + top + 'px; border-left: 1px solid # cdcdcd; border-top: 1px solid # cdcdcd; "> </div> ';

Document. body. appendChild (div );

}

}

-->

</Script>

<Body>

</Body>

</Html>

I hope this article will help you design javascript programs.

Related Article

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.