search

Home  >  Q&A  >  body text

Convert SVG to image (JPEG, PNG, etc.) in your browser

<p>I want to convert an SVG to a bitmap image (like JPEG, PNG, etc.) via JavaScript. </p>
P粉052686710P粉052686710515 days ago571

reply all(2)I'll reply

  • P粉668804228

    P粉6688042282023-08-24 18:10:17

    jbeard4 solution works well.

    I'm using Raphael SketchPad to create the SVG. Link to the file from step 1.

    For the "Save" button (the id of svg is "editor" and the id of canvas is "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...
    });

    reply
    0
  • P粉594941301

    P粉5949413012023-08-24 00:54:48

    The following is the method implemented through JavaScript:

    1. Use the canvg JavaScript library to render SVG images through Canvas: https://github.com/gabelerner/canvg
    2. Capture a data URI encoded as JPG (or PNG) from Canvas according to the following instructions: Capture HTML Canvas as gif/jpg/png/pdf?

    reply
    0
  • Cancelreply