Konvertieren Sie SVG in Ihrem Browser in ein Bild (JPEG, PNG usw.).
P粉052686710
P粉052686710 2023-08-23 21:18:39
0
2
446
<p>Ich möchte eine SVG-Datei über JavaScript in ein Bitmap-Bild (wie JPEG, PNG usw.) konvertieren. </p>
P粉052686710
P粉052686710

Antworte allen(2)
P粉668804228

jbeard4 解决方案运行良好。

我正在使用 Raphael SketchPad 创建 SVG。链接到步骤 1 中的文件。

对于“保存”按钮(svg 的 id 是“editor”,canvas 的 id 是“canvas”):

$("#editor_save").click(function() {

// the canvg call that takes the svg xml and converts it to a canvas
canvg('canvas', $("#editor").html());

// the canvas calls to output a png
var canvas = document.getElementById("canvas");
var img = canvas.toDataURL("image/png");
// do what you want with the base64, write to screen, post to server, etc...
});
P粉594941301

以下是通过 JavaScript 实现的方法:

  1. 使用canvg JavaScript库通过Canvas渲染SVG图像:https://github.com/gabelerner/canvg
  2. 根据以下说明从 Canvas 捕获编码为 JPG(或 PNG)的数据 URI:将 HTML Canvas 捕获为 gif/jpg/png/pdf?
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!