Skip to content

Commit 25551a4

Browse files
authored
Merge pull request #4236 from Tyriar/console_image
Add console.image helper
2 parents e7b347a + 4f4ce01 commit 25551a4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

demo/client.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,3 +992,33 @@ function addOverviewRuler(): void {
992992
term.registerDecoration({ marker: term.registerMarker(10), overviewRulerOptions: { color: '#ffffff80', position: 'full' } });
993993
}
994994

995+
(console as any).image = (source: ImageData | HTMLCanvasElement, scale: number = 1) => {
996+
function getBox(width: number, height: number): any {
997+
return {
998+
string: '+',
999+
style: 'font-size: 1px; padding: ' + Math.floor(height/2) + 'px ' + Math.floor(width/2) + 'px; line-height: ' + height + 'px;'
1000+
};
1001+
}
1002+
if (source instanceof HTMLCanvasElement) {
1003+
source = source.getContext('2d')?.getImageData(0, 0, source.width, source.height)!;
1004+
}
1005+
const canvas = document.createElement('canvas');
1006+
canvas.width = source.width;
1007+
canvas.height = source.height;
1008+
const ctx = canvas.getContext('2d')!;
1009+
ctx.putImageData(source, 0, 0);
1010+
1011+
const sw = source.width * scale;
1012+
const sh = source.height * scale;
1013+
const dim = getBox(sw, sh);
1014+
console.log(
1015+
`Image: ${source.width} x ${source.height}\n%c${dim.string}`,
1016+
`${dim.style}background: url(${canvas.toDataURL()}); background-size: ${sw}px ${sh}px; background-repeat: no-repeat; color: transparent;`
1017+
);
1018+
console.groupCollapsed('Zoomed');
1019+
console.log(
1020+
`%c${dim.string}`,
1021+
`${getBox(sw * 10, sh * 10).style}background: url(${canvas.toDataURL()}); background-size: ${sw * 10}px ${sh * 10}px; background-repeat: no-repeat; color: transparent; image-rendering: pixelated;-ms-interpolation-mode: nearest-neighbor;`
1022+
);
1023+
console.groupEnd();
1024+
};

0 commit comments

Comments
 (0)