【发布时间】:2017-03-18 22:59:57
【问题描述】:
我正在使用dom-to-image.js 插件。问题是,它会生成一个污染画布的<foreignObject> 标记。这是生成 svg 的部分。
function makeSvgDataUri(node, width, height) {
return Promise.resolve(node)
.then(function (node) {
node.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
return new XMLSerializer().serializeToString(node);
})
.then(util.escapeXhtml)
.then(function (xhtml) {
return '<foreignObject x="0" y="0" width="100%" height="100%">' + xhtml + '</foreignObject>';
})
.then(function (xhtml) {
return '<svg xmlns="http://www.w3.org/2000/svg" width="' + width + '" height="' + height + '">' +
xhtml + '</svg>';
})
.then(function (svg) {
return 'data:image/svg+xml;charset=utf-8,' + svg;
});
}
如果我只是注释掉 <foreignObject> 标记部分,则图像不会显示。在没有<foreignObject>的情况下生成这个svg的正确方法是什么
【问题讨论】:
-
结果
data URI是如何呈现的? -
这个想法是使用 canvas.toDataURL()。但是在 iOS 浏览器上,它会抛出一个错误,“SecurityError: Dom exception 18. Process is insecure”。经过大量挖掘,我得出的结论是 foreignOBject 标签导致了这个问题。在我的情况下,我只会有相同来源的内容,我只需要删除 foreignObject 标记
-
文档中描述的
.toSvg方法是否不会返回预期结果?或者,.toSvg方法是否使用<foreignObject>? -
avarachan.com/test3.html 将显示它是如何使用 foreignobject 标签呈现的。但它不适用于ios浏览器
-
我认为所有方法都使用 makeSvgDataUri() 函数,包括 toSvg
标签: javascript canvas svg