[英]How to export PNG within SVG
I have some trouble to export my SVG which contains a PNG image in it. 导出包含PNG图像的SVG时遇到了一些麻烦。 I'm using D3JS and the following code.
我正在使用D3JS和以下代码。
mysvg.append("image")
.attr("width", 299)
.attr("height", 168)
.attr("xlink:href", "image.png")
var html = d3.select("svg")
.attr("version", 1.1)
.attr("xmlns", "http://www.w3.org/2000/svg")
.node().parentNode.innerHTML;
var imgsrc = 'data:image/svg+xml;base64,'+ btoa(html);
var img = '<img src="'+imgsrc+'">';
d3.select("#chartRender").html(img);
It seems that an error occurs: Namespace prefix xlink for href on image is not defined
. 似乎发生错误:
Namespace prefix xlink for href on image is not defined
。 The PNG image is correctly displayed in the SVG container but not in #chartRender
PNG图像正确显示在SVG容器中,但不在
#chartRender
Any idea ? 任何的想法 ?
Update 1 : Drawing the image in a canvas works 更新1:在画布中绘制图像
canvas = d3.select("body").append("canvas").attr("id", "newCanvas")
.attr("width", "200px").attr("height", "100px").node()
context = canvas.getContext('2d')
imageObj = new Image()
imageObj.src = params.url
imageObj.onload = -> context.drawImage(imageObj, 0, 0)
But displaying the datas show empty content :( 但显示数据显示空内容:(
imageData = canvas.toDataURL("image/png")
d3.select("body").append("img").datum(imageData).attr("src", (d)-> return d)
d3.select("svg").append("image").datum(imageData).attr("height", "100")
.attr("width", "200").attr("xlink:href", (d)-> return d)
You need to add xmlns:xlink
to your svg tag. 您需要将
xmlns:xlink
添加到svg标记中。
It should look like this: 它应该如下所示:
<svg width="xxx" height="yyy" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
So just do... 所以就这样......
.attr("xmlns:xlink", "http://www.w3.org/1999/xlink")
I created a JSBIN to help us with this. 我创建了一个JSBIN来帮助我们。 It is working just fine.
它工作得很好。
var mysvg = d3.select('#mysvg');
var src = 'http://placehold.it/350x150';
mysvg.append("image")
.attr("width", 350)
.attr("height", 150)
.attr("xlink:href", src);
var img = document.createElement('img');
img.src = src;
document.getElementById("target").appendChild(img);
Does it solve your problem? 它能解决你的问题吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.