Hi all!
Background
I’m trying to figure out how to set the alpha channel on an image using tint when the sketch is in WEBGL mode.
I’m building a React app that integrates p5.js. My code uses a physics library to create bodies in the sketch and the physics library controls the drawing of p5 images (the physics library is responsible for updating the positions/orientation of the images).
When users press a button press in the interface, the alpha value stored on a Collection of physics objects is updated. I modified the physics library to read this alpha value into the external library when drawing the images (all SVGs). Toggling the button changes alpha from being fully opaque (255) to semi-transparent (50). My code works as expected in normal draw mode, but when I turn on WEBGL there is no change in the transparency of the image.
If this helps, my project uses:
- p5.js v0.10.2
- react-p5-wrapper v.2.0.0 (https://www.npmjs.com/package/react-p5-wrapper)
- b2.js wrapper physics library for box2d (https://github.com/bobcgausa/cook-js)
Code
To get to work with React, I pass in a reference to the sketch instance when the physics world is created, what I call ctx
.
I modified the draw function of the library to apply a tint to the image, but this isn’t working in WEBGL. Note: I’ve removed the code that isn’t directly relevant to drawing p5.Images.
draw() {
// ...
// Code to establish the position of the object from the physics library before drawing
// Drawing logic
if (this.fixtures[i].image != null) {
if (Array.isArray(xy))
ctx.image(this.fixtures[i].image, 0, 0);
else {
// MY MODIFIED CODE
ctx.push();
ctx.tint(255, this.parentClass.getParentCollection().getOpacity());
ctx.image(this.fixtures[i].image, 0, 0, xy.x, xy.y);
ctx.pop();
}
}
else {
// ....
// if no image is provided, the code just draws primitive p5 shapes
}
return false;
}
Question
Is it possible to update my draw statement to set the same tint behavior in WEBGL that works in regular draw mode?