gpt4 book ai didi

javascript - 尝试将 SVG 绘制到 Canvas ,为什么我的 SVG XML 被 chop 了?

转载 作者:行者123 更新时间:2023-11-29 15:32:48 25 4
gpt4 key购买 nike

我真正想做的就是将我喜欢的动态创建的 SVG 放入 PDF 中,目前通过 jsPDF。 addSVG 不起作用,所以我尝试将它们转换为 PNG,以便尝试使用 addImage。

这是在 IE11 中(客户端要求)。

如果我这样做:

var lsvg = d3.select("#nowhere2").node().parentNode.innerHTML;
console.log(lsvg);

也许三分之一的 SVG 显示在控制台中。更奇怪的是,它会在事物的中间 chop ,因此没有结束标记或其他任何内容:

<div id="nowhere2"><svg xmlns="http://www.w3.org/2000/svg" width="50px" height="800px"><defs><pattern id="oaghm" patternUnits="userSpaceOnUse" width="30" height="30"><path stroke="#343434" stroke-linecap="square" stroke-width="80" d="M 0 30 l 30 -30 M -7.5 7.5 l 15 -15 M 22.5 37.5 l 15 -15" shape-rendering="auto" /></pattern></defs><rect style="fill: url(#oaghm);" stroke="black" x="10" y="20" width="10" height="10" /><defs><pattern id="zpdff" patternUnits="userSpaceOnUse" width="4" height="4"><path stroke="#343434" stroke-linecap="square" stroke-width="1" d="M 0 4 l 4 -4 M -1 1 l 2 -2 M 3 5 l 2 -2" shape-rendering="auto" /></pattern></defs><rect style="fill: url(#zpdff);" stroke="black" x="10" y="40" width="10" height="10" /><defs><pattern id="dyxwi" patternUnits="userSpaceOnUse" width="10" height="10"><path stroke="#343434" stroke-linecap="square" stroke-width="2" d="M 0 10 l 10 -10 M -2.5 2.5 l 5 -5 M 7.5 12.5 l 5 -5" shape-rendering="auto" /><path stroke="#343434" stroke-linecap="square" stroke-width="2" d

当然,这意味着我的 DataURI 坏了,尽管 Chrome 勇敢地尝试了(仅用于测试,必须使用 IE11):

data:image/svg+xml;base64,PGRpdiBpZD0ibm93aGVyZTIiPjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iNTAiIGhlaWdodD0iODAwIj48ZGVmcz48cGF0dGVybiBpZD0iZmRldWsiIHBhdHRlcm5Vbml0cz0idXNlclNwYWNlT25Vc2UiIHdpZHRoPSIzMCIgaGVpZ2h0PSIzMCI+PHBhdGggc3Ryb2tlPSIjMzQzNDM0IiBzdHJva2UtbGluZWNhcD0ic3F1YXJlIiBzdHJva2Utd2lkdGg9IjgwIiBkPSJNIDAgMzAgbCAzMCAtMzAgTSAtNy41IDcuNSBsIDE1IC0xNSBNIDIyLjUgMzcuNSBsIDE1IC0xNSIgc2hhcGUtcmVuZGVyaW5nPSJhdXRvIiAvPjwvcGF0dGVybj48L2RlZnM+PHJlY3Qgc3R5bGU9ImZpbGw6IHVybCgjZmRldWspOyIgc3Ryb2tlPSJibGFjayIgeD0iMTAiIHk9IjIwIiB3aWR0aD0iMTAiIGhlaWdodD0iMTAiIC8+PGRlZnM+PHBhdHRlcm4gaWQ9InV2bWdnIiBwYXR0ZXJuVW5pdHM9InVzZXJTcGFjZU9uVXNlIiB3aWR0aD0iNCIgaGVpZ2h0PSI0Ij48cGF0aCBzdHJva2U9IiMzNDM0MzQiIHN0cm9rZS1saW5lY2FwPSJzcXVhcmUiIHN0cm9rZS13aWR0aD0iMSIgZD0iTSAwIDQgbCA0IC00IE0gLTEgMSBsIDIgLTIgTSAzIDUgbCAyIC0yIiBzaGFwZS1yZW5kZXJpbmc9ImF1dG8iIC8+PC9wYXR0ZXJuPjwvZGVmcz48cmVjdCBzdHlsZT0iZmlsbDogdXJsKCN1dm1nZyk7IiBzdHJva2U9ImJsYWNrIiB4PSIxMCIgeT0iNDAiIHdpZHRoPSIxMCIgaGVpZ2h0PSIxMCIgLz48ZGVmcz48cGF0dGVybiBpZD

