2012-08-30 62 views
0

我要替换这一行:我怎样才能把变量,而不是文字数字?

polygon.setAttribute("points", "50,0 200,0 125,150"); 

它与永久编号:

Demo jsFiddle

这一行:

polygon.setAttributeNS (null, "points", ("LeftWidth,0 RightWidth,0 RopTringleX,RopTringleY")); 

jsfiddle Demo

我怎样才能做到这一点?许多thx。

代码:

function CreateSVG() { 
      var xmlns = "http://www.w3.org/2000/svg"; 
      var boxWidth = 250; 
      var boxHeight = 250; 
      var LeftWidth=(250-boxHeight)/2; 
      var RightWidth=250-LeftWidth; 
      var RopTringleX=(RightWidth-(boxWidth/2)); 
      var RopTringleY=(boxHeight); 


      var svgElem = document.createElementNS (xmlns, "svg"); 
      svgElem.setAttributeNS (null, "viewBox", "0 0 " + boxWidth + " " + boxHeight); 
      svgElem.setAttributeNS (null, "width", 250); 
      svgElem.setAttributeNS (null, "height", 250); 
      svgElem.style.display = "block"; 

      var polygon = document.createElementNS (xmlns, "polygon"); 
      svgElem.appendChild (polygon); 
alert(RopTringleY);   

      polygon.setAttributeNS (null, "points", ("LeftWidth,0 RightWidth,0 RopTringleX,RopTringleY")); 

      var path = document.createElementNS (xmlns, "path"); 
      path.setAttributeNS (null, 'stroke', "white"); 
      path.setAttributeNS (null, 'stroke-width', 4); 
      path.setAttributeNS (null, 'fill', "yellow"); 
      polygon.appendChild (path); 

      var svgContainer = document.getElementById ("svgTriangle"); 

      svgContainer.appendChild (svgElem); 
      alert(boxWidth + ' ' + boxHeight);  
     } 
CreateSVG(); 
​ 

回答

2
polygon.setAttribute("points", LeftWidth + ",0 " + RightWidth + ",0 " + RopTringleX + "," + RopTringleY); 
相关问题