xxxxxxxxxx
72
let img;
let checkbox;
let countElement;
function preload() {
img = loadImage("turkey.png");
}
function setup() {
const canvas = createCanvas(windowWidth, windowHeight - 50);
canvas.parent('canvas');
checkbox = select('#checkbox');
countElement = select('#count');
const controls = select('#controls');
controls.style('width', width + 'px');
noSmooth();
imageMode(CENTER);
background(random(255), random(255), random(255));
}
function draw() {
let stampImg = img;
if(checkbox.checked()) {
stampImg = randomColorTurkey();
}
const newSize = random(16, 128);
image(stampImg,
random(width), random(height),
newSize, newSize);
countElement.html('Turkeys: ' + nfc(frameCount));
}
function randomColorTurkey() {
const pg = createGraphics(img.width, img.height);
const colorMap = new Map();
for (let y = 0; y < img.height; y++) {
for (let x = 0; x < img.width; x++) {
const inputColor = img.get(x, y);
if(alpha(inputColor) == 0){
continue;
}
const colorKey = inputColor.toString();
if (!colorMap.has(colorKey)) {
colorMap.set(colorKey,
randomColor());
}
pg.fill(colorMap.get(colorKey));
pg.noStroke();
pg.rect(x, y, 1, 1);
}
}
return pg;
}
function randomColor(){
return color(random(255), random(255), random(255));
}