Skip to main content
Version: Phaser v4.0.0

Key

The Key filter controller.

The Key effect removes or isolates a specific color from an image. It can be used to remove a background color from an image, or to isolate a specific color for further processing.

By default, Key will remove pixels that match the key color. You can instead keep only the matching pixels by setting isolate.

The threshold and feather settings control how closely the key color matches. A match is measured by "distance between color vectors"; that is, how close the RGB values of the pixel are to the RGB values of the key color.

A Key filter is added to a Camera via the FilterList component:


const camera = this.cameras.main;

camera.filters.internal.addKey({ color: '#ff00ff' });

camera.filters.external.addKey({ color: 0x00ff00 });

Constructor

new Key(camera, [config])

Parameters

nametypeoptionaldefaultdescription
cameraPhaser.Cameras.Scene2D.CameraNoThe camera that owns this filter.
configPhaser.Types.Filters.KeyConfigYes"{}"The configuration for the filter.

Scope: static

Extends

Phaser.Filters.Controller

Source: src/filters/Key.js#L11
Since: 4.0.0

Inherited Members

From Phaser.Filters.Controller:


Public Members

color

color: Array.<number>

Description:

The color to use for the key. It is an array of 4 numbers between 0 and 1, representing the RGBA values.

Source: src/filters/Key.js#L50
Since: 4.0.0


feather

feather: number

Description:

The feathering amount for the key color. Pixels outside the threshold, but still within the feather, will be a partial match. This should be a value between 0 and 1.

Source: src/filters/Key.js#L93
Since: 4.0.0


isolate

isolate: boolean

Description:

Whether to keep the region matching the key color. If true, the region matching the key color will be kept, and the rest will be removed. If false, the region matching the key color will be removed, and the rest will be kept.

Source: src/filters/Key.js#L63
Since: 4.0.0


threshold

threshold: number

Description:

The threshold for the key color. A pixel is considered to be the key color if the difference between the pixel and the key color is less than the threshold. This should be between 0 and 1. The default threshold is 1 / 16, which is a good starting point for most images.

Source: src/filters/Key.js#L78
Since: 4.0.0


Inherited Methods

From Phaser.Filters.Controller:


Public Methods

setAlpha

<instance> setAlpha(alpha)

Description:

Sets the alpha value to use for the key. Alpha controls the opacity of pixels matched by the key color, in the range 0 to 1. This is stored in the fourth element of the color array. The RGB color values are preserved.

Parameters:

nametypeoptionaldescription
alphanumberNoThe alpha value to set on the key texture, between 0 (fully transparent) and 1 (fully opaque).

Returns: Phaser.Filters.Key - This Filter Controller.

Source: src/filters/Key.js#L108
Since: 4.0.0


setColor

<instance> setColor(color)

Description:

Sets the color to use for the key. This is stored in the first three elements of the color array. The alpha value is preserved.

Parameters:

nametypeoptionaldescription
colornumber | stringArray.<number>Phaser.Display.Color

Returns: Phaser.Filters.Key - This Filter Controller.

Source: src/filters/Key.js#L125
Since: 4.0.0