关于javascript:JS生成海报

32次阅读

共计 668 个字符,预计需要花费 2 分钟才能阅读完成。

下载 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

正文完
 0