mirror of
https://github.com/NaitLee/Cat-Printer.git
synced 2025-05-16 07:10:30 -07:00
58 lines
2.5 KiB
JavaScript
58 lines
2.5 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');
|
|
previewButton = document.getElementById('preview_button');
|
|
threshold = 0.2;
|
|
canvasPreview = 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(i18N.get('Please select a device'));
|
|
return;
|
|
}
|
|
if (this.canvasPreview.children.length == 0) {
|
|
notice(i18N.get('Please preview image first'));
|
|
return;
|
|
}
|
|
html2canvas(this.container).then(canvas => {
|
|
notice(i18N.get('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);
|
|
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(i18N.get(xhr.responseText));
|
|
}
|
|
xhr.send(pbm_data);
|
|
});
|
|
});
|
|
this.previewButton.addEventListener('click', event => {
|
|
if (this.canvasPreview.children[0] != null) this.canvasPreview.children[0].remove();
|
|
html2canvas(this.container).then(canvas => {
|
|
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.canvasPreview.appendChild(canvas);
|
|
});
|
|
})
|
|
}
|
|
}
|
|
|
|
var document_printer = new DocumentPrinter();
|