Cat-Printer/www/print-document.js
2021-09-07 19:55:19 +08:00

44 lines
1.7 KiB
JavaScript

///<reference path="main.js" />
///<reference path="main.d.ts" />
class DocumentPrinter {
WIDTH = 384;
thresholdInput = document.getElementById('filter_threshold');
bluetoothMACInput = document.getElementById('bt_mac');
container = document.getElementById('container');
printButton = document.getElementById('print_button');
threshold = 0.6;
imagePreview = document.getElementById('image_preview');
monoMethod = imageDataColorToMonoSquare;
constructor() {
this.thresholdInput.onchange = event => {
this.threshold = this.thresholdInput.value;
}
this.printButton.addEventListener('click', event => {
let mac_address = this.bluetoothMACInput.value;
if (mac_address == '') {
notice('Please select a device');
return;
}
html2canvas(this.container).then(canvas => {
notice('Printing, please wait.');
let context = canvas.getContext('2d');
let imagedata = context.getImageData(0, 0, this.WIDTH, canvas.height);
let mono_imagedata = this.monoMethod(imagedata, this.threshold);
context.putImageData(mono_imagedata, 0, 0);
this.imagePreview.appendChild(canvas);
let pbm_data = imageDataMonoToPBM(mono_imagedata);
let xhr = new XMLHttpRequest();
xhr.open('POST', '/~print?address=' + mac_address);
xhr.setRequestHeader('Content-Type', 'application-octet-stream');
xhr.onload = () => {
notice(xhr.responseText);
}
xhr.send(pbm_data);
})
});
}
}
var document_printer = new DocumentPrinter();