html2canvas如果未将画布添加到主体,则生成空白

问题描述 投票:0回答:1

这个想法很简单,被广泛描述:使用html2canvas插件获取屏幕截图,然后使用Ajax将图像发送到服务器。

为此,我使用

html2canvas(document.querySelector("body"))
    .then(canvas => {

        document.body.appendChild(canvas) // => pay attention to this line

        const b64 = canvas.toDataURL("image/png").replace(/.*,/, '');
        theFunctionThatSendB64UsingAjax(b64)
        return;
     })

所以,这段优雅的代码效果很好!它生成一个canvas元素,将其附加到主体,然后将base64格式的字符串发送到服务器。

但是我不想显示画布或图像。因此,显而易见的是删除该行:

document.body.appendChild(canvas)

当我这样做时,生成的图像总是空白。

所以,外面有人至少有线索吗?!或者,替代html2canvas。

[而且,如果有问题,服务器正在运行golang脚本,该脚本会将图像存储在mongodb中。服务器端工作正常!

javascript ajax html2canvas
1个回答
0
投票

document.body.appendChild行之前和之后的“画布”是什么?

© www.soinside.com 2019 - 2024. All rights reserved.