Method to convert HTML5 canvas to SVG?

前端 未结 4 1647
自闭症患者
自闭症患者 2020-12-01 06:05

Need to convert an HTML5 canvas to SVG for editing ? Pointer will be appreciated

4条回答
  •  佛祖请我去吃肉
    2020-12-01 06:42

    I think the canvas must already be drawing an svg for this method, but I found it in the course of trying to create a download svg button myself, also ran into this stack overflow question in the same search figured it may be relevant.

    from https://bramp.github.io/js-sequence-diagrams/

    around line 200ish, but who knows he may edit site in the future

    editor is just a div element, and for the purpose of this noise, he's just packing the stuff the svg was generated off of into the downloaded svg.

    diagram_div is the canvas the actual svg is sitting in.

    function(ev) {
      var svg = diagram_div.find('svg')[0];
      var width = parseInt(svg.width.baseVal.value);
      var height = parseInt(svg.height.baseVal.value);
      var data = editor.getValue();
      var xml = '' + data + '' + svg.innerHTML + '';
    
      var a = $(this);
      a.attr("download", "diagram.svg"); // TODO I could put title here
      a.attr("href", "data:image/svg+xml," + encodeURIComponent(xml));
    }
    

提交回复
热议问题