p5.js

P5.js Get current fill/stroke color?


I'm developing a addon library for p5.js and I need to setup several fill/stroke colors in certain functions.

Is there a way to get the current fill/stroke value so I can ensure that when the user calls said functions he doesn't have to worry about the colors that I set?

Something of this sort:

function foo(){
    var tempColor = getFill();   //Hypothetical get color function 
    // ...
    fill(color1);                //Some color I use
    // ...
    fill(color2);                //Another color I use
    // ...
    fill(tempColor);             //Reset fill color to user set
}

Edit: Although undocumented, I found some references in p5.js to a curFillColor variable but I didn't find a way to use this.


Solution

  • P5.js is open source, so you can see exactly what they do when you call the fill() function here:

    p5.prototype.fill = function() {
      this._renderer._setProperty('_fillSet', true);
      this._renderer._setProperty('_doFill', true);
      this._renderer.fill.apply(this._renderer, arguments);
      return this;
    };
    

    Which takes you to the renderer-specific fill variable. You can track that down, but basically: there isn't an easy way to get the current fill color.

    What you might want to do is to use a buffer in your library. Don't call fill() on the main sketch; call fill() on the buffer. Do all your drawing to the buffer, and then draw that buffer to the sketch.