meo-skia-canvas
    Preparing search index...

    Interface Canvas

    A canvas in the browser: a real <canvas> element with this library's drawing and export API assigned onto it.

    Because it is the element, everything HTMLCanvasElement offers is there at runtime — toBlob, getContext, width, height — and the DOM types describe those. What this adds is the parts of the Node API the shim implements, with the return types the browser actually produces.

    interface Canvas {
        height: number;
        jpg: Promise<ArrayBuffer>;
        pages: CanvasRenderingContext2D[];
        png: Promise<ArrayBuffer>;
        width: number;
        getContext(type: "2d"): CanvasRenderingContext2D;
        newPage(): CanvasRenderingContext2D;
        newPage(width: number, height: number): CanvasRenderingContext2D;
        toBlob(
            callback: (blob: Blob | null) => void,
            type?: string,
            quality?: number,
        ): void;
        toBuffer(
            format?: ExportFormat,
            options?: number | ExportOptions,
        ): Promise<ArrayBuffer>;
        toDataURL(format?: string, quality?: number): string;
        toFile(
            filename: string,
            options?: number | SaveOptions & { archive?: string },
        ): Promise<void>;
        toURL(
            format?: ExportFormat,
            options?: number | ExportOptions,
        ): Promise<string>;
    }

    Hierarchy

    • Omit<Canvas, Absent | Narrowed>
      • Canvas
    Index
    height: number

    Gets or sets the height of a canvas element on a document.

    MDN Reference

    jpg: Promise<ArrayBuffer>

    The canvas as JPEG bytes, as an ArrayBuffer.

    No quality is passed, so the browser's own default applies rather than the 0.92 the Node build uses. Call toBuffer("jpg", 0.8) to choose one.

    Every page added so far, oldest first, each as the context that draws it. The last entry is the current page.

    🧪 Not in the HTML Canvas standard.

    png: Promise<ArrayBuffer>

    The canvas as PNG bytes -- toBuffer("png") with no arguments, and an ArrayBuffer rather than Node's Buffer.

    width: number

    Gets or sets the width of a canvas element on a document.

    MDN Reference

    • Returns an object that provides methods and properties for drawing and manipulating images and graphics on a canvas element in a document. A context object includes information about colors, line widths, fonts, and other graphic parameters that can be drawn on a canvas.

      Parameters

      • type: "2d"

        The type of canvas to create. Skia Canvas only supports a 2-D context using canvas.getContext("2d")

        The argument is required: the runtime returns null for anything other than "2d", including no argument at all, so declaring it optional promised a context that would not arrive.

        MDN Reference

      Returns CanvasRenderingContext2D

    • Add a page, and return its drawing context.

      Pages stay drawable once added, and which of them an export takes depends on the format and the filename. PDF, TIFF, ICO and the three animated formats gather every page into one file; the rest write the current page alone, unless the filename passed to toFile contains "{}", which writes one numbered file per page.

      The size is a pair or nothing: omit both to keep the canvas's current size, or give both to resize the canvas for this page onward. Earlier pages keep the size they were created at. Passing only a width throws, rather than adding a page at a size nobody asked for.

      🧪 Not in the HTML Canvas standard.

      Returns CanvasRenderingContext2D

    • Add a page, and return its drawing context.

      Pages stay drawable once added, and which of them an export takes depends on the format and the filename. PDF, TIFF, ICO and the three animated formats gather every page into one file; the rest write the current page alone, unless the filename passed to toFile contains "{}", which writes one numbered file per page.

      The size is a pair or nothing: omit both to keep the canvas's current size, or give both to resize the canvas for this page onward. Earlier pages keep the size they were created at. Passing only a width throws, rather than adding a page at a size nobody asked for.

      🧪 Not in the HTML Canvas standard.

      Parameters

      • width: number
      • height: number

      Returns CanvasRenderingContext2D

    • Encode the canvas and hand the result to a callback as a Blob.

      Callback-style and returning void, as the standard defines it, rather than the promise the other exporters on this class return. type is a mime type -- "image/png" -- not the bare format name they take.

      A failed encode calls back with null rather than raising: the callback has already been handed off by the time the encode runs.

      MDN Reference

      Parameters

      • callback: (blob: Blob | null) => void
      • Optionaltype: string
      • Optionalquality: number

      Returns void

    • The encoded bytes, as an ArrayBuffer rather than Node's Buffer: this comes from Blob.arrayBuffer(), and there is no Buffer in a page.

      format defaults to PNG. A number in place of the options is read as quality, as it is in the Node build.

      Parameters

      Returns Promise<ArrayBuffer>

    • format accepts a bare extension ("png") or a mime type ("image/png"), and defaults to PNG as in the browser.

      MDN Reference

      Parameters

      • Optionalformat: string
      • Optionalquality: number

      Returns string

    • Downloads the canvas rather than writing a file: there is no filesystem here, so filename names the download and the browser puts it wherever downloads go.

      A filename containing "{}" downloads every page as a zip archive instead, one numbered file inside it -- the same "{}" that writes a numbered sequence in Node. Page count alone does not trigger it: without the braces this downloads the current page, as the other exporters do. The archive is named by the archive option, defaulting to "canvas.zip".

      Zipping needs JSZip in your bundle. Without it the promise still resolves and nothing downloads -- the reason is logged to the console rather than thrown.

      Parameters

      • filename: string
      • Optionaloptions: number | SaveOptions & { archive?: string }

      Returns Promise<void>