Skip to content

Instantly share code, notes, and snippets.

@CezaryDanielNowak
Last active January 18, 2022 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CezaryDanielNowak/8ec9804e3169a7d5d340d1ab24c6729b to your computer and use it in GitHub Desktop.
Save CezaryDanielNowak/8ec9804e3169a7d5d340d1ab24c6729b to your computer and use it in GitHub Desktop.
/*
* This code helps with logging image processed in the worker.
*
* Simple visual explanation for image transformation.
*/
// WORKER:
postMessage({
imageData,
});
// WINDOW:
worker.onmessage = (e) => {
if (e.data.imageData) {
return logImageData(e.data.imageData, e.data.text);
}
};
function logImageData(imageData, text) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = imageData.width;
canvas.height = imageData.height;
ctx.putImageData(imageData, 0, 0);
// Create a new `Image` instance
const image = new Image();
image.onload = function() {
console.log(
'%c+',
`font-size: 0;
padding: ${Math.ceil(this.height / 2)}px ${Math.ceil(this.width / 2)}px;
line-height: 0;
background: url(${this.src.replace(/\(/g, '%28').replace(/\)/g, '%29')});
background-size: ${this.width}px ${this.height}px;
color: transparent;
background-repeat: no-repeat;`
);
};
image.src = canvas.toDataURL();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment