如何设置变量而不是文字数?

时间:2012-08-30 13:35:10

标签: javascript html5 svg literals substitution

我想替换这一行:

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

它适用于永久号码:

Demo jsFiddle

这一行:

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

jsfiddle Demo

我该怎么做?很多人。

代码:

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 ();
​

1 个答案:

答案 0 :(得分:2)

polygon.setAttribute("points", LeftWidth + ",0 " + RightWidth + ",0 " + RopTringleX + "," + RopTringleY);