Skip to content

Instantly share code, notes, and snippets.

@mariotacke
Created July 11, 2019 18:46
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 mariotacke/7109d629fa7eb0ac53720544cb5163a6 to your computer and use it in GitHub Desktop.
Save mariotacke/7109d629fa7eb0ac53720544cb5163a6 to your computer and use it in GitHub Desktop.
const getAverageRGB = (frame) => {
const length = frame.data.length / 4;
let r = 0;
let g = 0;
let b = 0;
for (let i = 0; i < length; i++) {
r += frame.data[i * 4 + 0];
g += frame.data[i * 4 + 1];
b += frame.data[i * 4 + 2];
}
return {
r: r / length,
g: g / length,
b: b / length,
};
};
// ...
for (let y = 0; y < height; y += fontHeight) {
for (let x = 0; x < width; x += fontWidth) {
const frameSection = hiddenContext.getImageData(x, y, fontWidth, fontHeight);
const { r, g, b } = getAverageRGB(frameSection);
outputContext.fillStyle = `rgb(${r},${g},${b})`;
outputContext.fillRect(x, y, fontWidth, fontHeight);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment