【问题标题】:Rendering html string to canvas without showing the html itself将 html 字符串渲染到画布而不显示 html 本身
【发布时间】:2015-01-17 18:19:18
【问题描述】:

我想将一个 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);
    }    
});
&lt;script src="https://github.com/niklasvh/html2canvas/blob/master/dist/html2canvas.js"&gt;&lt;/script&gt;

任何人都可以建议这样做的方式吗?

【问题讨论】:

    标签: javascript html html2canvas


    【解决方案1】:

    我想通了。这是the link,它把我引向了正确的方向。还值得检查 this page 的源代码,它说明了这个想法。

    我们设置了

    style="overflow:hidden"
    

    为容器设置属性并将我们想要渲染的内容移到视口之外

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

    希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2023-02-21
      • 1970-01-01
      • 2014-11-27
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      相关资源
      最近更新 更多