<div class="wrapper">
<canvas id="canvas"></canvas>
</div>
html,
body {
margin: 0;
width: 100%;
height: 100%;
}
.wrapper {
width: 100%;
height: 100%;
}
canvas {
image-rendering: pixelated;
}
const canvas = document.getElementById("canvas");
const outputContainer = document.getElementsByClassName("wrapper")[0];
const ctx = canvas.getContext("2d");
canvas.width = outputContainer.offsetWidth;
canvas.height = outputContainer.offsetHeight;
const colors = ["#107AB0", "#FDC1C5", "#FD5956"];
const pixelSize = 8;
draw();
function draw() {
for (let y = 0; y < canvas.height; y += pixelSize) {
for (let x = 0; x < canvas.width; x += pixelSize) {
ctx.fillStyle = colors[randomNum(0, colors.length - 1)];
ctx.fillRect(x, y, pixelSize, pixelSize);
}
}
}
function randomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
canvas.addEventListener("click", draw);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.