変数 = document.createElementNS( 名前空間 , タグ名 );SVGグラフィックを作成する場合、メソッドは「createElementNS」というものを使います。これは名前空間を指定してDOMオブジェクトを生成するためのものです。要するに、「普通のHTMLとはちょっと違うところで定義されているものを利用するのに、それが定義されているところを指定して作るようになっているメソッド」と考えるとよいでしょう。
newobj.setAttribute('cx',x);
newobj.setAttribute('cy',y);
newobj.setAttribute('r',r);
newobj.setAttribute('style','fill:#6666FF;stroke:0000FF;stroke-width:10;');
document.getElementById("svg").appendChild(newobj);諸設定が完了したら、<svg>タグにオブジェクトを追加します。オブジェクトの追加は、「appendChild」メソッドを利用します。これはJavaScriptのDOMオブジェクトでも登場しますから使い方はわかりますね。
※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="300px" height="300px" id="svg" xmlns="http://www.w3.org/2000/svg"> <script type="text/javascript"> <![CDATA[ var svgns = 'http://www.w3.org/2000/svg'; function doClick(event){ var x = event.x; var y = event.y; var r = 50; var newobj = document.createElementNS(svgns, 'circle'); newobj.setAttribute('cx',x); newobj.setAttribute('cy',y); newobj.setAttribute('r',r); newobj.setAttribute('style','fill:#6666FF;stroke:0000FF;stroke-width:10;'); document.getElementById("svg").appendChild(newobj); } ]]> </script> <rect x="0" y="0" width="300" height="300" fill="#F0F0FF" onclick="doClick(event);" /> <circle id="circle1" cx="100" cy="75" r="50" fill="#FF6666" stroke="#FF0000" stroke-width="5px" /> </svg>
<< 前へ | 次へ >> |