12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptでSVGタグ作成。

Last updated at Posted at 2015-03-06

スクリーンショット 2015-03-06 19.39.29.png

JavaScript
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttributeNS(null, 'version', '1.1')
svg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
svg.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");

var group = document.createElementNS('http://www.w3.org/2000/svg', 'g');

var circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
circle.setAttributeNS(null, 'cx', 50);
circle.setAttributeNS(null, 'cy', 50);
circle.setAttributeNS(null, 'r', 30);
circle.setAttributeNS(null, 'stroke-width', 2);
circle.setAttributeNS(null, 'fill', 'yellow');

var rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
rect.setAttributeNS(null, 'x', 10);
rect.setAttributeNS(null, 'y', 50);
rect.setAttributeNS(null, 'width', 30);
rect.setAttributeNS(null, 'height', 30);
rect.setAttributeNS(null, 'stroke', 'red');
rect.setAttributeNS(null, 'stroke-width', 2);
rect.setAttributeNS(null, 'fill', 'pink');

var image = document.createElementNS('http://www.w3.org/2000/svg', 'image');
image.setAttributeNS(null, 'x', 0);
image.setAttributeNS(null, 'y', 0);
image.setAttributeNS(null, 'width', 81);
image.setAttributeNS(null, 'height', 39);
image.setAttributeNS('http://www.w3.org/1999/xlink', 'href', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFEAAAAnCAYAAAB39KTqAAAAAXNSR0IArs4c6QAABNBJREFUaAXtmr+rXUUQx68iCMFAVKxEcrBI5dNCsRARhLQPm2B9KwuttfX26h9ga2EvCtZpbFUsBAneFNFGiAoiBIL6/cD9wmTdPWfmepUIZ2CYPbvze2fn7LnvbTYrrBlYM7BmYM2AM/CABzP0gtZeEj524LktekN4S3j3MPeC6DfCO4fn/wN5VE6+LHxc+LuQuL4V/ij8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TwaXb4WljlL2VV+G4gcHMwn5m+K6YPAiNxVb56LgfZ9LBtxtmKanf82G9qjm6swl3a83nGqJNx5s7IvTTKpY8zrsQSthKO2gio5r3QvOcjxsT8FPSg75je3DPjq5J9hE49xsMca5GXcSmJ7ReEle2liARdFFJp0K3Q62VDku2B9VX7V09XnCOR6LR+KC2LqvTbn01r77zmLyUxXjeoyp2wV51WDsW5uWrVchocxDYtUWP03TP63xs7wWh3kruWRov0g6+FPwUpdvJM+KLw6cP896KfCHv/+nFgKROq4SPhNeGdsnReADtceZ4XPiL8TXhT+IXwhjDzLyliW2HNwJqBNQNrBtYMrBm4/zLwF+xfAZbsk4bMAAAAAElFTkSuQmCC')

group.appendChild(circle);
group.appendChild(rect);
group.appendChild(image);

svg.appendChild(group);

// 文字列として出力
console.log(svg.outerHTML);
svg.outerHTML
<svg xmlns:xlink="
://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1"><g><circle fill="yellow" stroke-width="2" r="30" cy="50" cx="50"></circle><rect fill="pink" stroke-width="2" stroke="red" height="30" width="30" y="50" x="10"></rect><image xlink:href="data:image/png;base64,iVBORw0KG…省略" height="39" width="81" y="0" x="0"></image></g></svg>
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?