Class CanvasMedia

The CanvasMedia class allows pre-rendering of media (such as images or video) onto a canvas. This can be useful for reducing payloads by preparing the media for further use in a more optimized form.

Documentation

Hierarchy (View Summary)

Constructors

Accessors

  • get canRender(): boolean

    Checks if the canvas is ready to render.

    Returns boolean

  • get canvas(): HTMLCanvasElement

    The canvas element instance.

    Returns HTMLCanvasElement

  • get ctx(): CanvasRenderingContext2D

    Returns the 2D rendering context

    Returns CanvasRenderingContext2D

  • get dpr(): number

    Current device pixel ratio.

    Returns number

  • get height(): number

    Canvas height (DPR applied).

    Returns number

  • get isDestroyed(): boolean

    Indicates whether the instance has been destroyed.

    After destruction, destroy is a no-op.

    Returns boolean

  • get name(): string

    The name of the module, derived from the class name

    Returns string

  • get offsetHeight(): number

    Height without DPR scaling.

    Returns number

  • get offsetWidth(): number

    Width without DPR scaling.

    Returns number

  • get prefix(): string

    Optional prefix for classnames used by the module

    Returns string

  • get width(): number

    Canvas width (DPR applied).

    Returns number

Methods

  • Adds prefixed classes to an element and removes them on destroy.

    Skips classes that are already present on the element.

    Parameters

    • element: Element

      Target DOM element.

    • ...classes: string[]

      Unprefixed class name segments.

    Returns void

  • Builds one or more prefixed class names.

    Parameters

    • ...classes: string[]

      Unprefixed class name segments.

    Returns string

    Space-separated prefixed class string.

  • Destroys the canvas.

    Returns void

  • Set default mutable props

    Returns {
        __mutableProp: true;
        dpr: number | "auto";
        height: number | "auto";
        rule:
            | "center"
            | "cover"
            | "contain"
            | "top-left"
            | "top-right"
            | "bottom-left"
            | "bottom-right";
        width: number
        | "auto";
    }

    • __mutableProp: true
    • dpr: number | "auto"

      Device Pixel Ratio (DPR). Use 'auto' for automatic detection.

      'auto'
      
    • height: number | "auto"

      Canvas height. Use 'auto' to match the container's height.

      'auto'
      
    • rule:
          | "center"
          | "cover"
          | "contain"
          | "top-left"
          | "top-right"
          | "bottom-left"
          | "bottom-right"

      Defines how the media element is positioned within the canvas.

      'cover'
      
    • width: number | "auto"

      Canvas width. Use 'auto' to match the container's width.

      'auto'
      
  • Get default static props

    Returns {
        __staticProp: true;
        append: boolean;
        autoRenderVideo: boolean;
        container: HTMLElement;
        media:
            | HTMLImageElement
            | HTMLVideoElement
            | HTMLCanvasElement
            | Canvas<
                ICanvasCallbacksMap<ICanvasMutableProps>,
                ICanvasStaticProps,
                ICanvasMutableProps,
            >
            | SVGImageElement;
        resizeDebounce: number;
        resizeOnInit: boolean;
        resizeOnRuntime: boolean;
        viewportTarget: keyof IViewportCallbacksMap;
    }

    • __staticProp: true
    • append: boolean

      If true, appends the canvas to the container. Ignored if container is null.

      true
      
    • autoRenderVideo: boolean

      If true, video elements will automatically render on each frame update.

      true
      
    • container: HTMLElement

      Parent element used to determine canvas size. If null, it uses the viewport.

      null
      
    • media:
          | HTMLImageElement
          | HTMLVideoElement
          | HTMLCanvasElement
          | Canvas<
              ICanvasCallbacksMap<ICanvasMutableProps>,
              ICanvasStaticProps,
              ICanvasMutableProps,
          >
          | SVGImageElement

      The media element to be rendered. Accepts a Canvas instance or common media elements like images and videos.

    • resizeDebounce: number

      Debounce time (ms) for resize handling.

      0
      
    • resizeOnInit: boolean

      Automatically adjusts canvas size on initialization.

      true
      
    • resizeOnRuntime: boolean

      Enables dynamic resizing based on viewport or container changes.

      false
      
    • viewportTarget: keyof IViewportCallbacksMap

      Defines which dimension(s) should trigger a resize.

      'any'
      
  • Destroys the instance and runs all registered cleanup actions.

    Safe to call multiple times; subsequent calls are ignored.

    Execution order:

    1. _destroy
    2. all onDestroy callbacks, in registration order
    3. isDestroyed is set to true

    Returns void

  • Registers a function to run when the instance is destroyed.

    If the instance is already destroyed, action runs immediately.

    Parameters

    • action: () => void

      Cleanup function (for example remove listeners or DOM classes).

    Returns void

  • Pre-renders the media resource onto the canvas.

    Returns void

  • Resize the canvas

    Returns void