Linear Gradient Shape

Demonstrates creating and animating a linear gradient Game Object, including adjusting its position and rotation.

Linear Gradient Shape, running
800 × 600
GitHub
class Example extends Phaser.Scene
{
    bg;
    gradient;

    create ()
    {
        // Sneaky background gradient
        this.bg = this.add.gradient({
            shapeMode: 2,
            start: { x: 0.5, y: 0.5 },
            shape: { x: 0, y: 0.55 },
            bands: [
                {
                    colorStart: 0x442211,
                    colorEnd: 0x6699bb,
                    colorSpace: 1,
                    interpolation: 4,
                }
            ]
        }, 400, 300, 1400, 800)
        .setRotation(-0.25);

        this.gradient = this.add.gradient({
            shapeMode: 0, // LINEAR
        }, 400, 300, 400, 400);

        // Or, because this is default:
        // this.gradient = this.add.gradient({}, 400, 300, 400, 400);
    }

    update (time, delta)
    {
        const t = this.game.loop.time;
        this.bg.rotation = 0.25 * Math.sin(t / 3000);
        this.gradient.rotation = 0.03 * Math.sin(t / 1234);
        this.gradient.setPosition(
            400 + 16 * Math.sin(t / 876),
            300 + 16 * Math.sin(t / 765)
        );
    }
}

const config = {
    scale: { mode: Phaser.Scale.FIT },
    type: Phaser.WEBGL,
    width: 800,
    height: 600,
    backgroundColor: '#2d2d2d',
    parent: 'game-container',
    scene: Example
};

const game = new Phaser.Game(config);

More in Gradient14 examples