Open In App

p5.js blendMode() Function

The blendMode() function is used to blend two pixels according to the given blending mode. The different types of blending modes have different methods of mixing the source pixels with the ones present in the display window, to produce the resulting pixel.

Syntax:



blendMode( mode )

Parameters: This function accepts single parameter mode that is used to blend the pixels. It can have the following values:

The example below illustrates the blendMode() function in p5.js:



Example:




function setup() {
  blendModes = [
    BLEND,
    ADD,
    DARKEST,
    LIGHTEST,
    DIFFERENCE,
    EXCLUSION,
    MULTIPLY,
    OVERLAY,
    HARD_LIGHT,
    SOFT_LIGHT,
    DODGE,
    BURN
  ]
  
  index = 0;
  currBlendMode = blendModes[index];
  
  createCanvas(600, 300);
  textSize(20);
}
  
function draw() {
  clear();
  text('Click on the button to change the blend mode', 20, 20);
  text("Current blendMode: " + currBlendMode, 20, 50);
  
  btn = createButton("Change blendMode");
  btn.position(30, 80);
  btn.mousePressed(changeBlendMode);
  
  // Set the blend mode
  blendMode(currBlendMode);
  
  // Draw the first circle
  fill("red");
  circle(180, 200, 150);
  
  // Draw the second circle
  fill("green");
  circle(260, 200, 150);
}
  
function changeBlendMode() {
  if (index < blendModes.length - 1)
    index++;
  else
    index = 0;
  currBlendMode = blendModes[index];
}

Output:

Online editor: https://editor.p5js.org/

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/amp/

Reference: https://p5js.org/reference/#/p5/blendMode


Article Tags :