melonJS
    Preparing search index...

    Class Gradient

    A Gradient object representing a linear or radial gradient fill. Created via Renderer#createLinearGradient or Renderer#createRadialGradient. Can be passed to Renderer#setColor as a fill style.

    Index

    Constructors

    Properties

    Methods

    Constructors

    • Parameters

      • type: "linear" | "radial"

        the gradient type

      • coords: number[]

        gradient coordinates [x0, y0, x1, y1] for linear, [x0, y0, r0, x1, y1, r1] for radial

      Returns Gradient

    Properties

    type: "linear" | "radial"

    Methods

    • Add a color stop to the gradient.

      Parameters

      • offset: number

        value between 0.0 and 1.0

      • color: string | Color

        a CSS color string or Color object

      Returns Gradient

      this gradient for chaining

      gradient.addColorStop(0, "#FF0000");
      gradient.addColorStop(0.5, "green");
      gradient.addColorStop(1, "blue");
    • Get the interpolated color at a given position along the gradient. Useful for procedural effects like trails that need per-segment colors.

      Parameters

      • position: number

        position along the gradient (0.0–1.0)

      • out: Color

        output Color object to write into

      Returns Color

      the output Color object

      const gradient = new Gradient("linear", [0, 0, 1, 0]);
      gradient.addColorStop(0, "#ff0000");
      gradient.addColorStop(1, "#0000ff");
      gradient.getColorAt(0.5, myColor); // myColor is now purple