xxxxxxxxxx
42
let img;
let rounded;
function preload() {
// load original image
img = loadImage("dog.jpeg");
}
function setup() {
createCanvas(400, 200);
// create rounded image
rounded = roundImage(img, 100);
}
function draw() {
background(220);
image(img, 0, 0, 200, 200);
image(rounded, 200, 0, 200, 200);
}
function roundImage(img, radius) {
// create a seperate graphic to draw our mask
// onto, this tells us which pixels of the image
// to keep, so we'll draw a rounded rectangle on it
const mask = createGraphics(img.width, img.height);
// first make it blank
mask.clear();
mask.noStroke();
mask.fill(255);
// then draw the rectangle with rounded corners on it
mask.rect(0, 0, img.width, img.height, radius);
// img.get() creates a copy of the image
const newImage = img.get();
// p5.Image has a mask function, we'll use the
// mask we created earlier
newImage.mask(mask);
return newImage;
}