SVG轨迹基础

html代码 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<button name="button" onclick="input()" value="请输入坐标" style="width: 40;height: 30; color: green;">请输入坐标</button>
		<button name="button" onclick="findScale()" style="width: 40;height: 30; color: green;">查询历史轨迹</button>
		 <button name="button" onclick="clearScale()" style="width: 40;height: 30; color: green;">清除轨迹</button>
		
		<div>
		<embed src="xml/map.svg" id="embed" width="640" height="640" type="image/svg+xml"></embed>
		</div>
	</body>
	<script>
	    var scaleX="";//运动轨迹坐标
		var scaleY="";//运动轨迹坐标
		var scaleX0="";//初始坐标
		var scaleY0="";//初始坐标
	    var scaleXY=""; //生成轨迹属性值
	    var scaleHistory=[];
	    var scaleXY_history="";//记录历史坐标起点
	    var nameSpace = 'http://www.w3.org/2000/svg'; 
		var embedSvg=null;
		var path=null;
		
		
		window.onload=function(){
			 scaleX0=prompt("请输入起点X坐标");
			 scaleY0=prompt("请输入起点Y坐标");
			 scaleXY="M"+scaleX0+" "+scaleY0; 
			 scaleXY_history=scaleXY;
			 scaleHistory.push("M"+scaleX0);
			 scaleHistory.push(scaleY0);
			 embedSvg=document.getElementById("embed").getSVGDocument().getElementById("svgMap");
			 /*  绘制运动轨迹*/
		     path=document.createElementNS(nameSpace,"path");
			 drawScale(scaleXY)
			 embedSvg.appendChild(path); 
			  /*  绘制圆心*/
			 circle=document.createElementNS(nameSpace,"circle");
			 drawCircle(scaleX0,scaleY0);
			 embedSvg.appendChild(circle); 
			   /*  绘制木块*/
			 polyline=document.createElementNS(nameSpace,"polyline");
			 drawpolyline(scaleX0,scaleY0);
			 embedSvg.appendChild(polyline); 
			 
			 
		}
		function drawpolyline(scaleX,scaleY){//绘制方形木块
		    if(scaleX!="" && scaleY!=""){
		     var point0_x=scaleX;var point0_y=scaleY;
		     var point1_x=parseInt(scaleX)+30;var point1_y=scaleY;
		     var point2_x=parseInt(scaleX)+30;var point2_y=parseInt(scaleY)+30;
		     var point3_x=scaleX;var point3_y=parseInt(scaleY)+30;
		     var rectShapes=point0_x+","+point0_y+" "+point1_x+","+point1_y+" "+point2_x+","+point2_y+" "+point3_x+","+point3_y
		     
		     polyline.style="stroke:red;stroke-width:2;fill:red"; 
			 polyline.setAttribute("points",rectShapes);
			 
			
		    }
		}
		function drawCircle(scaleX,scaleY){//定位圆点 
		    if(scaleX!=null && scaleY!=null){
		     circle.style="stroke:blue;stroke-width:2;fill:none";
			 circle.setAttribute("r",2.5);
			 circle.setAttribute("cx",scaleX);
			 circle.setAttribute("cy",scaleY);
			
		    }
		}
		function drawScale(scaleXY){//轨迹
		    if(scaleX0!="" && scaleY0!=""){
		     path.style="stroke:black;stroke-width:2.5;fill:none";
			 path.setAttribute("d",scaleXY);
		    }
		}
		function input(){ 
			scaleX=prompt("请输入X坐标"); 
			scaleY=prompt("请输入Y坐标");
			scaleHistory.push(scaleX);
			scaleHistory.push(scaleY);
			if(scaleX!="" && scaleY!=""){
				scaleXY=scaleXY+"L"+scaleX+" "+scaleY;
		        drawScale(scaleXY);
		        drawCircle(scaleX,scaleY);
		        drawpolyline(scaleX,scaleY);
			}
			
			
		}
		function findScale(){
			
			for(var i=0;i<scaleHistory.length;i=i+2){
				scaleX=scaleHistory[i];
				scaleY=scaleHistory[i+1];
				if(scaleX!="" && scaleY!="" && i>1){
					scaleXY_history=scaleXY_history+"L"+scaleX+" "+scaleY;
			        drawScale(scaleXY_history);
				}else{
					scaleXY=scaleX+" "+scaleY;
				}
			}
		}
		function clearScale(){
			scaleXY="M"+scaleX0+" "+scaleY0; 
			drawScale(scaleXY);
			drawCircle(-20,-20);
			drawpolyline(-1000,-1000);
		}
	</script>
</html>
  

svg代码:

<svg width="640" height="400" xmlns="http://www.w3.org/2000/svg" id="svgMap">
   
</svg>

 

转载于:https://my.oschina.net/u/4185276/blog/3100649

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值