简体   繁体   English

在JavaScript中从base64创建SVG

[英]Create SVG from base64 in javascript

i am new to javascript. 我是javascript新手。

i want to create SVG from base64. 我想从base64创建SVG。 i am trying http://jsfiddle.net/XTUmV/28/ but it doesnt show anything. 我正在尝试http://jsfiddle.net/XTUmV/28/,但没有显示任何内容。

 var image="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
 var svgimg = document.createElementNS("http://www.w3.org/2000/svg", "image");
 svgimg.setAttributeNS("http://www.w3.org/1999/xlink", 'xlink:href', image);

 document.getElementById("mySvg").appendChild(svgimg);

and html: 和html:

 <svg id="mySvg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"></svg>

base64 should be correct, cause i took it from this http://jsfiddle.net/MxHPq/ example base64应该是正确的,因为我从此http://jsfiddle.net/MxHPq/示例中获取了它

I am doing something stupid or just wrong approach? 我在做一些愚蠢的事情或只是错误的方法?

thanks 谢谢

You forgot to give the <image> tag some dimensions: 您忘记给<image>标签提供一些尺寸:

var image="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
var svgimg = document.createElementNS("http://www.w3.org/2000/svg", "image");
// new
svgimg.setAttribute( 'width', '100' );
svgimg.setAttribute( 'height', '100' );

svgimg.setAttributeNS("http://www.w3.org/1999/xlink", 'xlink:href', image);


document.getElementById("mySvg").appendChild(svgimg);

Edited Fiddle 编辑小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM