| Reference | Downloads | Github

Setting opacity for fade effect


Hi everyone,

I’m pretty sure this has an easy solution, I’m just not seeing it. I’m trying to have a mask of hash marks i.e. ############# fade away gradually. I’m using this code:

Beginning of experiment:

trekMask = visual.TextStim(win=win, font=‘Courier’, color=‘white’,
height=0.05, pos=(0.0,0.0), text=’#########’)

Beginning of Routine:

Each Frame:
maskOpacity = maskOpacity - .002
trekMask.opacity = maskOpacity

Even though the value of maskOpacity is decreasing, the mask is not changing on the screen. It remains at opacity 1. I’ve also set the blending mode to ‘average’.

Any ideas would be appreciated.



You’ve tagged this as “coding” but it’s actually a Builder experiment, right (this affects any answers)?



@unagi_pie, instead of setting the opacity, you can set the text contrast, which has the same effect of reducing the opacity.



Sorry for posting in the wrong forum. I just assumed because I had a question about a custom code element…but whatever.

@dvbridges I’ve tried to use contrast instead of opacity, but even though the mask is becoming the same grey color as the background, it is still obscuring the word that should be appearing from behind the mask.

Is it not possible to set the opacity of a text stimulus?



It looks like this has been reported before in

As a one-off solution, you could manually set the opacity of the text stimulus, e.g.

text._pygletTextObj.color = (1, 1, 1, maskOpacity)

(the fragment shader takes care of making RGB correct)

Potential longer-term solutions:

  • Add the above to TextStim.draw() (conditional on shader usage)
  • Add something like the above in the setter for opacity, rather than having to call it every time during draw()
  • Pass opacity into the fragment shader (e.g. make fragSignedColorTexFont take an RGBA uniform)
1 Like