【发布时间】:2016-05-06 21:34:54
【问题描述】:
我正在使用 html2canvas.js 0.4.1 将 DOM 转换为画布以进行打印。当文本在 span 上有背景颜色,并且 span 换行到下一行时,画布版本会覆盖 2 行换行的整个矩形。它不仅涵盖文本。此外,它隐藏了先前跨度中的文本。
下面的小提琴很好地展示了它。 “一”跨度完全被“二二”跨度上的蓝色背景覆盖。
https://jsfiddle.net/sddah9k2/1/
css:
#content {
width:200px;
}
.select {
background-color:#3efcdb
}
html:
<div id='content'>
<span >one one one one one one </span>
<span class="select" >two two two two two two </span>
<span >three three three three three </span>
</div>
<div id="img-out"></div>
代码:
html2canvas($("#content"), {
onrendered: function(canvas) {
theCanvas = canvas;
document.body.appendChild(canvas);
// Convert and download as image
Canvas2Image.saveAsPNG(canvas);
$("#img-out").append(canvas);
// Clean up
//document.body.removeChild(canvas);
}
});
我的理论是 html2canvas 无法判断 span 的边界框是什么,或者它只是不支持多个矩形作为边界。
我可以在 HTML 文本上使用任何类型的 CSS 效果来在 html2canvas 中正确呈现吗?它不必是背景颜色,但我必须以某种方式表明环绕跨度已突出显示。
【问题讨论】:
-
是的,似乎内联元素的背景颜色效果不佳。项目的 github 页面上已经有 an issue。另请注意,在最新版本中,有一些改进:jsfiddle.net/sddah9k2/5
标签: html css canvas html2canvas