setAlpha

setAlpha( alpha )

Sets the alpha value to use when drawing. This is a value from 0.0 to 1.0

The alpha value refers to the transparency of what is drawn. An alpha of 1.0 causes the item to be fully drawn, with 0.5 it is half drawn, whilst 0.0 causes it to be entirely invisible.

/* Draws a white rectangle, that fades in and out, based on alpha */

angle = 0
onEachFrame() do |delta|
    fill( :black )
    
    angle = angle + delta/25.0
    alpha = angle.sin( 0, 1 )
    
    setAlpha( alpha )
    fillRect( 100, 100, 100, 100 )
end

setAlpha( alpha ) { ... }

This version of setAlpha takes a block, and yields to it after setting the alpha value. This allows the alpha value to only be altered during the lifetime of the block.

The alpha value is set back to it's original value, after the block returns.

/* 
 * Draws multiple rectangles, with varying amounts of alpha.
 */

NUM_ITERATIONS = 36

onEachFrame() do |delta|
    fill( :black )
    
    3.times() do |i|
        NUM_ITERATIONS.times() do |j|
            setAlpha( j / NUM_ITERATIONS ) do
                fillRect( i*110 + j*10, j*10, 20, 20 )
            end
        end
    end
end

See also

setBlend - setColor - multAlpha - getAlpha