0

それが可能かどうか、および既に描画された SVG を描画する方法を知りたいです。SVG で何かを描画したとしますが、すべてのマップを再度描画したり、元のタラを変更したりせずに、別の要素を追加したいとします。

4

1 に答える 1

1

はい、できます。実際、それは svg の利点の 1 つです。次に例を示します。

<!DOCTYPE html>
<html>
    <head>
        <title>HTML5 SVG demo</title>
    </head>

    <body>
        <h1>SVG DEMO</h1>
        <svg id="circle" height="200" xmlns="http://www.w3.org/2000/svg">
        <circle id="greencircle" cx="30" cy="30" r="30" fill="green" />
        </svg>



        <script type="text/javascript">

            var rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
            rect.setAttributeNS(null,"id", "myrect");
            rect.setAttributeNS(null,"fill", "red");
            rect.setAttributeNS(null,"stroke", "black");
            rect.setAttributeNS(null,"stroke-width", "5");
            rect.setAttributeNS(null,"x", "100");
            rect.setAttributeNS(null,"y", "100");
            rect.setAttributeNS(null,"width", "100");
            rect.setAttributeNS(null,"height", "50");
            var svg = document.getElementById("circle");
            svg.appendChild(rect);


        </script>
    </body>
</html>
于 2013-05-04T10:47:11.673 に答える