简体   繁体   English

使用JavaScript将SVG渲染为PNG

[英]Render SVG to PNG using JavaScript

The following HTML element is on my page: 我的页面上有以下HTML元素:

<canvas id="canvas" width=400 height=400 />

And I am trying to render the following tag as a PNG. 我正在尝试将以下标签呈现为PNG。

<div style="font-size=40px">
  Fun with d3.js
</div>

My JavaScript function is as follows. 我的JavaScript函数如下。 It should be rendering the tag containing the text "Fun with d3.js" as an image in the canvas, however, it is not being rendered. 它应该将包含文本“ Fun with d3.js”的标签渲染为画布中的图像,但是不会渲染。 But, if I insert the raw HTML elements using devtools, the SVG renders as legitimate. 但是,如果我使用devtools插入原始HTML元素,则SVG会呈现为合法。 Somewhere in translation the Javascript seems to be losing the html element, but for the life of me cannot figure out why. 在翻译的某个地方,Javascript似乎丢失了html元素,但就我的生活而言,无法弄清楚原因。 TThe img.onerror event is being called but provides very little in terms of useful information. 正在调用img.onerror事件,但在有用信息方面几乎没有提供。

renderToPNG: function() {
  var myCanvas = document.getElementById("canvas");
  var context = myCanvas.getContext("2d");
  var data = 
    '<svg width=\"400\" height=\"400\">' +
    '  <foreignObject width=\"100%\" height=\"100%\">' +
    '    <div style=\"font-size=40px\">' +
    '      Fun with d3.js' +
    '    </div>' +
    '  </foreignObject>' +
    '</svg>';
  var domURL = window.URL || window.webkitURL || window;
  var img = new Image();
  var svg = new Blob([data], {type:"image/svg+xml;charset=utf-8"});
  var url = domURL.createObjectURL(svg);

  img.onload = function() {
    context.drawImage(img, 0, 0);
    domURL.revokeObjectURL(url);
  }

  img.src = url;
}

I tried running your code and it turns out the foreignObject doesn't work for some reason. 我尝试运行您的代码,结果由于某些原因foreignObject不起作用。 So that might be at the root of the problem. 因此,这可能是问题的根源。

It also depends on which browser you are using. 它还取决于您使用的浏览器。 Take a look at the snippet, it shows the SVG, the Canvas, and the Image, and you can see each of them having different problems ( I see the canvas distorted on Chrome , and on Safari it doesn't even show). 看一下代码片段,它显示了SVG,画布和图像,并且您可以看到它们都有不同的问题( 我看到画布在Chrome 变形了 ,在Safari上甚至没有显示出来)。

 function renderToPNG() { var myCanvas = document.getElementById("canvas"); var context = myCanvas.getContext("2d"); var data = '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' + '<svg version="1.1"' + ' baseProfile="full"' + ' width="200" height="200"' + ' xmlns="http://www.w3.org/2000/svg">' + ' <foreignObject width="100%" height="100%">' + ' <div style="font-size=40px; color: black">' + ' Fun with d3.js' + ' </div>' + ' </foreignObject>' + ' <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />' + '</svg>'; var domURL = window.URL || window.webkitURL || window; var img = new Image(); var svg = new Blob([data], { type: "image/svg+xml;charset=utf-8" }); var url = domURL.createObjectURL(svg); img.onload = function() { context.drawImage(img, 0, 0); domURL.revokeObjectURL(url); } img.src = url; console.debug(img); $('#image').append(img); } renderToPNG(); 
 #svg, #canvas, #image { border: 1px solid; width: 200px; height: 200px; display: inline-block; vertical-align: bottom; } #svg { border-color: red } #canvas { border-color: green } #image { border-color: blue } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <svg id="svg" version="1.1" baseProfile="full" width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <foreignObject width="100%" height="100%"> <div style="font-size:40px; color: black"> Fun with d3.js </div> </foreignObject> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> </svg> <canvas id="canvas" width="200" height="200"></canvas> <div id="image"></div> 

One thing to notice: I added the SVG doctype declaration on the JS code since that's how it's supposed to work when using the SVG as source for an Image (it acts as if it was an external file). 需要注意的一件事:我在JS代码上添加了SVG doctype声明,因为这是将SVG用作图像源时的工作方式(它好像是一个外部文件)。

Just my two cents. 只是我的两分钱。 Hoping for a better answer... 希望有更好的答案...

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

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