スタートページ> Javascript> SVG>
img要素と同様、横軸は右が大。縦軸は下が大になります。
<svg width="300" height="200"> <line x1="50" y1="10" x2="200" y2="50" stroke="red" /> // "/>"が必要です。 <circle cx="150" cy="100" r="50" stroke="blue" fill="yellow" /> </svg>
<svg id="mySVG" width="290" height="160"> <line id="myline" x1="50" y1="10" /> </svg>
上と同じ図形を JavaScript で作図することを考えます。
HTML の svg要素は右の青字のようになっているとします。
・直線は、パラメタが一部しかありません。
・円に関しては、記述がありません。
// 現在の svg要素の情報を取り込む var svg = document.getElementById("mySVG"); // A // 既に一部が記述されている図形。その情報を取り込む var line = document.getElementById("myline"); // B line.setAttribute("x2","200"); // C line.setAttribute("y2","50"); line.setAttribute("stroke","red"); // 新規の図形を作成 var round = document.createElementNS("http://www.w3.org/2000/svg", "circle"); // D round.setAttributeNS(null,"cx", 150); // E round.setAttributeNS(null,"cy", 100); round.setAttributeNS(null,"r", 50); round.setAttributeNS(null,"fill", "yellow"); round.setAttributeNS(null,"stroke", "blue"); // 新規図形をこれまでの svg 要素の子要素として組み込む svg.appendChild(round); // F