setBlend

setBlend( mode )

Changes the blending mode when you draw. The 'mode' given should be a string or symbol describing the mode to switch to.

Currently this supports 4 types of blending:

Although you can use 'clear' and 'stencil' when drawing to the screen, they are primarily supported for when drawing to Images. When using these on the screen they will erase all of the pixels in a section to black.

/**
 * Builds examples of each blend mode, pre-drawn to images.
 * These images are then drawn across the screen.
 */

IMAGE_WIDTH  = getScreenWidth() / 4
IMAGE_HEIGHT = getScreenHeight()

// build examples of each blend mode
modes = [ 'alpha', 'add', 'clear', 'stencil' ]
images = []
modes.each() do |mode|
    images.add(
            new Image( IMAGE_WIDTH, IMAGE_HEIGHT ).
                    fill( :pink ).setColor( :blue ).
                    setBlend( mode ).fillRect( 50,  50, 50, 50, true ).
                    setBlend( :alpha ).fillText( mode, 5, IMAGE_HEIGHT-5, :left, :bottom )
    )
end

// draw each example
onEachFrame() do |delta|
    fill( :white )
    
    x = 0
    images.eachMapping() do |i, img|
        drawImage( img, x, 0 )
        
        x = x + img.getWidth()
    end
end

How much of an effect these have is based on the alpha value. An alpha value of 0 will have effect, whilst an alpha value of 1 has the maximum effect possible.

setBlend( mode ) { ... }

This localizes the change in the mode to just the block. The block is executed, and when the block returns, the blend is set back to what it was before this function was called.

See also

setAlpha - setColor