2

Is there any way I can interpret an external svg file with object tag in two.js? I tried in the way below but..

HTML

<object type="image/svg+xml" data="./svg/mydrawing.svg" id="mysvg"></object>

JS

var mySvg = document.getElementById("mysvg").contentDocument;
var shape = two.interpret(mySvg);
console.log(shape);

//in console:
Uncaught TypeError: Cannot call method 'toLowerCase' of undefined

It's nice if I can import an external .svg file as my svg file is too big to write in HTML as Inline SVG.

Thanks in advance.

leccmo
  • 329
  • 4
  • 16

1 Answers1

7

It could be that you need to select the svg tag within the contentDocument. For example:

var svgObject = document.getElementsByTagName('object')[0];
svgObject.onload = function(){
      var mySvg = svgObject.contentDocument.getElementsByTagName('svg')[0];
      var two = new Two();
      var shape = two.interpret(mySvg);
      console.log(shape);
};
Nathan Breit
  • 1,661
  • 13
  • 33