<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>获取坐标点位置,并连接点</title>
<style>
*{margin:0 ;padding:0;}
.svgcontainer,.container{position:absolute;top:50%;left:50%;margin-left:-400px;margin-top:-250px;width:800px;height:500px;}
.container{border:1px solid #ccc;cursor: pointer;}
.dot{width:10px;height:10px;border-radius:50%;position:absolute;background:#0078B6;margin-top:-5px;margin-left:-5px;}
.initdot{top:0;left:0;}
</style>
</head>
<body>
<div class="svgcontainer" id="svgcontainer"></div>
<div class="container" id="container">
<div class="dot initdot"></div>
</div>
</body>
</html>
<script>
var container=document.getElementById("container");
var svgcontainer=document.getElementById("svgcontainer");
var svgns="http://www.w3.org/2000/svg";
var svger=document.createElementNS(svgns,"svg");
svger.setAttribute("width",container.clientWidth);
svger.setAttribute("height",container.clientHeight);
svger.setAttribute("viewBox","0 0 "+container.clientWidth+" "+container.clientHeight);
svgcontainer.appendChild(svger);
container.onclick=function(e){
var e=e||window.event;
var mousePosition=mousePos(e);
creatdot(mousePosition.x,mousePosition.y);
var dots=container.children;
linedot(dots[dots.length-2],dots[dots.length-1]);
}
function intpixel(str){
return str.substring(0,str.length-2)*1;
}
function mousePos(e){
if(e.pageX){
return {x:e.pageX,y:e.pageY}
}else{
return {x:e.clientX+document.body.scrollLeft-document.body.clientLeft,
y:e.clientY+document.body.scrollTop-document.body.clientTop}
}
}
function creatdot(posX,posY){
var newposX=posX-container.offsetLeft;
var newposY=posY-container.offsetTop;
var dot=document.createElement("div");
dot.setAttribute("class","dot");
dot.style.left=newposX+"px";
dot.style.top=newposY+"px";
container.appendChild(dot);
}
function linedot(dot1,dot2){
var start={x:intpixel(dot1.style.left),y:intpixel(dot1.style.top)};
var end={x:intpixel(dot2.style.left),y:intpixel(dot2.style.top)};
var current={x:start.x,y:start.y};
var line=document.createElementNS(svgns,"line");
line.setAttribute("x1",dot1.style.left);
line.setAttribute("y1",dot1.style.top);
line.setAttribute("x2",dot1.style.left);
line.setAttribute("y2",dot1.style.top);
line.setAttribute("stroke","red");
line.setAttribute("fill","none");
svger.appendChild(line);
var tangle={
sin:(end.y-start.y)/Math.sqrt((end.y-start.y)*(end.y-start.y)+(end.x-start.x)*(end.x-start.x)),
cos:(end.x-start.x)/Math.sqrt((end.y-start.y)*(end.y-start.y)+(end.x-start.x)*(end.x-start.x))
};
var step=function(){
if(Math.abs(current.x-end.x)<10&&Math.abs(current.y-end.y)<10){
current.x=end.x;
current.y=end.y;
}else{
current.x+=10*tangle.cos;
current.y+=10*tangle.sin;
var timepath=setTimeout(step,17);
}
line.setAttribute("x2",current.x+"px");
line.setAttribute("y2",current.y+"px");
}
step();
}
</script>