How can I interpret an extrenal svg file in two.js

心已入冬 提交于 2020-01-06 01:36:10

问题


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.


回答1:


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);
};


来源:https://stackoverflow.com/questions/21720790/how-can-i-interpret-an-extrenal-svg-file-in-two-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!