从浏览器中将jvectormap保存为png

时间:2014-09-19 15:43:30

标签: svg jvectormap

有没有办法将结果svg jvectormap保存为png?我希望用户能够点击保存或下载按钮,并能够以某种图像格式将地图下载到他们的桌面。

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点,这是一种最有效的方式(通过使用canvg), 这是一个working example on JSfiddle..

$(function(){
    $('#world-map').vectorMap({
        map: 'world_mill_en',
        backgroundColor: 'white',
        normalizeFunction: 'polynomial',
        regionsSelectable: true,
        regionsSelectableOne: true,
        zoomOnScroll: true,
        zoomButtons: true,
        regionStyle: {
            initial: {
                fill: "red",
                "fill-opacity": 1,
                stroke: "none",
                "stroke-width": 0,
                "stroke-opacity": 1
            },
            hover: {
                fill: "blue",
                "fill-opacity": 1
            },
            selected: {
                fill: "#EC6602",
                "fill-opacity": 1
            },
            selectedHover: {
                fill: "#EC6602",
                "fill-opacity": 1
            }
        },
        onRegionClick: function(e, country){

            var map = $("#world-map").vectorMap("get", "mapObject");
            $("#world-map").vectorMap("set", "focus", country);
        }
    });
});

function saveImage() {
    var oSerializer = new XMLSerializer();
    var sXML = oSerializer.serializeToString(document.querySelector("#world-map svg"));

    canvg(document.getElementById('canvas'), sXML,{ ignoreMouse: true, ignoreAnimation: true })
    var imgData = canvas.toDataURL("image/png");
    window.location = imgData.replace("image/png", "image/octet-stream");
    // You can use http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js 
    // if you want to force filename.ext
}
相关问题