Most appropriate sub-area of p5.js?
p5.js version
2.3.0 (also reproduces on @latest)
Web browser and version
Arc Version 1.150.1 (81866) Chromium Engine Version 149.0.7827.103
Operating system
MacOSX
Steps to reproduce this
Steps:
tint() has no effect on images in 2D mode. Both alpha tinting
(tint(255, alpha)) and color tinting are silently ignored — the image is
always drawn fully opaque and untinted. The official reference example for
tint() does not work either.
Snippet:
see https://beta.p5js.org/reference/p5/tint/
or this snippet:
let img;
function setup() {
createCanvas(200, 200);
img = createImage(100, 100);
img.loadPixels();
for (let i = 0; i < img.pixels.length; i += 4) {
img.pixels[i] = 255; // R
img.pixels[i + 3] = 255; // A
}
img.updatePixels();
}
function draw() {
background(220);
tint(255, 50); // expect ~20% opacity
image(img, 50, 50);
noTint();
}
Expected vs actual
- Expected: a faint (~20% opacity) red square.
- Actual: a fully opaque red square.
tint(255, 0) does not hide the
image either, and color tints (e.g. tint(255, 0, 0)) have no effect.
Root cause (from reading the bundled source)
tint() stores a p5.Color object:
this._renderer.states.setValue('tint', this.color(...args)).
- The 2D renderer's
_getTintedImageCanvas() treats states.tint as a
numeric array: it reads this.states.tint[3] / 255 and
this.states.tint.slice(0, 3), and branches on
this.states.tint[0] < 255 || ....
- A
p5.Color instance has no numeric indices and no .slice(), so
states.tint[3] is undefined → ctx.globalAlpha = NaN, which the canvas
ignores → the image is drawn at full opacity. For color tints, every
states.tint[i] < 255 is undefined < 255 === false, so the color branch
is never entered. Net result: tint() is a no-op for images.
A fix would convert the stored color to an RGBA array before use in
_getTintedImageCanvas() (e.g. via the color's level/_array accessor), or
store the tint as an array in the tint() setter.
I'm happy to open a PR if that helps.
Most appropriate sub-area of p5.js?
p5.js version
2.3.0 (also reproduces on @latest)
Web browser and version
Arc Version 1.150.1 (81866) Chromium Engine Version 149.0.7827.103
Operating system
MacOSX
Steps to reproduce this
Steps:
tint()has no effect on images in 2D mode. Both alpha tinting(
tint(255, alpha)) and color tinting are silently ignored — the image isalways drawn fully opaque and untinted. The official reference example for
tint()does not work either.Snippet:
see https://beta.p5js.org/reference/p5/tint/
or this snippet:
Expected vs actual
tint(255, 0)does not hide theimage either, and color tints (e.g.
tint(255, 0, 0)) have no effect.Root cause (from reading the bundled source)
tint()stores ap5.Colorobject:this._renderer.states.setValue('tint', this.color(...args))._getTintedImageCanvas()treatsstates.tintas anumeric array: it reads
this.states.tint[3] / 255andthis.states.tint.slice(0, 3), and branches onthis.states.tint[0] < 255 || ....p5.Colorinstance has no numeric indices and no.slice(), sostates.tint[3]isundefined→ctx.globalAlpha = NaN, which the canvasignores → the image is drawn at full opacity. For color tints, every
states.tint[i] < 255isundefined < 255 === false, so the color branchis never entered. Net result:
tint()is a no-op for images.A fix would convert the stored color to an RGBA array before use in
_getTintedImageCanvas()(e.g. via the color's level/_arrayaccessor), orstore the tint as an array in the
tint()setter.I'm happy to open a PR if that helps.