Filters

Next2D Player provides various visual filters that can be applied to DisplayObjects.

Applying Filters

const { Sprite } = next2d.display;
const { BlurFilter, DropShadowFilter, GlowFilter } = next2d.filters;

const sprite = new Sprite();

// Single filter
sprite.filters = [new BlurFilter(4, 4)];

// Multiple filters
sprite.filters = [
    new DropShadowFilter(4, 45, 0x000000, 0.5),
    new GlowFilter(0xff0000, 1, 8, 8)
];

// Remove filters
sprite.filters = null;

Available Filters

FilterDescription
BlurFilterBlur effect
DropShadowFilterDrop shadow effect
GlowFilterGlow effect
BevelFilterBevel effect
ColorMatrixFilterColor matrix transformation
ConvolutionFilterConvolution effect
DisplacementMapFilterDisplacement map effect
GradientBevelFilterGradient bevel effect
GradientGlowFilterGradient glow effect

BlurFilter

Applies a blur effect. You can create blurs ranging from a softly unfocused look to a Gaussian blur.

const { BlurFilter } = next2d.filters;

new BlurFilter(blurX, blurY, quality);

Properties

PropertyTypeDefaultDescription
blurXnumber4The amount of horizontal blur (0-255)
blurYnumber4The amount of vertical blur (0-255)
qualitynumber1The number of times to perform the blur (0-15)

DropShadowFilter

Applies a drop shadow effect. Style options include inner shadow, outer shadow, and knockout mode.

const { DropShadowFilter } = next2d.filters;

new DropShadowFilter(
    distance, angle, color, alpha,
    blurX, blurY, strength, quality,
    inner, knockout, hideObject
);

Properties

PropertyTypeDefaultDescription
alphanumber1The alpha transparency value for the shadow (0-1)
anglenumber45The angle of the shadow (-360 to 360 degrees)
blurXnumber4The amount of horizontal blur (0-255)
blurYnumber4The amount of vertical blur (0-255)
colornumber0The color of the shadow (0x000000-0xFFFFFF)
distancenumber4The offset distance for the shadow (-255 to 255 pixels)
hideObjectbooleanfalseIndicates whether or not the object is hidden
innerbooleanfalseSpecifies whether the shadow is an inner shadow
knockoutbooleanfalseSpecifies whether the object has a knockout effect
qualitynumber1The number of times to perform the blur (0-15)
strengthnumber1The strength of the imprint or spread (0-255)

GlowFilter

Applies a glow effect. Style options include inner glow, outer glow, and knockout mode.

const { GlowFilter } = next2d.filters;

new GlowFilter(
    color, alpha, blurX, blurY,
    strength, quality, inner, knockout
);

Properties

PropertyTypeDefaultDescription
alphanumber1The alpha transparency value for the glow (0-1)
blurXnumber4The amount of horizontal blur (0-255)
blurYnumber4The amount of vertical blur (0-255)
colornumber0The color of the glow (0x000000-0xFFFFFF)
innerbooleanfalseSpecifies whether the glow is an inner glow
knockoutbooleanfalseSpecifies whether the object has a knockout effect
qualitynumber1The number of times to perform the blur (0-15)
strengthnumber1The strength of the imprint or spread (0-255)

BevelFilter

Applies a bevel effect. Gives objects a three-dimensional look.

const { BevelFilter } = next2d.filters;

new BevelFilter(
    distance, angle, highlightColor, highlightAlpha,
    shadowColor, shadowAlpha, blurX, blurY,
    strength, quality, type, knockout
);

Properties

PropertyTypeDefaultDescription
anglenumber45The angle of the bevel (-360 to 360 degrees)
blurXnumber4The amount of horizontal blur (0-255)
blurYnumber4The amount of vertical blur (0-255)
distancenumber4The offset distance for the bevel (-255 to 255 pixels)
highlightAlphanumber1The alpha transparency value for the highlight color (0-1)
highlightColornumber0xFFFFFFThe highlight color of the bevel (0x000000-0xFFFFFF)
knockoutbooleanfalseSpecifies whether the object has a knockout effect
qualitynumber1The number of times to perform the blur (0-15)
shadowAlphanumber1The alpha transparency value for the shadow color (0-1)
shadowColornumber0The shadow color of the bevel (0x000000-0xFFFFFF)
strengthnumber1The strength of the imprint or spread (0-255)
typestring”inner”The placement of the bevel (“inner”, “outer”, “full”)

ColorMatrixFilter

Applies a 4x5 color matrix transformation. Can adjust brightness, contrast, saturation, hue, and more.

const { ColorMatrixFilter } = next2d.filters;

new ColorMatrixFilter(matrix);

Properties

PropertyTypeDefaultDescription
matrixnumber[]Identity matrixAn array of 20 items for 4x5 color transform

Default Matrix Value (Identity Matrix)

[
    1, 0, 0, 0, 0,
    0, 1, 0, 0, 0,
    0, 0, 1, 0, 0,
    0, 0, 0, 1, 0
]

ConvolutionFilter

Applies a matrix convolution filter effect. Can achieve blur, edge detection, sharpen, emboss, bevel, and more.

const { ConvolutionFilter } = next2d.filters;

new ConvolutionFilter(
    matrixX, matrixY, matrix, divisor, bias,
    preserveAlpha, clamp, color, alpha
);

Properties

PropertyTypeDefaultDescription
alphanumber0The alpha transparency value for out-of-bounds pixels (0-1)
biasnumber0The amount of bias to add to the result of the matrix transformation
clampbooleantrueIndicates whether the image should be clamped
colornumber0The hexadecimal color to substitute for out-of-bounds pixels (0x000000-0xFFFFFF)
divisornumber1The divisor used during matrix transformation
matrixnumber[] | nullnullAn array of values used for matrix transformation
matrixXnumber0The x dimension of the matrix (number of columns, 0-15)
matrixYnumber0The y dimension of the matrix (number of rows, 0-15)
preserveAlphabooleantrueIndicates if the alpha channel is preserved without the filter effect

