Skip to main content
Module

x/canvas/mod.ts>SkCanvas

Canvas API for Deno, ported from canvaskit-wasm (Skia).
Go to Latest
interface SkCanvas
implements EmbindObject<SkCanvas>
import { type SkCanvas } from "https://deno.land/x/canvas@v1.0.0/mod.ts";

See SkCanvas.h for more information on this class.

Methods

clear(color: InputColor): void

Fills the current clip with the given color using Src BlendMode. This has the effect of replacing all pixels contained by clip with color.

clipPath(
path: SkPath,
op: ClipOp,
doAntiAlias: boolean,
): void

Replaces clip with the intersection or difference of the current clip and path, with an aliased or anti-aliased clip edge.

clipRect(
rect: InputRect,
op: ClipOp,
doAntiAlias: boolean,
): void

Replaces clip with the intersection or difference of the current clip and rect, with an aliased or anti-aliased clip edge.

clipRRect(
rrect: InputRRect,
op: ClipOp,
doAntiAlias: boolean,
): void

Replaces clip with the intersection or difference of the current clip and rrect, with an aliased or anti-aliased clip edge.

concat(m: InputMatrix): void

Replaces current matrix with m premultiplied with the existing matrix.

drawArc(
oval: InputRect,
startAngle: AngleInDegrees,
sweepAngle: AngleInDegrees,
useCenter: boolean,
paint: SkPaint,
): void

Draws arc using clip, SkMatrix, and SkPaint paint.

Arc is part of oval bounded by oval, sweeping from startAngle to startAngle plus sweepAngle. startAngle and sweepAngle are in degrees.

drawAtlas(
atlas: SkImage,
paint: SkPaint,
blendMode?: BlendMode,
colors?: ColorIntArray,
): void

Draws a set of sprites from atlas, using clip, SkMatrix, and optional SkPaint paint.

drawCircle(
cx: number,
cy: number,
radius: number,
paint: SkPaint,
): void

Draws a circle at (cx, cy) with the given radius.

drawColor(color: InputColor, blendMode?: BlendMode): void

Fills clip with the given color.

drawColorComponents(
r: number,
g: number,
b: number,
a: number,
blendMode?: BlendMode,
): void

Fills clip with the given color.

drawColorInt(color: SkColorInt, blendMode?: BlendMode): void

Fills clip with the given color.

drawDRRect(
outer: InputRRect,
inner: InputRRect,
paint: SkPaint,
): void

Draws SkRRect outer and inner using clip, SkMatrix, and SkPaint paint. outer must contain inner or the drawing is undefined.

drawImage(
img: SkImage,
left: number,
top: number,
paint?: SkPaint,
): void

Draws the given image with its top-left corner at (left, top) using the current clip, the current matrix, and optionally-provided paint.

drawImageAtCurrentFrame(
left: number,
top: number,
paint?: SkPaint,
): void

Draws the current frame of the given animated image with its top-left corner at (left, top) using the current clip, the current matrix, and optionally-provided paint.

drawImageNine(
img: SkImage,
center: InputIRect,
dest: InputRect,
paint: SkPaint,
): void

Draws the provided image stretched proportionally to fit into dst rectangle. The center rectangle divides the image into nine sections: four sides, four corners, and the center.

drawImageRect(
img: SkImage,
src: InputRect,
dest: InputRect,
paint: SkPaint,
fastSample?: boolean,
): void

Draws sub-rectangle src from provided image, scaled and translated to fill dst rectangle.

drawLine(
x0: number,
y0: number,
x1: number,
y1: number,
paint: SkPaint,
): void

Draws line segment from (x0, y0) to (x1, y1) using the current clip, current matrix, and the provided paint.

drawOval(oval: InputRect, paint: SkPaint): void

Draws an oval bounded by the given rectangle using the current clip, current matrix, and the provided paint.

drawPaint(paint: SkPaint): void

Fills clip with the given paint.

drawParagraph(
x: number,
y: number,
): void

Draws the given Paragraph at the provided coordinates. Requires the Paragraph code to be compiled in.

drawPath(path: SkPath, paint: SkPaint): void

Draws the given path using the current clip, current matrix, and the provided paint.

drawPicture(skp: SkPicture): void

Draws the given picture using the current clip, current matrix, and the provided paint.

drawPoints(): void

Draws the given points using the current clip, current matrix, and the provided paint.

See SkCanvas.h for more on the mode and its interaction with paint.

drawRect(rect: InputRect, paint: SkPaint): void

Draws the given rectangle using the current clip, current matrix, and the provided paint.

