var img;
var masked;
var mask;
function setup() {
createCanvas(400, 400);
// creates an invisible graphic buffer
// that contains a rectangle
img = createGraphics(200, 200);
img.rect(0, 0, 100, 100);
// creates an invisible graphic buffer
// that contains a circle
mask = createGraphics(200, 200);
mask.ellipse(100, 100, 100, 100);
// we apply the circle in 'mask' as a clipping mask
// to the rectangle image contained in 'img'
// we save the result in a third buffer: 'masked'
( masked = img.get() ).mask( mask.get() );
}
function draw() {
background(130,0,10);
// print the buffer after the clipping
image(masked, 0, 0);
}
<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>