melonJS
    Preparing search index...

    Class CanvasRenderTarget

    A 2D canvas render target: draw into it once and reuse it, as a texture, a baked background or a fog map. Make one and keep it rather than making a new one each time. The engine uses it too, for text, gradients, particle textures and tinted images. Drawn with the WebGL or WebGPU renderer, its canvas is uploaded to the GPU, and uploaded again after CanvasRenderTarget#invalidate.

    Hierarchy (View Summary)

    Index
    • Parameters

      • width: number

        the desired width of the canvas

      • height: number

        the desired height of the canvas

      • attributes: {
            antiAlias?: boolean;
            context?: string;
            offscreenCanvas?: boolean;
            transparent?: boolean;
            willReadFrequently?: boolean;
        } = defaultAttributes

        The attributes to create both the canvas and context

        • OptionalantiAlias?: boolean

          Whether to enable anti-aliasing, use false (default) for a pixelated effect.

        • Optionalcontext?: string

          the context type to be created ("2d", "webgl" — a "webgl" request creates a WebGL 2 context — or "webgpu")

        • OptionaloffscreenCanvas?: boolean

          will create an offscreenCanvas if true instead of a standard canvas

        • Optionaltransparent?: boolean

          specify if the canvas contains an alpha channel

        • OptionalwillReadFrequently?: boolean

          Indicates whether or not a lot of read-back operations are planned

      Returns CanvasRenderTarget

    attributes: {
        antiAlias: boolean;
        blendMode: string;
        context: string;
        failIfMajorPerformanceCaveat: boolean;
        offscreenCanvas: boolean;
        powerPreference: string;
        premultipliedAlpha: boolean;
        stencil: boolean;
        transparent: boolean;
        willReadFrequently: boolean;
    } & {
        antiAlias?: boolean;
        context?: string;
        offscreenCanvas?: boolean;
        transparent?: boolean;
        willReadFrequently?: boolean;
    }
    canvas: any
    context: any
    glTextureUnit: any
    WebGLVersion: number

    The WebGL version of the created context.

    since 20.0.0 — the WebGL renderer is WebGL 2 only, this is always 2

    • Returns an ImageData object representing the underlying pixel data for a specified portion of this canvas texture. (Note: when using getImageData(), it is highly recommended to use the willReadFrequently attribute when creating the corresponding canvas texture)

      Parameters

      • x: number

        The x-axis coordinate of the top-left corner of the rectangle from which the ImageData will be extracted

      • y: number

        The y-axis coordinate of the top-left corner of the rectangle from which the ImageData will be extracted

      • width: number

        The width of the rectangle from which the ImageData will be extracted. Positive values are to the right, and negative to the left

      • height: number

        The height of the rectangle from which the ImageData will be extracted. Positive values are down, and negative are up

      Returns ImageData

      The ImageData extracted from this CanvasRenderTarget.

    • Creates a Blob directly from the canvas (avoids the getImageData round-trip).

      Parameters

      • Optionaltype: string = "image/png"

        A string indicating the image format

      • Optionalquality: number

        A number between 0 and 1 for lossy formats (image/jpeg, image/webp)

      Returns Promise<Blob>

      A Promise resolving to a Blob

    • Returns a data URL directly from the canvas (avoids the getImageData/Blob round-trip). Note: not supported by OffscreenCanvas — falls back to the base implementation.

      Parameters

      • Optionaltype: string = "image/png"

        A string indicating the image format

      • Optionalquality: number

        A number between 0 and 1 for lossy formats (image/jpeg, image/webp)

      Returns Promise<string>

      A Promise resolving to a data URL string

    • Read back pixel data from this render target.

      The canvas backend can read back synchronously, so this resolves immediately; it exists so code that must work on every backend has one method to call. See RenderTarget#toImageData.

      Parameters

      • Optionalx: number

        x coordinate of the top-left corner

      • Optionaly: number

        y coordinate of the top-left corner

      • Optionalwidth: number

        width of the area to read

      • Optionalheight: number

        height of the area to read

      Returns Promise<ImageData>

      the pixel data