Class CanvasLayer

A layer which content is a canvas

Hierarchy (view full)

Constructors

Accessors

  • get canvas(): HTMLCanvasElement
  • Get output canvas

    Returns HTMLCanvasElement

Methods

  • Add a renderer to this layer. It's constructed with the layer's own width/height, so callers pass only the class and its config - never dimensions.

    Type Parameters

    Parameters

    • id: string

      unique id for this renderer within the layer

    • rendererClass: (new (width, height, params?) => T)

      the renderer class (not an instance)

        • new (width, height, params?): T
        • Parameters

          • width: number
          • height: number
          • Optional params: P

          Returns T

    • Optional params: P

      optional config forwarded to the renderer constructor

    • active: boolean = true

      if true (default) the renderer is added to the render queue

    Returns Promise<T>

    the constructed, initialised renderer instance

    Example

    const shaky = await layer.addRenderer('shaky', ShakyRenderer, { mode: 'decay' })
    shaky.triggerShake()
  • Remove a renderer from this layer. If it was in the render queue, it will be removed from there too.

    Parameters

    • id: string

      Renderer id to remove. If not found, this is a no-op.

    Returns void

  • Re-enable a previously deactivated renderer, restoring it at its original position in the queue. The renderer must have been registered via the constructor renderers dict or addRenderer.

    Parameters

    • id: string

      Renderer id to activate.

    Returns void

    Throws

    if the renderer is not registered on this layer.

  • Animate layer opacity from its current value to 1 (fully opaque).

    Parameters

    • duration: number

      fade duration in ms

    • easing: EasingFunction = Easing.easeOutSine

      easing function (defaults to easeOutSine)

    Returns Promise<void>

    resolves when the fade completes

  • Animate layer opacity from its current value to 0 (fully transparent).

    Parameters

    • duration: number

      fade duration in ms

    • easing: EasingFunction = Easing.easeOutSine

      easing function (defaults to easeOutSine)

    Returns Promise<void>

    resolves when the fade completes

  • Draw provided image onto canvas If img is a string then image is loaded before drawing

    Parameters

    • img: ImageBitmap

      bitmap object

    • Optional _options: Partial<BitmapOptions>

      options

    Returns void

  • Fill the entire layer with a color (supports alpha). The fill composites on top of existing content (actions are cumulative).

    Parameters

    • color: string

      CSS color string (e.g. '#FF0000', '#FFFFFF80', 'rgba(255,255,255,0.5)')

    Returns void

  • Draw a filled rectangle on top of existing content.

    Parameters

    • x: number

      Left position

    • y: number

      Top position

    • w: number

      Width

    • h: number

      Height

    • color: string

      CSS color string

    Returns void

  • Draw a line on top of existing content.

    Parameters

    • x1: number

      Start X

    • y1: number

      Start Y

    • x2: number

      End X

    • y2: number

      End Y

    • color: string

      CSS color string

    • lineWidth: number = 1

      Line width in pixels (default 1)

    Returns void

  • Fill the entire layer with a linear gradient on top of existing content.

    Parameters

    • colors: string[]

      Array of CSS color stops (at least 2)

    • direction: "horizontal" | "vertical" = 'horizontal'

      Gradient direction (default 'horizontal')

    Returns void

  • Draw a rectangle filled with a linear gradient on top of existing content.

    Parameters

    • x: number

      Left position

    • y: number

      Top position

    • w: number

      Width

    • h: number

      Height

    • colors: string[]

      Array of CSS color stops (at least 2)

    • direction: "horizontal" | "vertical" = 'horizontal'

      Gradient direction (default 'horizontal')

    Returns void

  • Register a draw function that will be called on every draw(). The callback receives a DrawContext with the available operations. Call draw() whenever external state changes. Pass undefined to unregister.

    Parameters

    Returns void

  • Clear the layer and re-execute the registered draw function. Call this whenever the external state driving the draw function changes.

    Returns void