Skip to content

Instantly share code, notes, and snippets.

@stengoes
Created February 13, 2020 08:37
Show Gist options
  • Save stengoes/e17781a920ba75c72eacf981360dbfcf to your computer and use it in GitHub Desktop.
Save stengoes/e17781a920ba75c72eacf981360dbfcf to your computer and use it in GitHub Desktop.
Example using html5 canvas tags to display rgb arrays.
<html>
<body>
<canvas id="mycanvas" width="2048" height="512"></canvas>
</body>
</html>
<script>
const height = 512;
const width = 256;
const depth = 4;
const data = new Uint8ClampedArray(height*width*depth);
// Fill data
for (let i = 0; i < data.length; i += 4)
{
data[i + 0] = 255; // R value
data[i + 1] = 0; // G value
data[i + 2] = 0; // B value
data[i + 3] = 255; // A value
}
const imgData = new ImageData(data, width, height);
const canvas = document.getElementById('mycanvas');
const ctx = canvas.getContext('2d');
ctx.putImageData(imgData, 0, 0);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment