有没有一种方法可以将表格和画布组合成一个单独的图像
Is there a way to combine a table and canvas into a single image?
上下文
-
我已经使用
HTML
和JavaScript
创建了一个页面。 -
该页面包含一个表和一个画布,用于根据
JavaScript
代码绘制图像。 -
我知道如何将画布元素转换为图像(通过使用toDataURL())。
问题
我还需要将HTML表与画布结合起来,并将其生成为一个单独的图像。有办法做到这一点吗?
<!DOCTYPE html>
<html moznomarginboxes mozdisallowselectionprint>
<head>
<script>
function byId(id,parent){return (parent == undefined ? document : parent).getElementById(id);}
window.addEventListener('load', onLoadd, false);
function onLoadd(evt)
{
drawBkg(byId('canvas'), 3.78, "0.35", "green");
}
function drawBkg(canvasElem, squareSize, minorLineWidthStr, lineColStr)
{
var nLinesDone = 0;
var i, curX, curY;
var ctx = canvasElem.getContext('2d');
ctx.clearRect(0,0,canvasElem.width,canvasElem.height);
// draw the vertical lines
curX=0;
ctx.strokeStyle = lineColStr;
while (curX < canvasElem.width)
{
if (nLinesDone % 5 == 0)
ctx.lineWidth = 0.69;
else
ctx.lineWidth = minorLineWidthStr;
ctx.beginPath();
ctx.moveTo(curX, 0);
ctx.lineTo(curX, canvasElem.height);
ctx.stroke();
curX += squareSize;
nLinesDone++;
}
// draw the horizontal lines
curY=0;
nLinesDone = 0;
while (curY < canvasElem.height)
{
if (nLinesDone % 5 == 0)
ctx.lineWidth = 0.69;
else
ctx.lineWidth = minorLineWidthStr;
ctx.beginPath();
ctx.moveTo(0, curY);
ctx.lineTo(canvasElem.width, curY);
ctx.stroke();
curY += squareSize;
nLinesDone++;
}
}
function print_voucher(){
var win=window.open();
win.document.write("<br><img style='margin:0' src='"+canvas.toDataURL()+"'/>");
win.print();
win.location.reload();
}
</script>
</head>
<body>
<div id='txt'></div>
<table style="width:600">
<tr>
<td>Patient ID:</td>
<td>1234</td>
</tr>
<tr>
<td>Patient Name:</td>
<td>Jackson</td>
</tr>
<tr>
<td>Patient Age:</td>
<td>94</td>
</tr>
<tr>
<td>Patient Gender:</td>
<td>Male</td>
</tr>
</table>
<canvas id="canvas" width="1030" height="600"></canvas>
<button onclick="onDocLoaded()">Click Me</button>
<input type="button" id="test" value="Print" onClick="print_voucher();"> </input>
</body>
</html>
您可以使用html2canvas。
代码:
function print_voucher() {
var container = document.getElementById("container");
html2canvas(container, {
onrendered: function(canvas) {
document.body.appendChild(canvas);
},
width: container.width,
height: container.height
});
}
工作小提琴:(注意,我将生成的画布图像附加到身体上)
function byId(id, parent) {
return (parent == undefined ? document : parent).getElementById(id);
}
window.addEventListener('load', onLoadd, false);
function onLoadd(evt) {
drawBkg(byId('canvas'), 3.78, "0.35", "green");
}
function drawBkg(canvasElem, squareSize, minorLineWidthStr, lineColStr) {
var nLinesDone = 0;
var i, curX, curY;
var ctx = canvasElem.getContext('2d');
ctx.clearRect(0, 0, canvasElem.width, canvasElem.height);
// draw the vertical lines
curX = 0;
ctx.strokeStyle = lineColStr;
while (curX < canvasElem.width) {
if (nLinesDone % 5 == 0)
ctx.lineWidth = 0.69;
else
ctx.lineWidth = minorLineWidthStr;
ctx.beginPath();
ctx.moveTo(curX, 0);
ctx.lineTo(curX, canvasElem.height);
ctx.stroke();
curX += squareSize;
nLinesDone++;
}
// draw the horizontal lines
curY = 0;
nLinesDone = 0;
while (curY < canvasElem.height) {
if (nLinesDone % 5 == 0)
ctx.lineWidth = 0.69;
else
ctx.lineWidth = minorLineWidthStr;
ctx.beginPath();
ctx.moveTo(0, curY);
ctx.lineTo(canvasElem.width, curY);
ctx.stroke();
curY += squareSize;
nLinesDone++;
}
}
function print_voucher() {
var container = document.getElementById("container");
html2canvas(container, {
onrendered: function(canvas) {
document.body.appendChild(canvas);
},
width: container.width,
height: container.height
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<body>
<div id='txt'></div>
<div id="container">
<table style="width:600">
<tr>
<td>Patient ID:</td>
<td>1234</td>
</tr>
<tr>
<td>Patient Name:</td>
<td>Jackson</td>
</tr>
<tr>
<td>Patient Age:</td>
<td>94</td>
</tr>
<tr>
<td>Patient Gender:</td>
<td>Male</td>
</tr>
</table>
<canvas id="canvas" width="1030" height="600"></canvas>
</div>
<button onclick="onDocLoaded()">Click Me</button>
<input type="button" id="test" value="Print" onClick="print_voucher();"></input>
相关文章:
- 将7个图像组合为一个,一个在另一个之上,并输出为单个图像
- 如何通过在灯箱外单击或在与图像贴图组合时点击esc退出灯箱
- KineticJS可以组合图像吗
- 将背景颜色和图像过滤器的组合应用于svg路径
- Fancybox - 将图像和 iframe 组合在一个图库中
- 将图像组合在一起并在窗口加载时对图像进行动画处理
- 显示图像的组合框
- Jquery/图像映射组合无法正常工作
- 有没有一种方法可以将表格和画布组合成一个单独的图像
- JavaScript代码更改组合框输出中的所有图像,而不是所需的图像
- 如何在图像地图中组合热点
- 如何在具有显示库的放大弹出窗口中组合内联元素和图像
- 在一个画布中组合两个图像
- 如何在角数据表中组合数据和显示图像
- 我如何在Extjs中添加图像到组合
- 如何设置加载组合框的图像
- Javascript组合图像
- 我如何使用ajax和javascript来构建一个图像组合
- 在填充组合框列表时显示加载图像
- 产品画廊-这个网站如何为每一个可能的组合生成图像