Skip to content

Instantly share code, notes, and snippets.

@evitolins evitolins/app.js
Created Jan 21, 2015

Embed
What would you like to do?
// Pixel Data
var pixel = 10;
var pixels = [
[0,0,0,0,0,0,0,1,0,0],
[1,0,0,0,0,0,0,0,3,0],
[0,0,1,0,0,0,0,1,0,0],
[0,0,0,0,1,0,0,1,0,0],
[1,1,0,0,0,1,0,0,0,0],
[0,0,0,0,0,2,0,0,1,0],
[2,0,1,0,0,0,0,0,1,0],
[0,0,0,0,3,0,0,0,0,0],
[0,1,0,0,0,0,1,0,0,0],
[1,2,0,1,0,0,0,0,0,1]
];
// Setup Canvas
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
canvas.width = (pixels.length) * pixel;
canvas.height = (pixels[0].length) * pixel;
// Renderer
var render = function(pixels){
var x,y,i,ii;
for (i=0; i<pixels.length; i++) {
for (ii=0; ii<pixels[i].length; ii++) {
x = ii * pixel;
y = i * pixel;
// Skip 'blank' pixel
if (pixels[i][ii] === 0) {
continue;
}
// Choose pixel color and draw
context.beginPath();
if (pixels[i][ii] === 1) {
context.fillStyle = 'red';
}
if (pixels[i][ii] === 2) {
context.fillStyle = 'green';
}
if (pixels[i][ii] === 3) {
context.fillStyle = 'blue';
}
context.rect(x, y, pixel, pixel);
context.fill();
}
}
};
render(pixels);
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<canvas id="canvas" width=100 height=100></canvas>
</body>
</html>
canvas {
border: 1px solid #aaa;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.