js implements the method of drawing lines between two points

  • 2020-06-12 08:27:42
  • OfStack

An example of js shows how to draw a line between two points. Share to everybody for everybody reference. The specific analysis is as follows:

Recently, I was a little bored. After a long consideration, I came up with the idea of killing time 1, which is to do the js version of repeatedly watching.

The line drawn between two points is only part 1 of the most basic function of lianlianjian, so the line I draw is only a fold line, and can only fold to the left. The direction of the fold line will be determined according to the position points of lianlianjian pictures.


<!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(a) 
{ 
 // if div Exists, then delete 
 if(document.getElementById('line_div')){
 var clearDiv=document.getElementById("line_div");
 clearDiv.parentNode.removeChild(clearDiv);
 }
 // Create when pressed 1 An event 
 if(!a) a=window.event;
 // To obtain x Shaft, y Axis coordinates 
 var x=a.clientX;
 var y=a.clientY;
 // When the array length is greater than or equal to 4 , clears the array 
 if(dot.length>=4) dot.splice(0,4);
 // will x , y Add to array , The array holds two sets of coordinates, equivalent to those on the page A(x1,y1) , B(x2,y2) At two o 'clock 
 dot.push(x,y);
 // When the array length is zero 4 ", draw a line. 
 if(dot.length==4){
 // To calculate div The length and width 
 var width=Math.abs(dot[0]-dot[2]);
 var height=Math.abs(dot[1]-dot[3]);
 // Position on the page div The exact location in the upper left corner 
 var left=dot[0]-dot[2]<0?dot[0]:dot[2];
 var top=dot[1]-dot[3]<0?dot[1]:dot[3];
 // create 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>

Hopefully, this article has been helpful in your javascript programming.


Related articles: