Skip to content

Commit b3c8dd6

Browse files
committed
Decode images for the preview
1 parent 0128e4e commit b3c8dd6

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

tools/playground.html

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,39 @@
352352
}
353353

354354
if (command.type === 'image') {
355-
result += `<div class='placeholder ${command.type} ${align}'><div style='width: ${command.width / 4 * 3}px; height: ${command.height / 4 * 3}px;'>image</div></div>`;
355+
let canvas = document.createElement('canvas');
356+
canvas.width = command.width;
357+
canvas.height = command.height;
358+
359+
let ctx = canvas.getContext('2d');
360+
ctx.fillStyle = 'black';
361+
362+
for (let x = 0; x < command.width; x++) {
363+
for (let y = 0; y < command.height; y++) {
364+
let bit = 0;
365+
366+
if (command.value == 'raster') {
367+
let byte = (y * (command.width >> 3)) + (x >> 3) + 8;
368+
bit = (command.payload[byte] >> (7 - (x % 8))) & 1;
369+
}
370+
else {
371+
let skip = 4;
372+
373+
if (encoder.language == 'esc-pos') {
374+
skip = 5;
375+
}
376+
377+
let byte = (x * 3) + Math.floor(y / 8) + skip;
378+
bit = (command.payload[byte] >> (7 - (y % 8))) & 1;
379+
}
380+
381+
if (bit) {
382+
ctx.fillRect(x, y, 1, 1);
383+
}
384+
}
385+
}
386+
387+
result += `<div class='placeholder ${command.type} ${align}'><img src='${canvas.toDataURL()}' style='width: ${command.width / 4 * 3}px; height: ${command.height / 4 * 3}px;'></div>`;
356388
}
357389

358390
if (command.type === 'cut') {
@@ -759,7 +791,9 @@
759791

760792
#output[data-format="text"] .placeholder {
761793
width: 100%;
762-
margin-bottom: 1px;
794+
}
795+
#output[data-format="text"] .placeholder * {
796+
display: block;
763797
}
764798
#output[data-format="text"] .placeholder div {
765799
background: #ddd;
@@ -771,17 +805,17 @@
771805
justify-content: center;
772806
font-size: 8px;
773807
}
774-
#output[data-format="text"] .placeholder.qrcode div {
808+
#output[data-format="text"] .placeholder.qrcode * {
775809
width: calc(var(--width-a) * 16);
776810
height: calc(var(--width-a) * 16);
777811
}
778-
#output[data-format="text"] .placeholder.pdf417 div {
812+
#output[data-format="text"] .placeholder.pdf417 * {
779813
height: calc(var(--height-a) * 6);
780814
}
781-
#output[data-format="text"] .placeholder.center div {
815+
#output[data-format="text"] .placeholder.center * {
782816
margin: 0 auto;
783817
}
784-
#output[data-format="text"] .placeholder.right div {
818+
#output[data-format="text"] .placeholder.right * {
785819
margin-left: auto;
786820
}
787821

0 commit comments

Comments
 (0)