DisplacementMapFilter

Uses the pixel values from a BitmapData object to perform a displacement of an object.

const { DisplacementMapFilter } = next2d.filters;

new DisplacementMapFilter(
    bitmapBuffer, bitmapWidth, bitmapHeight,
    mapPointX, mapPointY, componentX, componentY,
    scaleX, scaleY, mode, color, alpha
);

Properties

PropertyTypeDefaultDescription
alphanumber0The alpha transparency value for out-of-bounds displacements (0-1)
bitmapBufferUint8Array | nullnullA buffer containing the displacement map data
bitmapHeightnumber0The height of the displacement map image
bitmapWidthnumber0The width of the displacement map image
colornumber0The color to use for out-of-bounds displacements (0x000000-0xFFFFFF)
componentXnumber0The color channel to use in the map image to displace the x result
componentYnumber0The color channel to use in the map image to displace the y result
mapPointXnumber0The X offset of the map point
mapPointYnumber0The Y offset of the map point
modestring”wrap”The mode for the filter (“wrap”, “clamp”, “ignore”, “color”)
scaleXnumber0The multiplier to scale the x displacement result (-65535 to 65535)
scaleYnumber0The multiplier to scale the y displacement result (-65535 to 65535)

GradientBevelFilter

Applies a gradient bevel effect. A beveled edge enhanced with gradient color makes objects look three-dimensional.

const { GradientBevelFilter } = next2d.filters;

new GradientBevelFilter(
    distance, angle, colors, alphas, ratios,
    blurX, blurY, strength, quality, type, knockout
);

Properties

PropertyTypeDefaultDescription
alphasnumber[] | nullnullAn array of alpha transparency values for the corresponding colors (each value 0-1)
anglenumber45The angle of the bevel (-360 to 360 degrees)
blurXnumber4The amount of horizontal blur (0-255)
blurYnumber4The amount of vertical blur (0-255)
colorsnumber[] | nullnullAn array of RGB hexadecimal color values to use in the gradient
distancenumber4The offset distance for the bevel (-255 to 255 pixels)
knockoutbooleanfalseSpecifies whether the object has a knockout effect
qualitynumber1The number of times to perform the blur (0-15)
ratiosnumber[] | nullnullAn array of color distribution ratios for the corresponding colors (each value 0-255)
strengthnumber1The strength of the imprint or spread (0-255)
typestring”inner”The placement of the bevel (“inner”, “outer”, “full”)

GradientGlowFilter

Applies a gradient glow effect. A realistic-looking glow with a controllable color gradient.

const { GradientGlowFilter } = next2d.filters;

new GradientGlowFilter(
    distance, angle, colors, alphas, ratios,
    blurX, blurY, strength, quality, type, knockout
);

Properties

PropertyTypeDefaultDescription
alphasnumber[] | nullnullAn array of alpha transparency values for the corresponding colors (each value 0-1)
anglenumber45The angle of the glow (-360 to 360 degrees)
blurXnumber4The amount of horizontal blur (0-255)
blurYnumber4The amount of vertical blur (0-255)
colorsnumber[] | nullnullAn array of RGB hexadecimal color values to use in the gradient
distancenumber4The offset distance for the glow (-255 to 255 pixels)
knockoutbooleanfalseSpecifies whether the object has a knockout effect
qualitynumber1The number of times to perform the blur (0-15)
ratiosnumber[] | nullnullAn array of color distribution ratios for the corresponding colors (each value 0-255)
strengthnumber1The strength of the imprint or spread (0-255)
typestring”outer”The placement of the glow (“inner”, “outer”, “full”)

Usage Examples

Button Hover Effect

const { Sprite } = next2d.display;
const { GlowFilter } = next2d.filters;

const button = new Sprite();

button.addEventListener("rollOver", () => {
    button.filters = [
        new GlowFilter(0x00ff00, 0.8, 10, 10)
    ];
});

button.addEventListener("rollOut", () => {
    button.filters = null;
});

Text with Shadow

const { TextField } = next2d.text;
const { DropShadowFilter } = next2d.filters;

const textField = new TextField();
textField.text = "Hello World";
textField.filters = [
    new DropShadowFilter(2, 45, 0x000000, 0.5, 2, 2)
];

Combined Filters

const { GlowFilter, DropShadowFilter, BlurFilter } = next2d.filters;

sprite.filters = [
    // Outer glow
    new GlowFilter(0x0088ff, 0.8, 15, 15, 2, 1, false),
    // Drop shadow
    new DropShadowFilter(4, 45, 0x000000, 0.6, 4, 4),
    // Slight blur
    new BlurFilter(1, 1, 1)
];

Grayscale with ColorMatrixFilter

const { ColorMatrixFilter } = next2d.filters;

// Grayscale transformation matrix
const grayscaleMatrix = [
    0.299, 0.587, 0.114, 0, 0,
    0.299, 0.587, 0.114, 0, 0,
    0.299, 0.587, 0.114, 0, 0,
    0,     0,     0,     1, 0
];

sprite.filters = [new ColorMatrixFilter(grayscaleMatrix)];

Gradient Glow Effect

const { GradientGlowFilter } = next2d.filters;

sprite.filters = [
    new GradientGlowFilter(
        4, 45,
        [0xff0000, 0x00ff00, 0x0000ff], // colors
        [1, 1, 1],                       // alphas
        [0, 128, 255],                   // ratios
        10, 10, 2, 1, "outer", false
    )
];