How to save svg canvas to local filesystem

前端 未结 16 1344
南旧
南旧 2020-11-27 10:10

Is there a way to allow a user, after he has created a vector graph on a javascript svg canvas using a browser, to download this file to their local filesystem?

SVG

相关标签:
16条回答
  • 2020-11-27 10:20

    With FileSaver from Eli Grey I got it working (chrome):

    bb = new window.WebKitBlobBuilder;
    var svg = $('#designpanel').svg('get');
    bb.append(svg.toSVG());
    var blob = bb.getBlob("application/svg+xml;charset=" + svg.characterSet);
    saveAs(blob,"name.svg");
    

    SVG is from keith woods jquery's svg.

    Html Rocks 1

    Html Rocks 2

    0 讨论(0)
  • 2020-11-27 10:23

    I recently found this Chrome plugin http://nytimes.github.io/svg-crowbar/. It's a bit buggy for my needs but essentially works.

    I had previously made a solution similar to the accepted answer which involved a serverside script but it's quite long winded and also had the problem that all the styles had to be inline in the markup. Crowbar seems to take care of that for you, which is nice.

    0 讨论(0)
  • 2020-11-27 10:23

    Most compatible way would be a round-trip to the server. You could also use a data: URI in some browsers.

    0 讨论(0)
  • 2020-11-27 10:30

    There is no need to do base64 encoding - you can create a link with raw SVG code in it. Here is a modified function encode_as_img_and_link() from The_Who's answer:

    function img_and_link() {
      $('body').append(
        $('<a>')
          .attr('href-lang', 'image/svg+xml')
          .attr('href', 'data:image/svg+xml;utf8,' +  unescape($('svg')[0].outerHTML))
          .text('Download')
      );
    }
    
    0 讨论(0)
  • 2020-11-27 10:32

    	// save SVG
    	$('#SVGsave').click(function(){
    		var a      = document.createElement('a');
    		a.href     = 'data:image/svg+xml;utf8,' + unescape($('#SVG')[0].outerHTML);
    		a.download = 'plot.svg';
    		a.target   = '_blank';
    		document.body.appendChild(a); a.click(); document.body.removeChild(a);
    	});

    0 讨论(0)
  • 2020-11-27 10:32

    No need to do any programming at all. There are online apps people have already built for that, and include definable parameters such as dimensions, resolution, output format etc.

    This is one of the better online image conversion apps I've found for svg->jpeg.http://image.online-convert.com/convert-to-jpg

    0 讨论(0)
提交回复
热议问题