更奇怪的是,如果我简单地将 lsvg 对象追加回页面,它呈现得很好。

var lsvg = d3.select("#nowhere2").node().parentNode.innerHTML;
$("#nowhere").append(lsvg);

如果我在附加后尝试在控制台中检查它,我会得到相同的 chop 字符串。当然,这让我相信这是一个时间/异步问题,但我已经尝试了所有我能想到的方法来解决这个问题,包括设置 draw-to-canvas-then-save-to-png 函数作为我最初的 SVG 绘制函数的回调。我每次都得到相同的奇怪 chop 字符串。

这是我的 Canvas (由于其他问题在 html 中声明):

<canvas id="lcanvas" width="50" height="800"></canvas>

研究让我相信这可能是 Canvas /SVG 大小不匹配,但情况似乎并非如此(我得到的 SVG 显示相同的宽度/高度,见上文)。此外,如果这是真的,它甚至会在 Canvas 涉及之前 chop 初始字符串分配,这似乎很奇怪。

有一次我得到了完整的 DataURI:

data:image/svg+xml;base64,PGRpdiBpZD0ibm93aGVyZTIiPjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iNTAiIGhlaWdodD0iODAwIj48ZGVmcz48cGF0dGVybiBpZD0iZmxhY3EiIHBhdHRlcm5Vbml0cz0idXNlclNwYWNlT25Vc2UiIHdpZHRoPSIzMCIgaGVpZ2h0PSIzMCI+PHBhdGggc3Ryb2tlPSIjMzQzNDM0IiBzdHJva2UtbGluZWNhcD0ic3F1YXJlIiBzdHJva2Utd2lkdGg9IjgwIiBkPSJNIDAgMzAgbCAzMCAtMzAgTSAtNy41IDcuNSBsIDE1IC0xNSBNIDIyLjUgMzcuNSBsIDE1IC0xNSIgc2hhcGUtcmVuZGVyaW5nPSJhdXRvIiAvPjwvcGF0dGVybj48L2RlZnM+PHJlY3Qgc3R5bGU9ImZpbGw6IHVybCgjZmxhY3EpOyIgc3Ryb2tlPSJibGFjayIgeD0iMTAiIHk9IjIwIiB3aWR0aD0iMTAiIGhlaWdodD0iMTAiIC8+PGRlZnM+PHBhdHRlcm4gaWQ9InV5b3JkIiBwYXR0ZXJuVW5pdHM9InVzZXJTcGFjZU9uVXNlIiB3aWR0aD0iNCIgaGVpZ2h0PSI0Ij48cGF0aCBzdHJva2U9IiMzNDM0MzQiIHN0cm9rZS1saW5lY2FwPSJzcXVhcmUiIHN0cm9rZS13aWR0aD0iMSIgZD0iTSAwIDQgbCA0IC00IE0gLTE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GFjZU9uVXNlIiB3aWR0aD0iNSIgaGVpZ2h0PSI1Ij48Y2lyY2xlIGZpbGw9IiMzNDM0MzQiIHN0cm9rZT0iIzM0MzQzNCIgc3Ryb2tlLXdpZHRoPSIwIiBjeD0iMi41IiBjeT0iMi41IiByPSIyIiAvPjwvcGF0dGVybj48L2RlZnM+PHJlY3Qgc3R5bGU9ImZpbGw6IHVybCgjaGJ4dXApOyIgc3Ryb2tlPSJibGFjayIgeD0iMTAiIHk9IjE4MCIgd2lkdGg9IjEwIiBoZWlnaHQ9IjEwIiAvPjwvc3ZnPjwvZGl2Pg==

所以我明确地使用该 DataURI 对其进行了测试,换句话说,不用担心绘图是否已经完成。我做了:

