Converting simple p5 like this is straightforward after checking out a tutorial on basic canvas. That said, p5 is doing a lot under the hood and packing much more functionality into calls like createCanvas
, so if you find you're just reinventing many of the abstractions it already provides or using advanced features, it's probably best to just use p5.
rect(30, 30, 30, 30);
just keeps drawing on the same spot over and over, so I assume you wanted rect(30 * r, 30 * c, 30, 30);
Also, since you're not animating anything, it's probably best to use p5's noLoop
and skip requestAnimationFrame
altogether, since it's just burning CPU cycles as it stands here.
const canvas = document.createElement("canvas");
document.body.appendChild(canvas);
canvas.width = canvas.height = 300;
const ctx = canvas.getContext("2d");
(function draw() {
requestAnimationFrame(draw);
ctx.fillStyle = "rgb(50, 50, 50)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let r = 0; r < 10; r++) {
for (let c = 0; c < 10; c++) {
ctx.fillStyle = `rgb(${10 * r}, ${10 * c}, 255)`;
ctx.fillRect(30 * r, 30 * c, 30, 30);
ctx.strokeStyle = "#000";
ctx.strokeRect(30 * r, 30 * c, 30, 30);
}
}
})();