drawRect4f(
left: number,
top: number,
right: number,
bottom: number,
paint: SkPaint,
): void

Draws the given rectangle using the current clip, current matrix, and the provided paint.

drawRRect(rrect: InputRRect, paint: SkPaint): void

Draws the given rectangle with rounded corners using the current clip, current matrix, and the provided paint.

drawShadow(
path: SkPath,
zPlaneParams: Vector3,
lightPos: Vector3,
lightRadius: number,
ambientColor: InputColor,
spotColor: InputColor,
flags: number,
): void

Draw an offset spot shadow and outlining ambient shadow for the given path using a disc light. See SkShadowUtils.h for more details

drawText(
str: string | ShapedText,
x: number,
y: number,
paint: SkPaint,
font: SkFont,
): void

Draw the given text at the location (x, y) using the provided paint and font. If non-shaped text is provided, the text will be drawn as is; no line-breaking, no ligatures, etc.

drawTextBlob(
blob: SkTextBlob,
x: number,
y: number,
paint: SkPaint,
): void

Draws the given TextBlob at (x, y) using the current clip, current matrix, and the provided paint. Reminder that the fonts used to draw TextBlob are part of the blob.

drawVertices(
verts: SkVertices,
mode: BlendMode,
paint: SkPaint,
): void

Draws the given vertices (a triangle mesh) using the current clip, current matrix, and the provided paint. If paint contains an SkShader and vertices does not contain texCoords, the shader is mapped using the vertices' positions. If vertices colors are defined in vertices, and SkPaint paint contains SkShader, SkBlendMode mode combines vertices colors with SkShader.

findMarkedCTM(marker: string): Matrix4x4 | null

Returns the 4x4 matrix matching the given marker or null if there was none. See also markCTM.

getLocalToDevice(): Matrix4x4

Returns the current transform from local coordinates to the 'device', which for most purposes means pixels.

getSaveCount(): number

Returns the number of saved states, each containing: SkMatrix and clip. Equals the number of save() calls less the number of restore() calls plus one. The save count of a new canvas is one.

getTotalMatrix(): number[]

Legacy version of getLocalToDevice(), which strips away any Z information, and just returns a 3x3 version.

makeSurface(info: SkImageInfo): SkSurface | null

Creates SkSurface matching info and props, and associates it with SkCanvas. Returns null if no match found.

markCTM(marker: string): void

Record a marker (provided by caller) for the current CTM. This does not change anything about the ctm or clip, but does "name" this matrix value, so it can be referenced by custom effects (who access it by specifying the same name). See also findMarkedCTM.

readPixels(
x: number,
y: number,
w: number,
h: number,
alphaType?: AlphaType,
colorType?: ColorType,
colorSpace?: ColorSpace,
dstRowBytes?: number,
): Uint8Array

Copies the given rectangle of pixels into a new Uint8Array and returns it. If alphaType, colorType, and colorSpace are provided, those will describe the output format.

restore(): void

Removes changes to the current matrix and clip since SkCanvas state was last saved. The state is removed from the stack. Does nothing if the stack is empty.

restoreToCount(saveCount: number): void

Restores state to a previous stack value.

rotate(
rx: number,
ry: number,
): void

Rotates the current matrix by the number of degrees.

save(): number

Saves the current matrix and clip and returns current height of the stack.

saveLayer(
paint?: SkPaint,
bounds?: InputRect | null,
backdrop?: SkImageFilter | null,
flags?: SaveLayerFlag,
): number

Saves SkMatrix and clip, and allocates a SkBitmap for subsequent drawing. Calling restore() discards changes to SkMatrix and clip, and draws the SkBitmap. It returns the height of the stack. See SkCanvas.h for more.

scale(sx: number, sy: number): void

Scales the current matrix by sx on the x-axis and sy on the y-axis.

skew(sx: number, sy: number): void

Skews SkMatrix by sx on the x-axis and sy on the y-axis. A positive value of sx skews the drawing right as y-axis values increase; a positive value of sy skews the drawing down as x-axis values increase.

translate(dx: number, dy: number): void

Translates SkMatrix by dx along the x-axis and dy along the y-axis.

writePixels(
pixels: Uint8Array | number[],
srcWidth: number,
srcHeight: number,
destX: number,
destY: number,
alphaType?: AlphaType,
colorType?: ColorType,
colorSpace?: ColorSpace,
): boolean

Writes the given rectangle of pixels to the provided coordinates. The source pixels will be converted to the canvas's alphaType and colorType if they do not match.