如何解释 two.js 中的外部 svg 文件

lec*_*cmo 2 javascript svg two.js

有什么办法可以在two.js中解释带有对象标签的外部svg文件吗?我尝试了下面的方法,但是..

HTML

<object type="image/svg+xml" data="./svg/mydrawing.svg" id="mysvg"></object>
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

如果我可以导入外部 .svg 文件就好了,因为我的 svg 文件太大而无法在 HTML 中编写为内联 SVG。

提前致谢。

Nat*_*eit 7

可能是您需要在 contentDocument 中选择 svg 标签。例如:

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);
};
Run Code Online (Sandbox Code Playgroud)