limg.src = "data:image/svg+xml;base64,PGRpdiBpZD0ibm93aGVyZTIiPjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iNTAiIGhlaWdodD0iODAwIj48ZGVmcz48cGF0dGVybiBpZD0iZmxhY3EiIHBhdHRlcm5Vbml0cz0idXNlclNwYWNlT25Vc2UiIHdpZHRoPSIzMCIgaGVpZ2h0PSIzMCI+PHBhdGggc3Ryb2tlPSIjMzQzNDM0IiBzdHJva2UtbGluZWNhcD0ic3F1YXJlIiBzdHJva2Utd2lkdGg9IjgwIiBkPSJNIDAgMzAgbCAzMCAtMzAgTSAtNy41IDcuNSBsIDE1IC0xNSBNIDIyLjUgMzcuNSBsIDE1IC0xNSIgc2hhcGUtcmVuZGVyaW5nPSJhdXRvIiAvPjwvcGF0dGVybj48L2RlZnM+PHJlY3Qgc3R5bGU9ImZpbGw6IHVybCgjZmxhY3EpOyIgc3Ryb2tlPSJibGFjayIgeD0iMTAiIHk9IjIwIiB3aWR0aD0iMTAiIGhlaWdodD0iMTAiIC8+PGRlZnM+PHBhdHRlcm4gaWQ9InV5b3JkIiBwYXR0ZXJuVW5pdHM9InVzZXJTcGFjZU9uVXNlIiB3aWR0aD0iNCIgaGVpZ2h0PSI0Ij48c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IgeT0iMTYwIiB3aWR0aD0iMTAiIGhlaWdodD0iMTAiIC8+PGRlZnM+PHBhdHRlcm4gaWQ9ImhieHVwIiBwYXR0ZXJuVW5pdHM9InVzZXJTcGFjZU9uVXNlIiB3aWR0aD0iNSIgaGVpZ2h0PSI1Ij48Y2lyY2xlIGZpbGw9IiMzNDM0MzQiIHN0cm9rZT0iIzM0MzQzNCIgc3Ryb2tlLXdpZHRoPSIwIiBjeD0iMi41IiBjeT0iMi41IiByPSIyIiAvPjwvcGF0dGVybj48L2RlZnM+PHJlY3Qgc3R5bGU9ImZpbGw6IHVybCgjaGJ4dXApOyIgc3Ryb2tlPSJibGFjayIgeD0iMTAiIHk9IjE4MCIgd2lkdGg9IjEwIiBoZWlnaHQ9IjEwIiAvPjwvc3ZnPjwvZGl2Pg==";
console.log(limg.src);

并且在控制台中是一样的。爆炸了。 chop 。字符串。这是我的代码:

        var lsvg = d3.select("#nowhere2").node().parentNode.innerHTML;
console.log(lsvg);

var limg = new Image;
var lcontext = canl.getContext('2d');

limg.src = 'data:image/svg+xml;base64,' + btoa(lsvg);

console.log(limg.src);
lcontext.load = limg.addEventListener("load", function () {
//console.log(limg.src);
//This fun bit of code brought to you courtesy of IE11.
try {
lcontext.drawImage(limg, 0, 0);
}
catch (err) {
setTimeout(lcontext.drawImage(limg, 0, 0), 0);
};
console.log(canl.toDataURL('image/png'));
});

我让 Canvas 可见,这样我就可以看到可能会发生什么样的恶作剧。它会很好地从文件中绘制图像。另外,从技术上讲,我在这里得到了一张图片,它只是空白的。

我不想从文件中绘制,因为这会污染 Canvas 并且我在 canl.toDataURL 上收到安全错误。

我尝试了各种获取 lsvg 位、XMLSerialize 等的方法;我得到的只是稍微短一点的头部(没有 div 元素)和稍微长一点的尾部,然后在似乎完全相同的字符数之后被 chop 。像往常一样,就在事情的中间,所以仍然没有结束标签,只是中途突然结束。

需要说明的是,这不是裁剪后的 SVG。这是一个“剪裁”的 SVG。开始标签,没有结束标签,损坏的图像,除非你是 Chrome 并且你尝试渲染它(IE 甚至不会打扰)。 SVG 在涉及 Canvas 之前就被破坏了,甚至在显式分配后独立于绘图函数。

哦,亲爱的正义,我有一把 fiddle ! https://jsfiddle.net/94xhyv6t/在 Chrome 和 IE 中尝试并检查控制台。在 Chrome 中,如果您单击该链接,您将获得一个 XML 页面,但您可以在那里看到所有 SVG 元素,在 IE 中,它被 chop 了。

