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 ctx(): CanvasRenderingContext2D

    Returns the 2D rendering context

    Returns CanvasRenderingContext2D

  • get isDestroyed(): boolean

    Checks if the module has been destroyed.

    Returns boolean

  • get name(): string

    The name of the module, derived from the class name

    Returns string

  • get prefix(): string

    Optional prefix for classnames used by the module

    Returns string

Methods

  • Adds a class name on an element, and keeps track of it for removal when the module is destroyed.

    Parameters

    • element: Element

      The target DOM element.

    • className: string

      The class name to toggle.

    Returns void

  • Helper function to generate classnames with the module's prefix.

    Parameters

    • ...classNames: string[]

      The class names to generate.

    Returns string

    A string of class names with the module's prefix applied.

  • Get default mutable properties

    Returns {
        __mutableProp: true;
        dpr: number | "auto";
        height: number | "auto";
        rule:
            | "cover"
            | "contain"
            | "top-left"
            | "top-right"
            | "bottom-left"
            | "bottom-right"
            | "center";
        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:
          | "cover"
          | "contain"
          | "top-left"
          | "top-right"
          | "bottom-left"
          | "bottom-right"
          | "center"

      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 properties

    Returns {
        __staticProp: true;
        append: boolean;
        autoRenderVideo: boolean;
        container: HTMLElement;
        media:
            | HTMLImageElement
            | HTMLCanvasElement
            | HTMLVideoElement
            | 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
          | HTMLCanvasElement
          | HTMLVideoElement
          | 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'
      
  • Adds a callback on the module's destruction.

    Parameters

    • action: () => void

      The function to execute during destruction.

    Returns void