gpt4 book ai didi

javascript - 将 html 字符串渲染到 Canvas 而不显示 html 本身

转载 作者:行者123 更新时间:2023-11-30 12:30:12 26 4
gpt4 key购买 nike

我想将 html 字符串转换为图像,类似于此处的操作: html2canvas creates a canvas with a width and height of zero?

但是我不想在页面上显示结果图像以外的任何内容。因此,我不能按照那里的建议简单地将 html 字符串添加到 DOM。我认为我想要实现的目标是可能的,因为 html2canvas test console就是这样做的。

我尝试了以下产生空结果的方法:

var doc = document.implementation.createHTMLDocument();
doc.open();
doc.write("<html><header></header><body><div>test</div></body></html>");
doc.close();

html2canvas(doc.body, {
onrendered:function(newCanvas) {
document.body.appendChild(newCanvas);
}
});
<script src="https://github.com/niklasvh/html2canvas/blob/master/dist/html2canvas.js"></script>

有人可以建议这样做的方法吗?

最佳答案

我想通了。这是 the link这让我朝着正确的方向前进。还值得检查 this page 的源代码这说明了这个想法。

我们设定

style="overflow:hidden"

容器的属性并将我们要渲染的内容移到视口(viewport)之外

style="position:absolute;top:0px;left:3000px;"

希望对某人有所帮助。

关于javascript - 将 html 字符串渲染到 Canvas 而不显示 html 本身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27983731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com