meo-skia-canvas
    Preparing search index...

    Interface CanvasRenderingContext2D

    The CanvasRenderingContext2D interface, part of the Canvas API, provides the 2D rendering context for the drawing surface of a element. It is used for drawing shapes, text, images, and other objects.

    interface CanvasRenderingContext2D {
        canvas: Canvas;
        colorFilter: ColorFilter | null;
        direction: CanvasDirection;
        dither: boolean;
        fillStyle:
            | CanvasPattern
            | CanvasGradient
            | Color4fInput
            | CanvasTexture
            | Shader;
        filter: string;
        font: string;
        fontHinting: boolean;
        fontStretch: CanvasFontStretch;
        fontVariant: FontVariantSetting;
        fontVariantCaps: CanvasFontVariantCaps;
        fontVariationSettings: string;
        globalAlpha: number;
        globalCompositeOperation: GlobalCompositeOperation;
        imageFilter: ImageFilter
        | null;
        imageSmoothingEnabled: boolean;
        imageSmoothingQuality: ImageSmoothingQuality;
        letterSpacing: string;
        lineCap: CanvasLineCap;
        lineDashFit: "follow" | "move" | "turn";
        lineDashMarker: Path2D | null;
        lineDashOffset: number;
        lineJoin: CanvasLineJoin;
        lineWidth: number;
        maskFilter: MaskFilter | null;
        miterLimit: number;
        shadowBlur: number;
        shadowColor: string;
        shadowOffsetX: number;
        shadowOffsetY: number;
        strokeStyle:
            | CanvasPattern
            | CanvasGradient
            | Color4fInput
            | CanvasTexture
            | Shader;
        textAlign: CanvasTextAlign;
        textBaseline: CanvasTextBaseline;
        textDecoration: string;
        textWrap: boolean;
        wordSpacing: string;
        get currentTransform(): DOMMatrix;
        set currentTransform(matrix: Matrix): void;
        arc(
            x: number,
            y: number,
            radius: number,
            startAngle: number,
            endAngle: number,
            counterclockwise?: boolean,
        ): void;
        arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void;
        beginPath(): void;
        bezierCurveTo(
            cp1x: number,
            cp1y: number,
            cp2x: number,
            cp2y: number,
            x: number,
            y: number,
        ): void;
        clearRect(x: number, y: number, w: number, h: number): void;
        clip(fillRule?: CanvasFillRule): void;
        clip(path: Path2D, fillRule?: CanvasFillRule): void;
        closePath(): void;
        conicCurveTo(
            cpx: number,
            cpy: number,
            x: number,
            y: number,
            weight: number,
        ): void;
        createConicGradient(
            startAngle: number,
            x: number,
            y: number,
            endAngle?: number,
        ): CanvasGradient;
        createImageData(
            width: number,
            height: number,
            settings?: ImageDataSettings,
        ): ImageData;
        createImageData(imagedata: ImageData): ImageData;
        createLinearGradient(
            x0: number,
            y0: number,
            x1: number,
            y1: number,
        ): CanvasGradient;
        createPattern(
            image: CanvasPatternSource,
            repetition: string | null,
        ): CanvasPattern | null;
        createProjection(quad: QuadOrRect, basis?: QuadOrRect): DOMMatrix;
        createRadialGradient(
            x0: number,
            y0: number,
            r0: number,
            x1: number,
            y1: number,
            r1: number,
        ): CanvasGradient;
        createTexture(
            spacing: Offset,
            options?: CreateTextureOptions,
        ): CanvasTexture;
        drawCanvas(image: Canvas, dx: number, dy: number): void;
        drawCanvas(
            image: Canvas,
            dx: number,
            dy: number,
            dw: number,
            dh: number,
        ): void;
        drawCanvas(
            image: Canvas,
            sx: number,
            sy: number,
            sw: number,
            sh: number,
            dx: number,
            dy: number,
            dw: number,
            dh: number,
        ): void;
        drawImage(image: CanvasDrawable, dx: number, dy: number): void;
        drawImage(
            image: CanvasDrawable,
            dx: number,
            dy: number,
            dw: number,
            dh: number,
        ): void;
        drawImage(
            image: CanvasDrawable,
            sx: number,
            sy: number,
            sw: number,
            sh: number,
            dx: number,
            dy: number,
            dw: number,
            dh: number,
        ): void;
        drawParagraph(paragraph: Paragraph, x: number, y: number): void;
        ellipse(
            x: number,
            y: number,
            radiusX: number,
            radiusY: number,
            rotation: number,
            startAngle: number,
            endAngle: number,
            counterclockwise?: boolean,
        ): void;
        fill(fillRule?: CanvasFillRule): void;
        fill(path: Path2D, fillRule?: CanvasFillRule): void;
        fillRect(x: number, y: number, w: number, h: number): void;
        fillText(text: string, x: number, y: number, maxWidth?: number): void;
        getImageData(
            x: number,
            y: number,
            width: number,
            height: number,
            settings?: ImageDataExportSettings,
        ): ImageData;
        getLineDash(): number[];
        getTransform(): DOMMatrix;
        isContextLost(): boolean;
        isPointInPath(x: number, y: number, fillRule?: CanvasFillRule): boolean;
        isPointInPath(
            path: Path2D,
            x: number,
            y: number,
            fillRule?: CanvasFillRule,
        ): boolean;
        isPointInStroke(x: number, y: number): boolean;
        isPointInStroke(path: Path2D, x: number, y: number): boolean;
        lineTo(x: number, y: number): void;
        measureText(text: string, maxWidth?: number): TextMetrics;
        moveTo(x: number, y: number): void;
        outlineText(text: string, maxWidth?: number): Path2D;
        putImageData(imagedata: ImageData, dx: number, dy: number): void;
        putImageData(
            imagedata: ImageData,
            dx: number,
            dy: number,
            dirtyX: number,
            dirtyY: number,
            dirtyWidth: number,
            dirtyHeight: number,
        ): void;
        quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void;
        rect(x: number, y: number, w: number, h: number): void;
        reset(): void;
        resetTransform(): void;
        restore(): void;
        rotate(angle: number): void;
        roundRect(
            x: number,
            y: number,
            w: number,
            h: number,
            radii?: number | DOMPointInit | (number | DOMPointInit)[],
        ): void;
        save(): void;
        saveLayer(
            alpha?: number,
            bounds?: [number, number, number, number] | null,
            backdrop?: ImageFilter | null,
        ): void;
        scale(x: number, y: number): void;
        setLineDash(segments: Iterable<number>): void;
        setTransform(
            a: number,
            b: number,
            c: number,
            d: number,
            e: number,
            f: number,
        ): void;
        setTransform(transform?: Matrix): void;
        stroke(): void;
        stroke(path: Path2D): void;
        strokeRect(x: number, y: number, w: number, h: number): void;
        strokeText(text: string, x: number, y: number, maxWidth?: number): void;
        transform(
            a: number,
            b: number,
            c: number,
            d: number,
            e: number,
            f: number,
        ): void;
        transform(transform: Matrix): void;
        translate(x: number, y: number): void;
    }

    Hierarchy (View Summary)

    Index
    canvas: Canvas
    colorFilter: ColorFilter | null

    Color filter applied during drawing. Set null to disable.

    🧪 Not in the HTML Canvas standard.

    direction: CanvasDirection
    dither: boolean

    Dither draws to break up banding in gradients and dark frames on 8-bit surfaces. Mirrors CanvasKit's Paint.setDither. Not part of the HTML Canvas standard. Default false.

    🧪 Not in the HTML Canvas standard.

    fillStyle:
        | CanvasPattern
        | CanvasGradient
        | Color4fInput
        | CanvasTexture
        | Shader

    Solid color, gradient, pattern, or texture used for fills.

    Color inputs follow the [Color4fInput] convention:

    • A CSS string ("#ff8800", "rgb(...)", named colors) is parsed as sRGB-gamma.
    • A [r, g, b, a] array carries premultiplied linear-light floats interpreted in the surface's working color space; pass these to avoid the lossy sRGB-encoding round-trip used by the CSS form (the shape mirrors TextStyleInput.color and CanvasKit's Paint.setColor).

    MDN Reference

    filter: string
    font: string
    fontHinting: boolean

    🧪 Not in the HTML Canvas standard.

    fontStretch: CanvasFontStretch
    fontVariant: FontVariantSetting

    🧪 Not in the HTML Canvas standard.

    fontVariantCaps: CanvasFontVariantCaps

    The capitalization axis of CanvasRenderingContext2D.fontVariant.

    This is the CSS longhand and fontVariant the shorthand, so writing here replaces only the caps token and leaves the other axes -- figures, ligatures, alternates -- as they were. An unrecognised value is ignored, as the standard requires of an attribute setter.

    MDN Reference

    fontVariationSettings: string

    🧪 Not in the HTML Canvas standard.

    globalAlpha: number
    globalCompositeOperation: GlobalCompositeOperation
    imageFilter: ImageFilter | null

    Image filter applied during drawing. Set null to disable.

    🧪 Not in the HTML Canvas standard.

    imageSmoothingEnabled: boolean
    imageSmoothingQuality: ImageSmoothingQuality
    letterSpacing: string
    lineCap: CanvasLineCap
    lineDashFit: "follow" | "move" | "turn"

    🧪 Not in the HTML Canvas standard.

    lineDashMarker: Path2D | null

    🧪 Not in the HTML Canvas standard.

    lineDashOffset: number
    lineJoin: CanvasLineJoin
    lineWidth: number
    maskFilter: MaskFilter | null

    Coverage-mask filter (styled blur) applied during drawing -- glows, feathered edges, outline blur. Set null to disable.

    🧪 Not in the HTML Canvas standard.

    miterLimit: number
    shadowBlur: number
    shadowColor: string
    shadowOffsetX: number
    shadowOffsetY: number
    strokeStyle:
        | CanvasPattern
        | CanvasGradient
        | Color4fInput
        | CanvasTexture
        | Shader

    Solid color, gradient, pattern, texture, or shader used for strokes. See fillStyle for the color-input contract.

    MDN Reference

    textAlign: CanvasTextAlign
    textBaseline: CanvasTextBaseline
    textDecoration: string

    🧪 Not in the HTML Canvas standard.

    textWrap: boolean

    🧪 Not in the HTML Canvas standard.

    wordSpacing: string
    • get currentTransform(): DOMMatrix

      The context's current transformation matrix, as an alternative to getTransform() and setTransform().

      The setter takes anything new DOMMatrix() does -- a matrix, a CSS transform string, an {a, b, c, d, e, f} object, or a 6- or 16-element array -- and replaces the transform rather than multiplying into it.

      🧪 Not in the HTML Canvas standard.

      Returns DOMMatrix

    • set currentTransform(matrix: Matrix): void

      Replaces the current transform, accepting the same matrix forms CanvasTransform.setTransform does.

      🧪 Not in the HTML Canvas standard.

      Parameters

      Returns void

    • Parameters

      • x: number
      • y: number
      • radius: number
      • startAngle: number
      • endAngle: number
      • Optionalcounterclockwise: boolean

      Returns void

    • Parameters

      • x1: number
      • y1: number
      • x2: number
      • y2: number
      • radius: number

      Returns void

    • Parameters

      • cp1x: number
      • cp1y: number
      • cp2x: number
      • cp2y: number
      • x: number
      • y: number

      Returns void

    • Curve to (x, y), pulled toward the control point.

      weight sets how close the curve comes: 0 draws a straight line, 1 matches quadraticCurveTo, and larger values pull it nearer the control point.

      🧪 Not in the HTML Canvas standard.

      Parameters

      • cpx: number
      • cpy: number
      • x: number
      • y: number
      • weight: number

      Returns void

    • MDN Reference

      The optional fourth argument is this library's own: the Canvas API always sweeps a full turn, and Skia can sweep any arc. Omitting it draws exactly what a browser draws.

      Parameters

      • startAngle: number
      • x: number
      • y: number
      • OptionalendAngle: number

      Returns CanvasGradient

    • Draw another canvas, replaying its contents as vectors.

      Unlike CanvasRenderingContext2D.drawImage, which rasterizes the source first, this keeps text as text and paths as paths -- so an SVG or PDF export of the result stays selectable and scalable.

      The replay is clipped to the destination rectangle, so anything that spreads -- an imageFilter blur, a shadow -- stops at its edge instead of bleeding past it the way it does through drawImage. Give the destination room if you want the spread. This is inherited behaviour, matching upstream, not a rule the Canvas standard sets: drawCanvas has no standard counterpart, and drawImage does let a filter spread.

      🧪 Not in the HTML Canvas standard.

      Parameters

      • image: Canvas
      • dx: number
      • dy: number

      Returns void

    • As above, scaled to fill the dw by dh rectangle at (dx, dy), and clipped to it.

      🧪 Not in the HTML Canvas standard.

      Parameters

      • image: Canvas
      • dx: number
      • dy: number
      • dw: number
      • dh: number

      Returns void

    • As above, replaying only the sw by sh region of image at (sx, sy) into the destination rectangle.

      🧪 Not in the HTML Canvas standard.

      Parameters

      • image: Canvas
      • sx: number
      • sy: number
      • sw: number
      • sh: number
      • dx: number
      • dy: number
      • dw: number
      • dh: number

      Returns void

    • Paints a laid-out Paragraph with its top-left corner at (x, y).

      This is the only way to render a Paragraph; the rest of that API builds and measures one. Rich text, per-span styling, strut-controlled leading and inline placeholders all arrive through here rather than through CanvasRenderingContext2D.fillText.

      Three differences from fillText that are easy to trip over:

      • Paragraph.layout must have been called first. A paragraph that has never been laid out has no line breaks to draw, and this silently paints nothing rather than throwing.
      • (x, y) is the top-left of the text block, not a baseline. The same coordinates passed to fillText put the text roughly one line higher.
      • Colour comes from the text style, not from fillStyle. Setting fillStyle before the call changes nothing; colour, opacity and decoration are whatever the TextStyleInput carried.
      • The compositing state does apply, along with the transform and clip: the paragraph is drawn as a group, so globalAlpha fades it and globalCompositeOperation composites it. Before 4.2.0 both were dropped, and every blend mode behaved as source-over.

      Parameters

      Returns void

      const builder = ParagraphBuilder.Make({
      textAlign: "center",
      textStyle: { fontSize: 18, color: [0, 0, 0, 1] },
      });
      builder.addText("Wrapped, styled, measured text.");

      const paragraph = builder.build();
      paragraph.layout(320); // wrap width, in pixels
      ctx.drawParagraph(paragraph, 20, 20);

      🧪 Not in the HTML Canvas standard.
    • Parameters

      • x: number
      • y: number
      • radiusX: number
      • radiusY: number
      • rotation: number
      • startAngle: number
      • endAngle: number
      • Optionalcounterclockwise: boolean

      Returns void

    • Always false.

      Context loss is a GPU-compositor event -- a browser reclaiming the backing store of a backgrounded tab -- and there is no compositor here. A canvas either has its surface or its construction failed.

      MDN Reference

      Returns boolean

    • 🧪 Not in the HTML Canvas standard.

      Parameters

      • text: string
      • OptionalmaxWidth: number

      Returns Path2D

    • Parameters

      Returns void

    • As above, writing only the dirtyWidth by dirtyHeight region of imagedata at (dirtyX, dirtyY) rather than all of it.

      MDN Reference

      Parameters

      • imagedata: ImageData
      • dx: number
      • dy: number
      • dirtyX: number
      • dirtyY: number
      • dirtyWidth: number
      • dirtyHeight: number

      Returns void

    • Push an isolated compositing layer (CanvasKit saveLayer). Draws until the matching restore() accumulate into the layer, which is then composited onto the canvas at alpha (default 1) with the current globalCompositeOperation. bounds is an optional [x, y, w, h] that clips the layer -- Skia describes it as a sizing hint for the offscreen, but nothing outside it is drawn. backdrop applies an ImageFilter to the content behind the layer (blur-behind / frosted glass). Not part of the HTML Canvas standard.

      🧪 Not in the HTML Canvas standard.

      Parameters

      • Optionalalpha: number
      • Optionalbounds: [number, number, number, number] | null
      • Optionalbackdrop: ImageFilter | null

      Returns void

    • Parameters

      • a: number
      • b: number
      • c: number
      • d: number
      • e: number
      • f: number

      Returns void

    • Replaces the transform with transform, or resets it when nothing is given.

      The matrix forms are this fork's extension: the standard takes six numbers, and these also accept a DOMMatrix, a matrix-like object or a CSS transform string.

      MDN Reference

      Parameters

      Returns void