Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

In phần tử HTML5 canvas

Đoạn mã sau hiển thị phần tử HTML5 canvas:

<a href="javascript:print_voucher()">PRINT CANVAS</a>
function print_canvas() {
  $("#canvas_voucher").printElement();
}

Ở đây canvas_voucher là ID của canvas element. Để làm cho nó hoạt động, cần chuyển canvas thành .png image URL và mở trong cửa sổ trình duyệt mới. Print dialog được kích hoạt để cho người dùng in trang.

function print_canvasr(){
  var win1 = window.open();
  win1.document.write("<br><img src='" + canvas.toDataURL() + "'/>");
  win1.print();
  win1.location.reload();
}