let img;
var scl = 20;
var cols, rows;
function setup() {
createCanvas(400, 400);
img = createImage(width, height);
let tmp = createGraphics(width, height);
tmp.stroke(238, 238, 238);
cols = floor(width / scl);
rows = floor(height / scl);
for (var y = 0; y < rows; y++) {
for (var x = 0; x < cols; x++) {
tmp.fill(random(255));
tmp.rect(x * scl, y * scl, scl, scl);
}
}
img.copy(tmp, 0, 0, tmp.width, tmp.height, 0, 0, tmp.width, tmp.height);
}
function draw() {
background(img);
}
why? idk design choices? createGraphics is basically createCanvas as it creates basically the same thing but it would have been nice if they introduced a toImage method into p5.graphics (and maybe they have?) anyways best of luck
Both background() & set() are incompatible w/ datatype p5.Graphics in p5js.
Obviously that’s a flawed design oversight b/c in Processing’s Java Mode its PGraphics is a subclass of PImage; so everything works smoothly as it should.
As a workaround you could just use image() instead: image(img, 0, 0);