SVG to Canvas with d3.js

前端 未结 4 527
闹比i
闹比i 2020-11-28 09:07

Has anyone tried using a svg to canvas library when creating d3.js visualizations? I\'ve tried to use canvg.js and d3.js to convert svg to canvas from within an android 2.3

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 09:20

    The answer by @ace is very good, however it doesn't handle the case of external CSS stylesheets. My example below will automatically style the generated image exactly how the original SVG looks, even if it's pulling styles form separate stylesheets.

    // when called, will open a new tab with the SVG
    // which can then be right-clicked and 'save as...'
    function saveSVG(){
    
        // get styles from all required stylesheets
        // http://www.coffeegnome.net/converting-svg-to-png-with-canvg/
        var style = "\n";
        var requiredSheets = ['phylogram_d3.css', 'open_sans.css']; // list of required CSS
        for (var i=0; i');
    };
    

    And, to be complete, the button that calls the function:

    // save button
    d3.select('body')
        .append("button")
        .on("click",saveSVG)
        .attr('class', 'btn btn-success')
    

提交回复
热议问题