JS生成海报

下载html2canvas

$("#btn_finished").click(function () {
    // 滚动条不置顶,生成的图片会有残缺
    document.documentElement.scrollTop = 0;
    html2canvas(document.querySelector("#theme_poster")).then(canvas => {
        var pageData = canvas.toDataURL('image/jpeg', 1.0);
        saveFile(pageData.replace("image/jpeg", "image/octet-stream"), new Date()
            .getTime() + ".jpeg");
    })
})
var saveFile = function (data, filename) {
    var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
    save_link.href = data;
    save_link.download = filename;

    var event = document.createEvent('MouseEvents');
    event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false,
        false, 0, null);
    save_link.dispatchEvent(event);
};

如果生成过程中图片不显示,有可能是跨越问题,需要涉及到CORS

你可能感兴趣的:(JS生成海报)