Save inline SVG as JPEG/PNG/SVG

前端 未结 4 868
礼貌的吻别
礼貌的吻别 2020-11-28 04:13

I have an inline SVG in my html, and I need to be able to save this as either a JPEG, PNG or SVG.

I have tried a few different methods with converting the SVG to can

4条回答
  •  再見小時候
    2020-11-28 04:26

    Working off @CiroCosta. 1 option if you are having trouble exporting an element you could just draw the image to the canvas before drawing the svg image

    btn.addEventListener('click', function () {
      var canvas = document.getElementById('canvas');
      var ctx = canvas.getContext('2d');
      var data = (new XMLSerializer()).serializeToString(svg);
      var DOMURL = window.URL || window.webkitURL || window;
    
      // get the raw image from the DOM
      var rawImage = document.getElementById('yourimageID');
      var img = new Image();
      var svgBlob = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
      var url = DOMURL.createObjectURL(svgBlob);
    
      img.onload = function () {
        ctx.drawImage(rawImage, 0, 0);
        ctx.drawImage(img, 0, 0);
        DOMURL.revokeObjectURL(url);
    
        var imgURI = canvas
          .toDataURL('image/png')
          .replace('image/png', 'image/octet-stream');
    
        triggerDownload(imgURI);
      };
    
      img.src = url;
    });
    

    Worked for me but only for png and jpeg. SVG files still only display inline elements and not tags

    EDIT: The way you create an svg like this is actually by converting the image tag into Base64 and the setting that as the xlink:href in the image attributes like this:

    
    

    and then triggering the download on the whole svg url like this:

    btn.addEventListener('click', function () {
      var canvas = document.getElementById('canvas');
      var ctx = canvas.getContext('2d');
      var data = (new XMLSerializer()).serializeToString(svg);
      var DOMURL = window.URL || window.webkitURL || window;
    
      var rawImage = document.getElementById('yourimageID');
      var img = new Image();
      var svgBlob = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
      var url = DOMURL.createObjectURL(svgBlob);
    
      img.onload = function () {
        ctx.drawImage(img, 0, 0);
    
        triggerDownload(url);
        DOMURL.revokeObjectURL(url);
      }
    };
    

    you can convert pngs like this here:

    function getDataUri(url, callback) {
      var image = new Image();
    
      image.onload = function () {
        var canvas = document.createElement('canvas');
        canvas.width = this.naturalWidth; // or 'width' if you want a special/scaled size
        canvas.height = this.naturalHeight; // or 'height' if you want a special/scaled size
    
        canvas.getContext('2d').drawImage(this, 0, 0);
    
        // Get raw image data
        callback(canvas.toDataURL('image/png').replace(/^data:image\/(png|jpg);base64,/, ''));
    
        // ... or get as Data URI
        callback(canvas.toDataURL('image/png'));
      };
    
      image.src = url;
    }
    

    then setting the attribute

    getDataUri('localImagepath', function (dataUri) {
      image.setAttribute('xlink:href', dataUri);
    });
    

提交回复
热议问题