maskmaskingp5.jsclipping

masking, or clipping mask with p5.js


I want to use one shape (for instance a rectangle) to act as a mask or clipping path for another shape (for instance a circle, or line) in P5.js

I can see solutions for using images as masks, but not shapes. It seems mask() is not a function of shapes: https://p5js.org/reference/#/p5.Image/mask


Solution

  • yes, you can.

    Here is a working code example:

    let circleMask;
    let myImage;
    
    function setup() {
      createCanvas(400, 400);
    
      circleMask = createGraphics(128, 128);
    
      myImage = loadImage('FzFH41IucIY.jpg');
    }
    
    function draw() {
      background(255);
    
      circleMask.fill('rgba(0, 0, 0, 1)');
    
      circleMask.circle(64, 64, 128);
    
      myImage.mask(circleMask);
    
      image(myImage, 200 - 64, 200 - 64, 128, 128);
    }