很高兴知道我还没有完全疯掉。

只是因为我知道有人会提到它,这是没有父 DIV 节点的:https://jsfiddle.net/94xhyv6t/1/同样的问题。


编辑:注意:这个 fiddle 显示了问题:https://jsfiddle.net/94xhyv6t/1/ .在 Chrome 中加载它,一切正常。在 IE11 中加载它,一切都被 chop 了。我删除了所有其他代码,这个 fiddle 只显示了 SVG 字符串生成组件以及它是如何在 IE 中被 chop 的。我已经根据 https://jsfiddle.net/94xhyv6t/4/ 尝试过 XMLSerializer ,同样的问题。也许有解决方法?


目前的最后编辑:控制台只是 chop 显示,否则字符串没问题。接受的答案有一个完整的 fiddle 示例,展示了如何将 SVG 放入 Canvas 中,但是你不能在 IE11 中执行 canvas.toDataURL() 所以这不是合适的转换方法。我使用 canvg 作为解决方法。

最佳答案

不知道你的问题到底是什么,但是使用

var data = new XMLSerializer().serializeToString(document.querySelector('svg'));
img.src = 'data:image/svg+xml; charset=utf8, ' + encodeURIComponent(data);

适用于 FF 和 Chrome。

var n = 20, // number of layers
m = 200, // number of samples per layer
stack = d3.layout.stack().offset("wiggle"),
layers0 = stack(d3.range(n).map(function() { return bumpLayer(m); })),
layers1 = stack(d3.range(n).map(function() { return bumpLayer(m); }));

var width = 960,
height = 500;

var x = d3.scale.linear()
.domain([0, m - 1])
.range([0, width]);

var y = d3.scale.linear()
.domain([0, d3.max(layers0.concat(layers1), function(layer) { return d3.max(layer, function(d) { return d.y0 + d.y; }); })])
.range([height, 0]);

var color = d3.scale.linear()
.range(["#aad", "#556"]);

var area = d3.svg.area()
.x(function(d) { return x(d.x); })
.y0(function(d) { return y(d.y0); })
.y1(function(d) { return y(d.y0 + d.y); });

var svg = d3.select("#worm").append("svg")
.attr("width", width)
.attr("height", height);

svg.selectAll("path")
.data(layers0)
.enter().append("path")
.attr("d", area)
.style("fill", function() { return color(Math.random()); });

function transition() {
d3.selectAll("path")
.data(function() {
var d = layers1;
layers1 = layers0;
return layers0 = d;
})
.transition()
.duration(2500)
.attr("d", area);
}

// Inspired by Lee Byron's test data generator.
function bumpLayer(n) {

function bump(a) {
var x = 1 / (.1 + Math.random()),
y = 2 * Math.random() - .5,
z = 10 / (.1 + Math.random());
for (var i = 0; i < n; i++) {
var w = (i / n - y) * z;
a[i] += x * Math.exp(-w * w);
}
}

var a = [], i;
for (i = 0; i < n; ++i) a[i] = 0;
for (i = 0; i < 5; ++i) bump(a);
return a.map(function(d, i) { return {x: i, y: Math.max(0, d)}; });
}
var lsvg = d3.select("#worm").node().parentNode.innerHTML;
console.log(lsvg);
var img = new Image;

console.log(img.src);
document.body.appendChild(img);
img.onload = function(){
var canvas = document.createElement('canvas');
// IE seems to not set width and height for svg in img tag...
canvas.width = this.width||960;
canvas.height = this.height||500;
canvas.getContext('2d').drawImage(this, 0,0);
document.body.appendChild(canvas);
}
img.src = 'data:image/svg+xml; charset=utf8, ' + encodeURIComponent(new XMLSerializer().serializeToString(document.querySelector('svg')));
svg{border: 1px solid green}
img{border: 1px solid blue}
canvas{border: 1px solid red}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="worm"></div>

请注意,我在 onload 中添加了 Canvas 内容img 标签的事件。

编辑:我发现 IE 没有设置在 <img> 中加载的 svg 的宽度和高度属性。标签,因此您必须对其进行硬编码或从 <svg> 中获取它元素

关于javascript - 尝试将 SVG 绘制到 Canvas ,为什么我的 SVG XML 被 chop 了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32895732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com