Randomly selecting blendmode

Is there a way to randomly select blendmode? I tried replacing blendMode(DARKEST) with a variable, but that didn't seem to work. Console log spits out a random blendmode, but when I try to use it it fails.

var blendModes = ['BLEND', 'ADD', 'DARKEST', 'ADD', 'DIFFERENCE ', 'EXCLUSION', 'MULTIPLY', 'SCREEN', 'REPLACE', 'OVERLAY', 'HARD_LIGHT', 'SOFT_LIGHT', 'DODGE', 'BURN']; //https://p5js.org/reference/#/p5/blendMode

var randBlendmode = blendModes[Math.floor(Math.random() * blendModes.length)]; console.log(randBlendmode);

blendMode(randBlendmode);

Test: https://codepen.io/mskogly/pen/ZojbYe?editors=0111 (the blendmode line is commented out)

Tagged:

Answers

  • Answer ✓
    int bmode = int(random(14));
    if( bmode == 0 ){
      blendmode(BLEND);
    } else if( bmode == 1 ){
      blendmode(ADD);
    } else ...
    

    It's not pretty, but it'll do it.

    You might luck out... try println(BLEND) (and all the other modes) and see if that is actually a value. Then you could try blendMode(0);, maybe. Maybe.

  • I think your problem is that you're creating Strings rather than using the constants?

    eg. this seems to work - got an error without using constants inside setup()

    var blendModes = [];
    
    function setup() {
      createCanvas(960, 600);
      blendModes = [BLEND, ADD, DARKEST, ADD, DIFFERENCE];
    }
    
  • creating an if would solve it. not as elegant, but works :)

  • @mskogly actually using the right constants solves it! ;-)

  • edited May 17

    To underscore the solution:

    Wrong -- don't use strings:

    var blendModes = ['BLEND', 'ADD'];
    

    Right -- do use the names of built-in constant variables:

    var blendModes = [BLEND, ADD];
    
Sign In or Register to comment.