Canvas

class Canvas

Canvas provides an interface for drawing, and how the drawing is clipped and transformed.

Canvas contains a stack of Matrix and clip values.

Canvas and Paint together provide the state to draw into Surface or BaseDevice. Each Canvas draw call transforms the geometry of the object by the concatenation of all Matrix values in the stack. The transformed geometry is clipped by the intersection of all of clip values in the stack. The Canvas draw calls use Paint to supply drawing state such as color, Typeface, text size, stroke width, Shader and so on.

To draw to a pixel-based destination, create raster surface or GPU surface. Request Canvas from Surface to obtain the interface to draw. Canvas generated by raster surface draws to memory visible to the CPU. Canvas generated by GPU surface uses Vulkan or OpenGL to draw to the GPU.

To draw to a document, obtain Canvas from SVG canvas, document PDF, or PictureRecorder. Document based Canvas and other Canvas subclasses reference BaseDevice describing the destination.

Canvas can be constructed to draw to Bitmap without first creating raster surface. This approach may be deprecated in the future.

Classes

Lattice

Canvas.Lattice divides Bitmap or Image into a rectangular grid.

PointMode

Members:

QuadAAFlags

Members:

SaveLayerFlags

Members:

SaveLayerRec

SaveLayerRec contains the state used to create the layer.

SrcRectConstraint

Members:

Methods

MakeRasterDirect

Allocates raster Canvas that will draw directly into pixels.

MakeRasterDirectN32

Allocates raster Canvas specified by inline image specification.

__init__

Overloaded function.

accessTopLayerPixels

Returns the pixel base address, and origin if the pixels can be read directly.

androidFramework_setDeviceClipRestriction

Sets the maximum clip rectangle, which can be set by clipRect(), clipRRect() and clipPath() and intersect the current clip with the specified rect.

clear

Overloaded function.

clipPath

Overloaded function.

clipRRect

Overloaded function.

clipRect

Overloaded function.

clipRegion

Replaces clip with the intersection or difference of clip and Region deviceRgn.

concat

Overloaded function.

discard

Makes Canvas contents undefined.

drawAnnotation

Associates Rect on Canvas when an annotation; a key-value pair, where the key is an UTF-8 string, and optional value is stored as Data.

drawArc

Draws arc using clip, Matrix, and Paint paint.

drawAtlas

Draws a set of sprites from atlas, using clip, Matrix, and optional Paint paint.

drawBitmap

Draws Bitmap bitmap, with its top-left corner at (left, top), using clip, Matrix, and optional Paint paint.

drawBitmapRect

Overloaded function.

drawCircle

Overloaded function.

drawColor

Overloaded function.

drawDRRect

Draws RRect outer and inner using clip, Matrix, and Paint paint.

drawIRect

Draws IRect rect using clip, Matrix, and Paint paint.

drawImage

Draws Image image, with its top-left corner at (left, top), using clip, Matrix, and optional Paint paint.

drawImageRect

Overloaded function.

drawLine

Overloaded function.

drawOval

Draws oval using clip, Matrix, and Paint paint.

drawPaint

Fills clip with Paint paint.

drawPatch

Draws a Coons patch: the interpolation of four cubics with shared corners, associating a color, and optionally a texture Point, with each corner.

drawPath

Draws Path path using clip, Matrix, and Paint paint.

drawPicture

Draws Picture picture, using clip and Matrix; transforming picture with Matrix matrix, if provided; and use Paint paint alpha, ColorFilter, ImageFilter, and BlendMode, if provided.

drawPoint

Overloaded function.

drawPoints

Draws pts using clip, Matrix and Paint paint.

drawRRect

Draws RRect rrect using clip, Matrix, and Paint paint.

drawRect

Draws Rect rect using clip, Matrix, and Paint paint.

drawRegion

Draws Region region using clip, Matrix, and Paint paint.

drawRoundRect

Draws RRect bounded by Rect rect, with corner radii (rx, ry) using clip, Matrix, and Paint paint.

drawSimpleText

Draws text, with origin at (x, y), using clip, Matrix, Font font, and Paint paint.

drawString

Draws string, with origin at (x, y), using clip, Matrix, Font font, and Paint paint.

drawTextBlob

Draws TextBlob blob at (x, y), using clip, Matrix, and Paint paint.

drawVertices

Draws Vertices vertices, a triangle mesh, using clip and Matrix.

flush

Triggers the immediate execution of all pending draw operations.

getBaseLayerSize

Gets the size of the base or root layer in global canvas coordinates.

getDeviceClipBounds

Overloaded function.

getLocalClipBounds

Overloaded function.

getLocalToDevice

getProps

Copies SurfaceProps, if Canvas is associated with raster surface or GPU surface, and returns true.

getSaveCount

Returns the number of saved states, each containing: Matrix and clip.

getSurface

Sometimes a canvas is owned by a surface.

getTotalMatrix

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

imageInfo

Returns ImageInfo for Canvas.

isClipEmpty

Returns true if clip is empty; that is, nothing will draw.

isClipRect

Returns true if clip is Rect and not empty.

makeSurface

Creates Surface matching info and props, and associates it with Canvas.

peekPixels

Returns true if Canvas has direct access to its pixels.

quickReject

Overloaded function.

readPixels

Overloaded function.

resetMatrix

Sets SkMatrix to the identity matrix.

restore

Removes changes to Matrix and clip since Canvas state was last saved.

restoreToCount

Restores state to Matrix and clip values when save(), saveLayer(), saveLayerPreserveLCDTextRequests(), or saveLayerAlpha() returned saveCount.

rotate

Overloaded function.

save

Saves Matrix and clip.

saveLayer

Overloaded function.

saveLayerAlpha

Saves Matrix and clip, and allocates a Bitmap for subsequent drawing.

scale

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

setMatrix

Overloaded function.

skew

Skews Matrix by sx on the x-axis and sy on the y-axis.

toarray

Exports a numpy.ndarray.

translate

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

writePixels

Overloaded function.

Attributes

kAll_QuadAAFlags

kBottom_QuadAAFlag

kF16ColorType

kFast_SrcRectConstraint

kInitWithPrevious_SaveLayerFlag

kLeft_QuadAAFlag

kLines_PointMode

kNone_QuadAAFlags

kPoints_PointMode

kPolygon_PointMode

kPreserveLCDText_SaveLayerFlag

kRight_QuadAAFlag

kStrict_SrcRectConstraint

kTop_QuadAAFlag

Methods

static Canvas.MakeRasterDirect(imageInfo: skia.ImageInfo, pixels: Buffer, rowBytes: int = 0, surfaceProps: SkSurfaceProps = None) skia.Canvas

Allocates raster Canvas that will draw directly into pixels.

Canvas is returned if all parameters are valid. Valid parameters include: info dimensions are zero or positive; info contains ColorType and AlphaType supported by raster surface; pixels is buffer object of sufficient length; rowBytes is zero or large enough to contain info width pixels of ColorType.

Pass zero for rowBytes to compute rowBytes from info width and size of pixel. If rowBytes is greater than zero, it must be equal to or greater than info width times bytes required for ColorType.

Pixel buffer size should be info height times computed rowBytes. Pixels are not initialized. To access pixels after drawing, call flush() or peekPixels().

Parameters:
  • info (skia.ImageInfo) – width, height, ColorType, AlphaType, ColorSpace, of raster surface; width, or height, or both, may be zero

  • pixels (Union[bytes,bytearray,memoryview]) – destination pixels buffer

  • rowBytes (int) – interval from one Surface row to the next, or zero

  • props (skia.SurfaceProps) – LCD striping orientation and setting for device independent fonts; may be None

static Canvas.MakeRasterDirectN32(width: int, height: int, pixels: Buffer, rowBytes: int = 0) skia.Canvas

Allocates raster Canvas specified by inline image specification.

Subsequent Canvas calls draw into pixels. ColorType is set to ColorType.kN32_ColorType. AlphaType is set to AlphaType.kPremul_AlphaType. To access pixels after drawing, call flush() or peekPixels().

Canvas is returned if all parameters are valid. Valid parameters include: width and height are zero or positive; pixels is buffer object with sufficient length; rowBytes is zero or large enough to contain width pixels of ColorType.kN32_ColorType.

Pass zero for rowBytes to compute rowBytes from width and size of pixel. If rowBytes is greater than zero, it must be equal to or greater than width times bytes required for ColorType.

Pixel buffer size should be height times rowBytes.

Parameters:
  • width (int) – pixel column count on raster surface created; must be zero or greater

  • height (int) – pixel row count on raster surface created; must be zero or greater

  • pixels (Union[bytes,bytearray,memoryview]) – pointer to destination pixels buffer; buffer size should be height times rowBytes

  • rowBytes (int) – interval from one Surface row to the next, or zero

Canvas.__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: skia.Canvas) -> None

    Creates an empty Canvas with no backing device or pixels, with a width and height of zero.

  2. __init__(self: skia.Canvas, array: numpy.ndarray, colorType: skia.ColorType = skia.ColorType.kN32_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnpremul_AlphaType, colorSpace: skia.ColorSpace = None, surfaceProps: SkSurfaceProps = None) -> None

    Creates raster Canvas backed by numpy array.

    Subsequent Canvas calls draw into pixels. To access pixels after drawing, call flush() or peekPixels().

    array:

    numpy ndarray of shape=(height, width, channels) and appropriate dtype. Must have non-zero width and height, and the valid number of channels for the specified color type.

    colorType:

    color type of the array

    alphaType:

    alpha type of the array

    colorSpace:

    range of colors; may be nullptr

  3. __init__(self: skia.Canvas, width: int, height: int, props: SkSurfaceProps = None) -> None

    Creates Canvas of the specified dimensions without a Surface.

    Used by subclasses with custom implementations for draw member functions.

    If props equals None, SurfaceProps are created with SurfaceProps.InitType settings, which choose the pixel striping direction and order. Since a platform may dynamically change its direction when the device is rotated, and since a platform may have multiple monitors with different characteristics, it is best not to rely on this legacy behavior.

    width:

    zero or greater

    height:

    zero or greater

    props:

    LCD striping orientation and setting for device independent fonts; may be None

  4. __init__(self: skia.Canvas, bitmap: skia.Bitmap) -> None

    Constructs a canvas that draws into bitmap.

    Sets SurfaceProps.kLegacyFontHost in constructed Surface.

    Bitmap is copied so that subsequently editing bitmap will not affect constructed Canvas.

    May be deprecated in the future.

    bitmap:

    width, height, ColorType, AlphaType, and pixel storage of raster surface

  5. __init__(self: skia.Canvas, bitmap: skia.Bitmap, props: SkSurfaceProps) -> None

    Constructs a canvas that draws into bitmap.

    Use props to match the device characteristics, like LCD striping.

    bitmap is copied so that subsequently editing bitmap will not affect constructed Canvas.

    bitmap:

    width, height, ColorType, AlphaType, and pixel storage of raster surface

    props:

    order and orientation of RGB striping; and whether to use device independent fonts

Canvas.accessTopLayerPixels(self: skia.Canvas, origin: skia.IPoint = None) object

Returns the pixel base address, and origin if the pixels can be read directly.

The returned address is only valid while Canvas is in scope and unchanged. Any Canvas call or Surface call may invalidate the returned address and other returned values.

If pixels are inaccessible, returns None.

Parameters:

origin – storage for Canvas top layer origin, its top-left corner; may be nullptr

Returns:

address of pixels, or nullptr if inaccessible

Canvas.androidFramework_setDeviceClipRestriction(self: skia.Canvas, rect: skia.IRect) None

Sets the maximum clip rectangle, which can be set by clipRect(), clipRRect() and clipPath() and intersect the current clip with the specified rect.

The maximum clip affects only future clipping operations; it is not retroactive. The clip restriction is not recorded in pictures.

Pass an empty rect to disable maximum clip. This private API is for use by Android framework only.

Parameters:

rect (skia.IRect) – maximum allowed clip in device coordinates

Canvas.clear(*args, **kwargs)

Overloaded function.

  1. clear(self: skia.Canvas, color: int) -> None

    Fills clip with color color using BlendMode.kSrc.

    This has the effect of replacing all pixels contained by clip with color.

    param int color:

    unpremultiplied ARGB

  2. clear(self: skia.Canvas, color: skia.Color4f) -> None

    Fills clip with color color using BlendMode.kSrc.

    This has the effect of replacing all pixels contained by clip with color.

    param color:

    Color4f representing unpremultiplied color.

Canvas.clipPath(*args, **kwargs)

Overloaded function.

  1. clipPath(self: skia.Canvas, path: skia.Path, op: skia.ClipOp, doAntiAlias: bool) -> None

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

    Path.FillType determines if path describes the area inside or outside its contours; and if path contour overlaps itself or another path contour, whether the overlaps form part of the area. path is transformed by Matrix before it is combined with clip.

    path:

    Path to combine with clip

    op:

    ClipOp to apply to clip

    doAntiAlias:

    true if clip is to be anti-aliased

  2. clipPath(self: skia.Canvas, path: skia.Path, op: skia.ClipOp) -> None

    Replaces clip with the intersection or difference of clip and path.

    Resulting clip is aliased; pixels are fully contained by the clip. Path.FillType determines if path describes the area inside or outside its contours; and if path contour overlaps itself or another path contour, whether the overlaps form part of the area. path is transformed by Matrix before it is combined with clip.

    path:

    Path to combine with clip

    op:

    ClipOp to apply to clip

  3. clipPath(self: skia.Canvas, path: skia.Path, doAntiAlias: bool = False) -> None

    Replaces clip with the intersection of clip and path.

    Resulting clip is aliased; pixels are fully contained by the clip. Path.FillType determines if path describes the area inside or outside its contours; and if path contour overlaps itself or another path contour, whether the overlaps form part of the area. path is transformed by Matrix before it is combined with clip.

    path:

    Path to combine with clip

    doAntiAlias:

    true if clip is to be anti-aliased

Canvas.clipRRect(*args, **kwargs)

Overloaded function.

  1. clipRRect(self: skia.Canvas, rrect: skia.RRect, op: skia.ClipOp, doAntiAlias: bool) -> None

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

    rrect is transformed by Matrix before it is combined with clip.

    rrect:

    RRect to combine with clip

    op:

    ClipOp to apply to clip

    doAntiAlias:

    true if clip is to be anti-aliased

  2. clipRRect(self: skia.Canvas, rrect: skia.RRect, op: skia.ClipOp) -> None

    Replaces clip with the intersection or difference of clip and rrect.

    Resulting clip is aliased; pixels are fully contained by the clip. rrect is transformed by Matrix before it is combined with clip.

    rrect:

    RRect to combine with clip

    op:

    ClipOp to apply to clip

  3. clipRRect(self: skia.Canvas, rrect: skia.RRect, doAntiAlias: bool = False) -> None

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

    rrect is transformed by Matrix before it is combined with clip.

    rrect:

    RRect to combine with clip

    doAntiAlias:

    true if clip is to be anti-aliased

Canvas.clipRect(*args, **kwargs)

Overloaded function.

  1. clipRect(self: skia.Canvas, rect: skia.Rect, op: skia.ClipOp, doAntiAlias: bool) -> None

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

    rect is transformed by Matrix before it is combined with clip.

    rect:

    Rect to combine with clip

    op:

    ClipOp to apply to clip

    doAntiAlias:

    true if clip is to be anti-aliased

  2. clipRect(self: skia.Canvas, rect: skia.Rect, op: skia.ClipOp) -> None

    Replaces clip with the intersection or difference of clip and rect.

    Resulting clip is aliased; pixels are fully contained by the clip. rect is transformed by Matrix before it is combined with clip.

    rect:

    Rect to combine with clip

    op:

    ClipOp to apply to clip

  3. clipRect(self: skia.Canvas, rect: skia.Rect, doAntiAlias: bool = False) -> None

    Replaces clip with the intersection of clip and rect.

    Resulting clip is aliased; pixels are fully contained by the clip. rect is transformed by Matrix before it is combined with clip.

    rect:

    Rect to combine with clip

    doAntiAlias:

    true if clip is to be anti-aliased

Canvas.clipRegion(self: skia.Canvas, deviceRgn: skia.Region, op: skia.ClipOp = skia.ClipOp.kIntersect) None

Replaces clip with the intersection or difference of clip and Region deviceRgn.

Resulting clip is aliased; pixels are fully contained by the clip. deviceRgn is unaffected by Matrix.

Parameters:
Canvas.concat(*args, **kwargs)

Overloaded function.

  1. concat(self: skia.Canvas, matrix: skia.Matrix) -> None

    Replaces Matrix with matrix premultiplied with existing Matrix.

    This has the effect of transforming the drawn geometry by matrix, before transforming the result with existing Matrix.

    matrix:

    matrix to premultiply with existing Matrix

  2. concat(self: skia.Canvas, m44: skia.M44) -> None

Canvas.discard(self: skia.Canvas) None

Makes Canvas contents undefined.

Subsequent calls that read Canvas pixels, such as drawing with BlendMode, return undefined results. discard() does not change clip or Matrix.

discard() may do nothing, depending on the implementation of Surface or BaseDevice that created Canvas.

discard() allows optimized performance on subsequent draws by removing cached data associated with Surface or BaseDevice. It is not necessary to call discard() once done with SkCanvas; any cached data is deleted when owning Surface or BaseDevice is deleted.

Canvas.drawAnnotation(self: skia.Canvas, rect: skia.Rect, key: str, value: skia.Data) None

Associates Rect on Canvas when an annotation; a key-value pair, where the key is an UTF-8 string, and optional value is stored as Data.

Only some canvas implementations, such as recording to Picture, or drawing to document PDF, use annotations.

Parameters:
  • rect (skia.Rect) – Rect extent of canvas to annotate

  • key (str) – string used for lookup

  • value (skia.Data) – data holding value stored in annotation

Canvas.drawArc(self: skia.Canvas, oval: skia.Rect, startAngle: float, sweepAngle: float, useCenter: bool, paint: skia.Paint) None

Draws arc using clip, Matrix, and Paint paint.

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

startAngle of zero places start point at the right middle edge of oval. A positive sweepAngle places arc end point clockwise from start point; a negative sweepAngle places arc end point counterclockwise from start point. sweepAngle may exceed 360 degrees, a full circle. If useCenter is true, draw a wedge that includes lines from oval center to arc end points. If useCenter is false, draw arc between end points.

If Rect oval is empty or sweepAngle is zero, nothing is drawn.

Parameters:
  • oval (skia.Rect) – Rect bounds of oval containing arc to draw

  • startAngle (float) – angle in degrees where arc begins

  • sweepAngle (float) – sweep angle in degrees; positive is clockwise

  • useCenter (bool) – if true, include the center of the oval

  • paint (skia.Paint) – Paint stroke or fill, blend, color, and so on, used to draw

Canvas.drawAtlas(self: skia.Canvas, atlas: skia.Image, xform: list[skia.RSXform], tex: list[skia.Rect], colors: list[int], mode: skia.BlendMode, options: skia.SamplingOptions = skia.SamplingOptions(), cullRect: skia.Rect = None, paint: skia.Paint = None) None

Draws a set of sprites from atlas, using clip, Matrix, and optional Paint paint.

paint uses anti-alias, alpha, ColorFilter, ImageFilter, and BlendMode to draw, if present. For each entry in the array, Rect tex locates sprite in atlas, and RSXform xform transforms it into destination space.

xform, text, and colors if present, must contain count entries. Optional colors are applied for each sprite using BlendMode mode, treating sprite as source and colors as destination. Optional cullRect is a conservative bounds of all transformed sprites. If cullRect is outside of clip, canvas can skip drawing.

If atlas is None, this draws nothing.

Parameters:
Canvas.drawBitmap(self: skia.Canvas, bitmap: skia.Bitmap, left: float, top: float, paint: skia.Paint = None) None

Draws Bitmap bitmap, with its top-left corner at (left, top), using clip, Matrix, and optional Paint paint.

If Paint paint is not nullptr, apply ColorFilter, alpha, ImageFilter, BlendMode, and DrawLooper. If bitmap is ColorType.kAlpha_8_ColorType, apply Shader. If paint contains MaskFilter, generate mask from bitmap bounds.

If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, just as Shader made from Shader.MakeBitmapShader() with TileMode.kClamp set replicates the bitmap edge color when it samples outside of its bounds.

Parameters:
Canvas.drawBitmapRect(*args, **kwargs)

Overloaded function.

  1. drawBitmapRect(self: skia.Canvas, bitmap: skia.Bitmap, src: skia.Rect, dst: skia.Rect, paint: skia.Paint = None, constraint: skia.Canvas.SrcRectConstraint = skia.Canvas.SrcRectConstraint.kStrict_SrcRectConstraint) -> None

    Draws Rect src of Bitmap bitmap, scaled and translated to fill Rect dst.

    Additionally transform draw using clip, Matrix, and optional Paint paint.

    If Paint paint is supplied, apply ColorFilter, alpha, ImageFilter, BlendMode, and DrawLooper. If bitmap is ColorType.kAlpha_8_ColorType, apply Shader. If paint contains MaskFilter, generate mask from bitmap bounds.

    If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, just as Shader made from Shader.MakeBitmapShader() with TileMode.kClamp set replicates the bitmap edge color when it samples outside of its bounds.

    constraint set to kStrict_SrcRectConstraint limits Paint FilterQuality to sample within src; set to kFast_SrcRectConstraint allows sampling outside to improve performance.

    bitmap:

    Bitmap containing pixels, dimensions, and format

    src:

    source Rect of image to draw from

    dst:

    destination Rect of image to draw to

    paint:

    Paint containing BlendMode, ColorFilter, ImageFilter, and so on; or nullptr

    constraint:

    filter strictly within src or draw faster

  2. drawBitmapRect(self: skia.Canvas, bitmap: skia.Bitmap, isrc: skia.Rect, dst: skia.Rect, paint: skia.Paint = None, constraint: skia.Canvas.SrcRectConstraint = skia.Canvas.SrcRectConstraint.kStrict_SrcRectConstraint) -> None

    Draws IRect isrc of Bitmap bitmap, scaled and translated to fill Rect dst.

    Additionally transform draw using clip, Matrix, and optional Paint paint.

    If Paint paint is supplied, apply ColorFilter, alpha, ImageFilter, BlendMode, and DrawLooper. If bitmap is ColorType.kAlpha_8_ColorType, apply Shader. If paint contains MaskFilter, generate mask from bitmap bounds.

    If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, just as Shader made from Shader.MakeBitmapShader() with TileMode.kClamp set replicates the bitmap edge color when it samples outside of its bounds.

    constraint set to kStrict_SrcRectConstraint limits Paint FilterQuality to sample within isrc; set to kFast_SrcRectConstraint allows sampling outside to improve performance.

    bitmap:

    Bitmap containing pixels, dimensions, and format

    isrc:

    source IRect of image to draw from

    dst:

    destination Rect of image to draw to

    paint:

    Paint containing BlendMode, ColorFilter, ImageFilter, and so on; or nullptr

    constraint:

    filter strictly within isrc or draw faster

  3. drawBitmapRect(self: skia.Canvas, bitmap: skia.Bitmap, dst: skia.Rect, paint: skia.Paint = None, constraint: skia.Canvas.SrcRectConstraint = skia.Canvas.SrcRectConstraint.kStrict_SrcRectConstraint) -> None

    Draws Bitmap bitmap, scaled and translated to fill Rect dst.

    bitmap bounds is on integer pixel boundaries; dst may include fractional boundaries. Additionally transform draw using clip, Matrix, and optional Paint paint.

    If Paint paint is supplied, apply ColorFilter, alpha, ImageFilter, BlendMode, and DrawLooper. If bitmap is ColorType.kAlpha_8_ColorType, apply Shader. If paint contains MaskFilter, generate mask from bitmap bounds.

    If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, just as Shader made from Shader.MakeBitmapShader() with TileMode.kClamp set replicates the bitmap edge color when it samples outside of its bounds.

    constraint set to kStrict_SrcRectConstraint limits Paint FilterQuality to sample within isrc; set to kFast_SrcRectConstraint allows sampling outside to improve performance.

    bitmap:

    Bitmap containing pixels, dimensions, and format

    dst:

    destination Rect of image to draw to

    paint:

    Paint containing BlendMode, ColorFilter, ImageFilter, and so on; or nullptr

    constraint:

    filter strictly within bitmap or draw faster

Canvas.drawCircle(*args, **kwargs)

Overloaded function.

  1. drawCircle(self: skia.Canvas, cx: float, cy: float, radius: float, paint: skia.Paint) -> None

    Draws circle at (cx, cy) with radius using clip, Matrix, and Paint paint.

    If radius is zero or less, nothing is drawn. In paint: Paint.Style determines if circle is stroked or filled; if stroked, Paint stroke width describes the line thickness.

    cx:

    circle center on the x-axis

    cy:

    circle center on the y-axis

    radius:

    half the diameter of circle

    paint:

    Paint stroke or fill, blend, color, and so on, used to draw

  2. drawCircle(self: skia.Canvas, center: skia.Point, radius: float, paint: skia.Paint) -> None

    Draws circle at center with radius using clip, Matrix, and Paint paint.

    If radius is zero or less, nothing is drawn. In paint: Paint.Style determines if circle is stroked or filled; if stroked, Paint stroke width describes the line thickness.

    center:

    circle center

    radius:

    half the diameter of circle

    paint:

    Paint stroke or fill, blend, color, and so on, used to draw

Canvas.drawColor(*args, **kwargs)

Overloaded function.

  1. drawColor(self: skia.Canvas, color: int, mode: skia.BlendMode = skia.BlendMode.kSrcOver) -> None

    Fills clip with color color.

    mode determines how ARGB is combined with destination.

    param int color:

    unpremultiplied ARGB

    param skia.BlendMode mode:

    BlendMode used to combine source color and destination

  2. drawColor(self: skia.Canvas, color: skia.Color4f, mode: skia.BlendMode = skia.BlendMode.kSrcOver) -> None

    Fills clip with color color.

    mode determines how ARGB is combined with destination.

    param color:

    Color4f representing unpremultiplied color.

    param skia.BlendMode mode:

    BlendMode used to combine source color and destination

Canvas.drawDRRect(self: skia.Canvas, outer: skia.RRect, inner: skia.RRect, paint: skia.Paint) None

Draws RRect outer and inner using clip, Matrix, and Paint paint.

outer must contain inner or the drawing is undefined. In paint: Paint.Style determines if RRect is stroked or filled; if stroked, Paint stroke width describes the line thickness, and Paint.Join draws the corners rounded or square.

GPU-backed platforms optimize drawing when both outer and inner are concave and outer contains inner. These platforms may not be able to draw Path built with identical data as fast.

Parameters:
Canvas.drawIRect(self: skia.Canvas, rect: skia.IRect, paint: skia.Paint) None

Draws IRect rect using clip, Matrix, and Paint paint.

In paint: Paint.Style determines if rectangle is stroked or filled; if stroked, Paint stroke width describes the line thickness, and Paint.Join draws the corners rounded or square.

Parameters:
  • rect (skia.IRect) – rectangle to draw

  • paint (skia.Paint) – stroke or fill, blend, color, and so on, used to draw

Canvas.drawImage(self: skia.Canvas, image: skia.Image, left: float, top: float, options: skia.SamplingOptions = skia.SamplingOptions(), paint: skia.Paint = None) None

Draws Image image, with its top-left corner at (left, top), using clip, Matrix, and optional Paint paint.

This is equivalent to drawImageRect() using a dst rect at (x,y) with the same width and height of the image.

Image:

uncompressed rectangular map of pixels

Left:

left side of image

Top:

top side of image

Paint:

Paint containing BlendMode, ColorFilter, ImageFilter, and so on; or nullptr

Canvas.drawImageRect(*args, **kwargs)

Overloaded function.

  1. drawImageRect(self: skia.Canvas, image: skia.Image, src: skia.Rect, dst: skia.Rect, options: skia.SamplingOptions = skia.SamplingOptions(), paint: skia.Paint = None, constraint: skia.Canvas.SrcRectConstraint = skia.Canvas.SrcRectConstraint.kStrict_SrcRectConstraint) -> None

    Draws Rect src of Image image, scaled and translated to fill Rect dst.

    Additionally transform draw using clip, Matrix, and optional Paint paint.

    If Paint paint is supplied, apply ColorFilter, alpha, ImageFilter, BlendMode, and DrawLooper. If image is ColorType.kAlpha_8_ColorType, apply Shader. If paint contains MaskFilter, generate mask from image bounds.

    If generated mask extends beyond image bounds, replicate image edge colors, just as Shader made from Image.makeShader() with TileMode.kClamp set replicates the image edge color when it samples outside of its bounds.

    When using a shader or shader mask filter, its coordinate system is based on the current CTM, so will reflect the dst rect geometry and is equivalent to drawRect(dst). The src rect is only used to access the provided image.

    constraint set to kStrict_SrcRectConstraint limits Paint FilterQuality to sample within src; set to kFast_SrcRectConstraint allows sampling outside to improve performance.

    image:

    Image containing pixels, dimensions, and format

    src:

    source Rect of image to draw from

    dst:

    destination Rect of image to draw to

    paint:

    Paint containing BlendMode, ColorFilter, ImageFilter, and so on; or nullptr

    constraint:

    filter strictly within src or draw faster

  2. drawImageRect(self: skia.Canvas, image: skia.Image, isrc: skia.Rect, dst: skia.Rect, options: skia.SamplingOptions = skia.SamplingOptions(), paint: skia.Paint = None, constraint: skia.Canvas.SrcRectConstraint = skia.Canvas.SrcRectConstraint.kStrict_SrcRectConstraint) -> None

    Draws IRect isrc of Image image, scaled and translated to fill Rect dst.

    Additionally transform draw using clip, Matrix, and optional Paint paint.

    If Paint paint is supplied, apply ColorFilter, alpha, ImageFilter, BlendMode, and DrawLooper. If image is ColorType.kAlpha_8_ColorType, apply Shader. If paint contains MaskFilter, generate mask from image bounds.

    If generated mask extends beyond image bounds, replicate image edge colors, just as Shader made from Image.makeShader() with TileMode.kClamp set replicates the image edge color when it samples outside of its bounds.

    When using a shader or shader mask filter, its coordinate system is based on the current CTM, so will reflect the dst rect geometry and is equivalent to drawRect(dst). The isrc rect is only used to access the provided image.

    constraint set to kStrict_SrcRectConstraint limits Paint FilterQuality to sample within isrc; set to kFast_SrcRectConstraint allows sampling outside to improve performance.

    image:

    Image containing pixels, dimensions, and format

    isrc:

    source IRect of image to draw from

    dst:

    destination Rect of image to draw to

    paint:

    Paint containing BlendMode, ColorFilter, ImageFilter, and so on; or nullptr

    constraint:

    filter strictly within isrc or draw faster

  3. drawImageRect(self: skia.Canvas, image: skia.Image, dst: skia.Rect, options: skia.SamplingOptions = skia.SamplingOptions(), paint: skia.Paint = None) -> None

    Draws Image image, scaled and translated to fill Rect dst, using clip, Matrix, and optional Paint paint.

    If Paint paint is supplied, apply ColorFilter, alpha, ImageFilter, BlendMode, and DrawLooper. If image is ColorType.kAlpha_8_ColorType, apply Shader. If paint contains MaskFilter, generate mask from image bounds.

    If generated mask extends beyond image bounds, replicate image edge colors, just as Shader made from Image.makeShader() with TileMode.kClamp set replicates the image edge color when it samples outside of its bounds.

    When using a shader or shader mask filter, its coordinate system is based on the current CTM, so will reflect the dst rect geometry and is equivalent to drawRect(dst).

    image:

    Image containing pixels, dimensions, and format

    dst:

    destination Rect of image to draw to

    paint:

    Paint containing BlendMode, ColorFilter, ImageFilter, and so on; or nullptr

    constraint:

    filter strictly within src or draw faster

Canvas.drawLine(*args, **kwargs)

Overloaded function.

  1. drawLine(self: skia.Canvas, x0: float, y0: float, x1: float, y1: float, paint: skia.Paint) -> None

    Draws line segment from (x0, y0) to (x1, y1) using clip, Matrix, and Paint paint.

    In paint: Paint stroke width describes the line thickness; Paint.Cap draws the end rounded or square; Paint.Style is ignored, as if were set to Paint.Style.kStroke.

    x0:

    start of line segment on x-axis

    y0:

    start of line segment on y-axis

    x1:

    end of line segment on x-axis

    y1:

    end of line segment on y-axis

    paint:

    stroke, blend, color, and so on, used to draw

  2. drawLine(self: skia.Canvas, p0: skia.Point, p1: skia.Point, paint: skia.Paint) -> None

    Draws line segment from p0 to p1 using clip, Matrix, and Paint paint.

    In paint: Paint stroke width describes the line thickness; Paint.Cap draws the end rounded or square; Paint.Style is ignored, as if were set to Paint.Style.kStroke.

    p0:

    start of line segment

    p1:

    end of line segment

    paint:

    stroke, blend, color, and so on, used to draw

Canvas.drawOval(self: skia.Canvas, oval: skia.Rect, paint: skia.Paint) None

Draws oval using clip, Matrix, and Paint paint.

In paint: Paint.Style determines if oval is stroked or filled; if stroked, Paint stroke width describes the line thickness, and Paint.Join draws the corners rounded or square.

Parameters:
  • oval (skia.Rect) – oval to draw

  • paint (skia.Paint) – stroke or fill, blend, color, and so on, used to draw

Canvas.drawPaint(self: skia.Canvas, paint: skia.Paint) None

Fills clip with Paint paint.

Paint components MaskFilter, Shader, ColorFilter, ImageFilter, and BlendMode affect drawing; PathEffect in paint is ignored.

Parameters:

paint (skia.Paint) – graphics state used to fill Canvas

Canvas.drawPatch(self: skia.Canvas, cubics: list[skia.Point], colors: list[int], texCoords: list[skia.Point], mode: skia.BlendMode, paint: skia.Paint) None

Draws a Coons patch: the interpolation of four cubics with shared corners, associating a color, and optionally a texture Point, with each corner.

Coons patch uses clip and Matrix, paint Shader, ColorFilter, alpha, ImageFilter, and BlendMode. If Shader is provided it is treated as Coons patch texture; BlendMode mode combines color colors and Shader if both are provided.

Point array cubics specifies four Path cubic starting at the top-left corner, in clockwise order, sharing every fourth point. The last Path cubic ends at the first point.

Color array color associates colors with corners in top-left, top-right, bottom-right, bottom-left order.

If paint contains Shader, Point array texCoords maps Shader as texture to corners in top-left, top-right, bottom-right, bottom-left order. If texCoords is nullptr, Shader is mapped using positions (derived from cubics).

Parameters:
  • cubics (List[skia.Point]) – Path cubic array, sharing common points (length 12)

  • colors (List[int]) – color array, one for each corner (length 4)

  • texCoords (List[skia.Point]) – Point array of texture coordinates, mapping Shader to corners (length 4); may be an empty list

  • modeBlendMode for colors, and for Shader if paint has one

  • paintShader, ColorFilter, BlendMode, used to draw

Canvas.drawPath(self: skia.Canvas, path: skia.Path, paint: skia.Paint) None

Draws Path path using clip, Matrix, and Paint paint.

Path contains an array of path contour, each of which may be open or closed.

In paint: Paint.Style determines if RRect is stroked or filled: if filled, Path.FillType determines whether path contour describes inside or outside of fill; if stroked, Paint stroke width describes the line thickness, Paint::Cap describes line ends, and Paint.Join describes how corners are drawn.

Parameters:
Canvas.drawPicture(self: skia.Canvas, picture: skia.Picture, matrix: skia.Matrix = None, paint: skia.Paint = None) None

Draws Picture picture, using clip and Matrix; transforming picture with Matrix matrix, if provided; and use Paint paint alpha, ColorFilter, ImageFilter, and BlendMode, if provided.

Picture records a series of draw commands for later playback.

matrix transformation is equivalent to: save(), concat(), drawPicture(), restore(). paint use is equivalent to: saveLayer(), drawPicture(), restore().

Parameters:
  • picture (skia.Picture) – recorded drawing commands to play

  • matrix (skia.Matrix) – Matrix to rotate, scale, translate, and so on; may be None

  • paint (skia.Paint) – Paint to apply transparency, filtering, and so on; may be None

Canvas.drawPoint(*args, **kwargs)

Overloaded function.

  1. drawPoint(self: skia.Canvas, x: float, y: float, paint: skia.Paint) -> None

    Draws point at (x, y) using clip, Matrix and Paint paint.

    The shape of point drawn depends on paint Paint.Cap. If paint is set to Paint.Cap.kRound, draw a circle of diameter Paint stroke width. If paint is set to Paint.Cap.kSquare or Paint.Cap.Butt, draw a square of width and height Paint stroke width. Paint.Style is ignored, as if were set to Paint.Style.kStroke.

    x:

    left edge of circle or square

    y:

    top edge of circle or square

    paint:

    stroke, blend, color, and so on, used to draw

  2. drawPoint(self: skia.Canvas, p: skia.Point, paint: skia.Paint) -> None

    Draws point p using clip, Matrix and Paint paint.

    The shape of point drawn depends on paint Paint.Cap. If paint is set to Paint.Cap.kRound, draw a circle of diameter Paint stroke width. If paint is set to Paint.Cap.kSquare or Paint.Cap.Butt, draw a square of width and height Paint stroke width. Paint.Style is ignored, as if were set to Paint.Style.kStroke.

    p:

    top-left edge of circle or square

    paint:

    stroke, blend, color, and so on, used to draw

Canvas.drawPoints(self: skia.Canvas, mode: skia.Canvas.PointMode, pts: list[skia.Point], paint: skia.Paint) None

Draws pts using clip, Matrix and Paint paint.

mode may be one of: kPoints, kLines, or kPolygon.

If mode is kPoints, the shape of point drawn depends on paint Paint.Cap. If paint is set to Paint.kRound, each point draws a circle of diameter Paint stroke width. If paint is set to Paint.kSquare or Paint.kButt, each point draws a square of width and height Paint stroke width.

If mode is kLines, each pair of points draws a line segment. One line is drawn for every two points; each point is used once. If count is odd, the final point is ignored.

If mode is kPolygon, each adjacent pair of points draws a line segment. count minus one lines are drawn; the first and last point are used once.

Each line segment respects paint Paint.Cap and Paint stroke width. Paint.Style is ignored, as if were set to Paint.kStroke.

Always draws each element one at a time; is not affected by Paint.Join, and unlike drawPath(), does not create a mask from all points and lines before drawing.

Parameters:
Canvas.drawRRect(self: skia.Canvas, rrect: skia.RRect, paint: skia.Paint) None

Draws RRect rrect using clip, Matrix, and Paint paint.

In paint: Paint.Style determines if rrect is stroked or filled; if stroked, Paint stroke width describes the line thickness, and Paint.Join draws the corners rounded or square.

rrect may represent a rectangle, circle, oval, uniformly rounded rectangle, or may have any combination of positive non-square radii for the four corners.

Parameters:
  • rrect (skia.RRect) – rrect to draw

  • paint (skia.Paint) – stroke or fill, blend, color, and so on, used to draw

Canvas.drawRect(self: skia.Canvas, rect: skia.Rect, paint: skia.Paint) None

Draws Rect rect using clip, Matrix, and Paint paint.

In paint: Paint.Style determines if rectangle is stroked or filled; if stroked, Paint stroke width describes the line thickness, and Paint.Join draws the corners rounded or square.

Parameters:
  • rect (skia.Rect) – rectangle to draw

  • paint (skia.Paint) – stroke or fill, blend, color, and so on, used to draw

Canvas.drawRegion(self: skia.Canvas, region: skia.Region, paint: skia.Paint) None

Draws Region region using clip, Matrix, and Paint paint.

In paint: Paint.Style determines if rectangle is stroked or filled; if stroked, Paint stroke width describes the line thickness, and Paint.Join draws the corners rounded or square.

Parameters:
  • region (skia.Region) – region to draw

  • paint (skia.Paint) – stroke or fill, blend, color, and so on, used to draw

Canvas.drawRoundRect(self: skia.Canvas, rect: skia.Rect, rx: float, ry: float, paint: skia.Paint) None

Draws RRect bounded by Rect rect, with corner radii (rx, ry) using clip, Matrix, and Paint paint.

In paint: Paint.Style determines if RRect is stroked or filled; if stroked, Paint stroke width describes the line thickness. If rx or ry are less than zero, they are treated as if they are zero. If rx plus ry exceeds rect width or rect height, radii are scaled down to fit. If rx and ry are zero, RRect is drawn as Rect and if stroked is affected by Paint.Join.

Parameters:
  • rect (skia.Rect) – SkRect bounds of SkRRect to draw

  • rx (float) – axis length on x-axis of oval describing rounded corners

  • ry (float) – axis length on y-axis of oval describing rounded corners

  • paint (skia.Paint) – stroke, blend, color, and so on, used to draw

Canvas.drawSimpleText(self: skia.Canvas, text: str, x: float, y: float, font: skia.Font, paint: skia.Paint) None

Draws text, with origin at (x, y), using clip, Matrix, Font font, and Paint paint.

This function uses the default character-to-glyph mapping from the Typeface in font. It does not perform typeface fallback for characters not found in the Typeface. It does not perform kerning or other complex shaping; glyphs are positioned based on their default advances.

Text size is affected by Matrix and Font text size. Default text size is 12 point.

All elements of paint: PathEffect, MaskFilter, Shader, ColorFilter, ImageFilter, and DrawLooper; apply to text. By default, draws filled black glyphs.

Parameters:
  • text (str) – character code points or glyphs drawn

  • x (float) – start of text on x-axis

  • y (float) – start of text on y-axis

  • font (skia.Font) – typeface, text size and so, used to describe the text

  • paint (skia.Paint) – blend, color, and so on, used to draw

Canvas.drawString(self: skia.Canvas, text: str, x: float, y: float, font: skia.Font, paint: skia.Paint) None

Draws string, with origin at (x, y), using clip, Matrix, Font font, and Paint paint.

This function uses the default character-to-glyph mapping from the Typeface in font. It does not perform typeface fallback for characters not found in the Typeface. It does not perform kerning; glyphs are positioned based on their default advances.

String text is encoded as UTF-8.

Text size is affected by Matrix and font text size. Default text size is 12 point.

All elements of paint: PathEffect, MaskFilter, Shader, ColorFilter, ImageFilter, and DrawLooper; apply to text. By default, draws filled black glyphs.

Parameters:
  • text (str) – character code points drawn, ending with a char value of zero

  • x (float) – start of string on x-axis

  • y (float) – start of string on y-axis

  • font (skia.Font) – typeface, text size and so, used to describe the text

  • paint (skia.Paint) – blend, color, and so on, used to draw

Canvas.drawTextBlob(self: skia.Canvas, blob: skia.TextBlob, x: float, y: float, paint: skia.Paint) None

Draws TextBlob blob at (x, y), using clip, Matrix, and Paint paint.

blob contains glyphs, their positions, and paint attributes specific to text: Typeface, Paint text size, Paint text scale x, Paint text skew x, Paint::Align, Paint::Hinting, anti-alias, Paint fake bold, Paint font embedded bitmaps, Paint full hinting spacing, LCD text, Paint linear text, and Paint subpixel text.

TextEncoding must be set to TextEncoding.kGlyphID.

Elements of paint: anti-alias, BlendMode, color including alpha, ColorFilter, Paint dither, DrawLooper, MaskFilter, PathEffect, Shader, and Paint.Style; apply to blob. If Paint contains Paint.kStroke: Paint iter limit, Paint.Cap, Paint.Join, and Paint stroke width; apply to Path created from blob.

Parameters:
  • blob (skia.TextBlob) – glyphs, positions, and their paints’ text size, typeface, and so on

  • x (float) – horizontal offset applied to blob

  • y (float) – vertical offset applied to blob

  • paint (skia.Paint) – blend, color, stroking, and so on, used to draw

Canvas.drawVertices(self: skia.Canvas, vertices: skia.Vertices, paint: skia.Paint, mode: skia.BlendMode = skia.BlendMode.kModulate) None

Draws Vertices vertices, a triangle mesh, using clip and Matrix. If paint contains an Shader and vertices does not contain texCoords, the shader is mapped using the vertices’ positions.

If vvertices colors are defined in vertices, and Paint paint contains Shader, BlendMode mode combines vertices colors with Shader.

Parameters:
Canvas.flush(self: skia.Canvas) None

Triggers the immediate execution of all pending draw operations.

If Canvas is associated with GPU surface, resolves all pending GPU operations. If Canvas is associated with raster surface, has no effect; raster draw operations are never deferred.

Canvas.getBaseLayerSize(self: skia.Canvas) skia.ISize

Gets the size of the base or root layer in global canvas coordinates.

The origin of the base layer is always (0,0). The area available for drawing may be smaller (due to clipping or saveLayer).

Returns:

integral width and height of base layer

Return type:

skia.ISize

Canvas.getDeviceClipBounds(*args, **kwargs)

Overloaded function.

  1. getDeviceClipBounds(self: skia.Canvas) -> skia.IRect

    Returns IRect bounds of clip, unaffected by Matrix.

    If clip is empty, return Rect.MakeEmpty(), where all Rect sides equal zero.

    Unlike getLocalClipBounds(), returned IRect is not outset.

    return:

    bounds of clip in BaseDevice coordinates

  2. getDeviceClipBounds(self: skia.Canvas, bounds: skia.IRect) -> bool

    Returns IRect bounds of clip, unaffected by Matrix.

    If clip is empty, return false, and set bounds to Rect.MakeEmpty(), where all Rect sides equal zero.

    Unlike getLocalClipBounds(), returned IRect is not outset.

    param skia.Rect bounds:

    Rect of clip in device coordinates

    return:

    bounds of clip in BaseDevice coordinates

Canvas.getLocalClipBounds(*args, **kwargs)

Overloaded function.

  1. getLocalClipBounds(self: skia.Canvas) -> skia.Rect

    Returns bounds of clip, transformed by inverse of Matrix.

    If clip is empty, return Rect.MakeEmpty(), where all Rect sides equal zero.

    Rect returned is outset by one to account for partial pixel coverage if clip is anti-aliased.

    return:

    bounds of clip in local coordinates

  2. getLocalClipBounds(self: skia.Canvas, bounds: skia.Rect) -> bool

    Returns bounds of clip, transformed by inverse of Matrix.

    If clip is empty, return false, and set bounds to Rect.MakeEmpty(), where all Rect sides equal zero.

    bounds is outset by one to account for partial pixel coverage if clip is anti-aliased.

    bounds:

    Rect of clip in local coordinates

    return:

    true if clip bounds is not empty

Canvas.getLocalToDevice(self: skia.Canvas) skia.M44
Canvas.getProps(self: skia.Canvas, props: SkSurfaceProps) bool

Copies SurfaceProps, if Canvas is associated with raster surface or GPU surface, and returns true.

Otherwise, returns false and leave props unchanged.

Parameters:

props (skia.SurfaceProps) – storage for writable SurfaceProps

Returns:

true if SurfaceProps was copied

Canvas.getSaveCount(self: skia.Canvas) int

Returns the number of saved states, each containing: Matrix 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.

Returns:

depth of save state stack

Canvas.getSurface(self: skia.Canvas) SkSurface

Sometimes a canvas is owned by a surface.

If it is, getSurface() will return a bare pointer to that surface, else this will return nullptr.

Return type:

skia.Surface or None

Canvas.getTotalMatrix(self: skia.Canvas) skia.Matrix

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

Returns:

3x3 version of getLocalToDevice()

Canvas.imageInfo(self: skia.Canvas) skia.ImageInfo

Returns ImageInfo for Canvas.

If Canvas is not associated with raster surface or GPU surface, returned ColorType is set to ColorType.kUnknown_ColorType.

Returns:

dimensions and ColorType of Canvas

Canvas.isClipEmpty(self: skia.Canvas) bool

Returns true if clip is empty; that is, nothing will draw.

May do work when called; it should not be called more often than needed. However, once called, subsequent calls perform no work until clip changes.

Returns:

true if clip is empty

Canvas.isClipRect(self: skia.Canvas) bool

Returns true if clip is Rect and not empty.

Returns false if the clip is empty, or if it is not Rect.

Returns:

true if clip is Rect and not empty

Canvas.makeSurface(self: skia.Canvas, info: skia.ImageInfo, props: SkSurfaceProps = None) SkSurface

Creates Surface matching info and props, and associates it with Canvas.

Returns nullptr if no match found.

If props is nullptr, matches SurfaceProps in Canvas. If props is nullptr and Canvas does not have SurfaceProps, creates Surface with default SurfaceProps.

Parameters:
Returns:

SkSurface matching info and props, or nullptr if no match is available

Return type:

skia.Surface or None

Canvas.peekPixels(self: skia.Canvas, pixmap: skia.Pixmap) bool

Returns true if Canvas has direct access to its pixels.

Pixels are readable when BaseDevice is raster. Pixels are not readable when Canvas is returned from GPU surface, returned by Document::beginPage, returned by PictureRecorder::beginRecording, or Canvas is the base of a utility class like DebugCanvas.

pixmap is valid only while Canvas is in scope and unchanged. Any Canvas or Surface call may invalidate the pixmap values.

Parameters:

pixmap (skia.Pixmap) – storage for pixel state if pixels are readable; otherwise, ignored

Returns:

true if Canvas has direct access to pixels

Canvas.quickReject(*args, **kwargs)

Overloaded function.

  1. quickReject(self: skia.Canvas, rect: skia.Rect) -> bool

    Returns true if Rect rect, transformed by Matrix, can be quickly determined to be outside of clip.

    May return false even though rect is outside of clip.

    Use to check if an area to be drawn is clipped out, to skip subsequent draw calls.

    rect:

    Rect to compare with clip

    return:

    true if rect, transformed by Matrix, does not intersect clip

  2. quickReject(self: skia.Canvas, path: skia.Path) -> bool

    Returns true if path, transformed by Matrix, can be quickly determined to be outside of clip.

    May return false even though path is outside of clip.

    Use to check if an area to be drawn is clipped out, to skip subsequent draw calls.

    path:

    Path to compare with clip

    return:

    true if path, transformed by Matrix, does not intersect clip

Canvas.readPixels(*args, **kwargs)

Overloaded function.

  1. readPixels(self: skia.Canvas, dstInfo: skia.ImageInfo, dstPixels: Buffer, dstRowBytes: int = 0, srcX: int = 0, srcY: int = 0) -> bool

    Copies Rect of pixels from Canvas into dstPixels.

    Matrix and clip are ignored.

    Source Rect corners are (srcX, srcY) and ( imageInfo().width(), imageInfo().height()). Destination Rect corners are (0, 0) and (array.shape[1], array.shape[0]). Copies each readable pixel intersecting both rectangles, without scaling, converting to ColorType.kN32_ColorType and AlphaType.kPremul_AlphaType if required.

    Pixels are readable when BaseDevice is raster, or backed by a GPU. Pixels are not readable when Canvas is returned by Document.beginPage(), returned by PictureRecorder.beginRecording(), or Canvas is the base of a utility class like DebugCanvas.

    The destination pixel storage must be allocated by the caller.

    Pixel values are converted only if ColorType and AlphaType do not match. Only pixels within both source and destination rectangles are copied. array contents outside Rect intersection are unchanged.

    Pass negative values for srcX or srcY to offset pixels across or down destination.

    Does not copy, and returns false if:

    dstInfo:

    width, height, ColorType, and AlphaType of dstPixels

    dstPixels:

    storage for pixels; dstInfo.height() times dstRowBytes, or larger

    dstRowBytes:

    size of one destination row; dstInfo.width() times pixel size, or larger. Ignored when dstPixels has more than one-dimension.

    srcX:

    offset into readable pixels on x-axis; may be negative

    srcY:

    offset into readable pixels on y-axis; may be negative

    return:

    true if pixels were copied

  2. readPixels(self: skia.Canvas, pixmap: skia.Pixmap, srcX: int = 0, srcY: int = 0) -> bool

    Copies Rect of pixels from Canvas into pixmap.

    Matrix and clip are ignored.

    Source Rect corners are (srcX, srcY) and ( imageInfo().width(), imageInfo().height()). Destination Rect corners are (0, 0) and (pixmap.width(), pixmap.height()). Copies each readable pixel intersecting both rectangles, without scaling, converting to pixmap.colorType() and pixmap.alphaType() if required.

    Pixels are readable when BaseDevice is raster, or backed by a GPU. Pixels are not readable when Canvas is returned by Document.beginPage(), returned by PictureRecorder.beginRecording(), or Canvas is the base of a utility class like DebugCanvas.

    Caller must allocate pixel storage in pixmap if needed.

    Pixel values are converted only if ColorType and AlphaType do not match. Only pixels within both source and destination Rect are copied. pixmap pixels contents outside Rect intersection are unchanged.

    Pass negative values for srcX or srcY to offset pixels across or down pixmap.

    Does not copy, and returns false if:

    • Source and destination rectangles do not intersect.

    • Canvas pixels could not be converted to

      pixmap.colorType() or pixmap.alphaType().

    • Canvas pixels are not readable; for instance,

      Canvas is document-based.

    • Pixmap pixels could not be allocated.

    • pixmap.rowBytes() is too small to contain one row of pixels.

    pixmap:

    storage for pixels copied from Canvas

    srcX:

    offset into readable pixels on x-axis; may be negative

    srcY:

    offset into readable pixels on y-axis; may be negative

    return:

    true if pixels were copied

  3. readPixels(self: skia.Canvas, bitmap: skia.Bitmap, srcX: int = 0, srcY: int = 0) -> bool

    Copies Rect of pixels from Canvas into bitmap.

    Matrix and clip are ignored.

    Source Rect corners are (srcX, srcY) and ( imageInfo().width(), imageInfo().height()). Destination Rect corners are (0, 0) and (bitmap.width(), bitmap.height()). Copies each readable pixel intersecting both rectangles, without scaling, converting to bitmap.colorType() and bitmap.alphaType() if required.

    Pixels are readable when BaseDevice is raster, or backed by a GPU. Pixels are not readable when Canvas is returned by Document.beginPage(), returned by PictureRecorder.beginRecording(), or Canvas is the base of a utility class like DebugCanvas.

    Caller must allocate pixel storage in bitmap if needed.

    Bitmap values are converted only if ColorType and AlphaType do not match. Only pixels within both source and destination rectangles are copied. Bitmap pixels outside Rect intersection are unchanged.

    Pass negative values for srcX or srcY to offset pixels across or down bitmap.

    Does not copy, and returns false if:

    • Source and destination rectangles do not intersect.

    • Canvas pixels could not be converted to bitmap.colorType()

      or bitmap.alphaType().

    • Canvas pixels are not readable; for instance,

      Canvas is document-based.

    • bitmap pixels could not be allocated.

    • bitmap.rowBytes() is too small to contain one row of pixels.

    bitmap:

    storage for pixels copied from Canvas

    srcX:

    offset into readable pixels on x-axis; may be negative

    srcY:

    offset into readable pixels on y-axis; may be negative

    return:

    true if pixels were copied

Canvas.resetMatrix(self: skia.Canvas) None

Sets SkMatrix to the identity matrix.

Any prior matrix state is overwritten.

Canvas.restore(self: skia.Canvas) None

Removes changes to Matrix and clip since Canvas state was last saved.

The state is removed from the stack.

Does nothing if the stack is empty.

Canvas.restoreToCount(self: skia.Canvas, saveCount: int) None

Restores state to Matrix and clip values when save(), saveLayer(), saveLayerPreserveLCDTextRequests(), or saveLayerAlpha() returned saveCount.

Does nothing if saveCount is greater than state stack count. Restores state to initial values if saveCount is less than or equal to one.

Parameters:

saveCount (int) – depth of state stack to restore

Canvas.rotate(*args, **kwargs)

Overloaded function.

  1. rotate(self: skia.Canvas, degrees: float) -> None

    Rotates Matrix by degrees.

    Positive degrees rotates clockwise.

    Mathematically, replaces Matrix with a rotation matrix premultiplied with Matrix.

    This has the effect of rotating the drawing by degrees before transforming the result with Matrix.

    degrees:

    amount to rotate, in degrees

  2. rotate(self: skia.Canvas, degrees: float, px: float, py: float) -> None

    Rotates Matrix by degrees about a point at (px, py).

    Positive degrees rotates clockwise.

    Mathematically, constructs a rotation matrix; premultiplies the rotation matrix by a translation matrix; then replaces Matrix with the resulting matrix premultiplied with Matrix.

    This has the effect of rotating the drawing about a given point before transforming the result with Matrix.

    degrees:

    amount to rotate, in degrees

    px:

    x-axis value of the point to rotate about

    py:

    y-axis value of the point to rotate about

Canvas.save(self: skia.Canvas) int

Saves Matrix and clip.

Calling restore() discards changes to Matrix and clip, restoring the Matrix and clip to their state when save() was called.

Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(), clipPath(), clipRegion().

Saved Canvas state is put on a stack; multiple calls to save() should be balance by an equal number of calls to restore().

Call restoreToCount() with result to restore this and subsequent saves.

Returns:

depth of saved stack

Canvas.saveLayer(*args, **kwargs)

Overloaded function.

  1. saveLayer(self: skia.Canvas, bounds: skia.Rect = None, paint: skia.Paint = None) -> int

    Saves Matrix and clip, and allocates a Bitmap for subsequent drawing.

    Calling restore() discards changes to Matrix and clip, and draws the Bitmap.

    Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(), clipPath(), clipRegion().

    Rect bounds suggests but does not define the Bitmap size. To clip drawing to a specific rectangle, use clipRect().

    Optional Paint paint applies alpha, ColorFilter, ImageFilter, and BlendMode when restore() is called.

    Call restoreToCount() with returned value to restore this and subsequent saves.

    bounds:

    hint to limit the size of the layer; may be nullptr

    paint:

    graphics state for layer; may be nullptr

    return:

    depth of saved stack

  2. saveLayer(self: skia.Canvas, layerRec: skia.Canvas.SaveLayerRec) -> int

    Saves Matrix and clip, and allocates a Bitmap for subsequent drawing.

    Calling restore() discards changes to Matrix and clip, and blends Bitmap with alpha opacity onto the prior layer.

    Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(), clipPath(), clipRegion().

    SaveLayerRec contains the state used to create the layer.

    Call restoreToCount() with returned value to restore this and subsequent saves.

    layerRec:

    layer state

    return:

    depth of save state stack before this call was made.

Canvas.saveLayerAlpha(self: skia.Canvas, bounds: skia.Rect, alpha: int) int

Saves Matrix and clip, and allocates a Bitmap for subsequent drawing.

Calling restore() discards changes to Matrix and clip, and blends layer with alpha opacity onto prior layer.

Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(), clipPath(), clipRegion().

Rect bounds suggests but does not define the Bitmap size. To clip drawing to a specific rectangle, use clipRect().

alpha of zero is fully transparent, 255 is fully opaque.

Call restoreToCount() with returned value to restore this and subsequent saves.

Parameters:
  • bounds (skia.Rect) – hint to limit the size of the layer; may be nullptr

  • alpha (int) – opacity of layer

Returns:

depth of saved stack

Canvas.scale(self: skia.Canvas, sx: float, sy: float) None

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

Mathematically, replaces Matrix with a scale matrix premultiplied with Matrix.

This has the effect of scaling the drawing by (sx, sy) before transforming the result with Matrix.

Parameters:
  • sx (float) – amount to scale on x-axis

  • sy (float) – amount to scale on y-axis

Canvas.setMatrix(*args, **kwargs)

Overloaded function.

  1. setMatrix(self: skia.Canvas, matrix: skia.M44) -> None

    Replaces Matrix with matrix.

    Unlike concat(), any prior matrix state is overwritten.

    param skia.Matrix matrix:

    matrix to copy, replacing existing Matrix

  2. setMatrix(self: skia.Canvas, matrix: skia.Matrix) -> None

    DEPRECATED – use SkM44 version

    Unlike concat(), any prior matrix state is overwritten.

    param skia.Matrix matrix:

    matrix to copy, replacing existing Matrix

Canvas.skew(self: skia.Canvas, sx: float, sy: float) None

Skews Matrix 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.

Mathematically, replaces Matrix with a skew matrix premultiplied with Matrix.

This has the effect of skewing the drawing by (sx, sy) before transforming the result with Matrix.

Parameters:
  • sx (float) – amount to skew on x-axis

  • sy (float) – amount to skew on y-axis

Canvas.toarray(self: skia.Canvas, srcX: int = 0, srcY: int = 0, colorType: skia.ColorType = skia.ColorType.kUnknown_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnpremul_AlphaType, colorSpace: skia.ColorSpace = None) numpy.ndarray

Exports a numpy.ndarray.

Parameters:
  • srcX – offset into readable pixels on x-axis; may be negative

  • srcY – offset into readable pixels on y-axis; may be negative

  • colorType – target ColorType

  • alphaType – target AlphaType

  • colorSpace – target ColorSpace

Returns:

numpy.ndarray

Canvas.translate(self: skia.Canvas, dx: float, dy: float) None

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

Mathematically, replaces Matrix with a translation matrix premultiplied with Matrix.

This has the effect of moving the drawing by (dx, dy) before transforming the result with Matrix.

Parameters:
  • dx – distance to translate on x-axis

  • dy – distance to translate on y-axis

Canvas.writePixels(*args, **kwargs)

Overloaded function.

  1. writePixels(self: skia.Canvas, info: skia.ImageInfo, pixels: Buffer, rowBytes: int = 0, x: int = 0, y: int = 0) -> bool

    Copies Rect from pixels to Canvas.

    Matrix and clip are ignored. Source Rect corners are (0, 0) and (info.width(), info.height()). Destination Rect corners are (x, y) and (imageInfo().width(), imageInfo().height()).

    Copies each readable pixel intersecting both rectangles, without scaling, converting to imageInfo().colorType() and imageInfo().alphaType() if required.

    Pixels are writable when BaseDevice is raster, or backed by a GPU. Pixels are not writable when Canvas is returned by Document.beginPage(), returned by PictureRecorder.beginRecording(), or Canvas is the base of a utility class like DebugCanvas.

    Pixel values are converted only if ColorType and AlphaType do not match. Only pixels within both source and destination rectangles are copied. Canvas pixels outside Rect intersection are unchanged.

    Pass negative values for x or y to offset pixels to the left or above Canvas pixels.

    Does not copy, and returns false if:

    • Source and destination rectangles do not intersect.

    • pixels could not be converted to Canvas

      imageInfo().colorType() or imageInfo().alphaType().

    • Canvas pixels are not writable; for instance,

      Canvas is document-based.

    • rowBytes is too small to contain one row of pixels.

    array:

    pixels to copy, in native 32-bit colors.

    x:

    offset into Canvas writable pixels on x-axis; may be negative

    y:

    offset into Canvas writable pixels on y-axis; may be negative

    return:

    true if pixels were written to Canvas

  2. writePixels(self: skia.Canvas, bitmap: skia.Bitmap, x: int = 0, y: int = 0) -> bool

    Copies Rect from pixels to Canvas.

    Matrix and clip are ignored. Source Rect corners are (0, 0) and (bitmap.width(), bitmap.height()).

    Destination Rect corners are (x, y) and (imageInfo().width(), imageInfo().height()).

    Copies each readable pixel intersecting both rectangles, without scaling, converting to imageInfo().colorType() and imageInfo().alphaType() if required.

    Pixels are writable when BaseDevice is raster, or backed by a GPU. Pixels are not writable when Canvas is returned by Document.beginPage(), returned by PictureRecorder.beginRecording(), or Canvas is the base of a utility class like DebugCanvas.

    Pixel values are converted only if ColorType and AlphaType do not match. Only pixels within both source and destination rectangles are copied. Canvas pixels outside Rect intersection are unchanged.

    Pass negative values for x or y to offset pixels to the left or above Canvas pixels.

    Does not copy, and returns false if:

    • Source and destination rectangles do not intersect.

    • bitmap does not have allocated pixels.

    • bitmap pixels could not be converted to

      Canvas.imageInfo().colorType() or alphaType().

    • Canvas pixels are not writable; for instance,

      Canvas is document-based.

    • bitmap pixels are inaccessible; for instance, bitmap wraps a texture.

    bitmap:

    contains pixels copied to Canvas.

    x:

    offset into Canvas writable pixels on x-axis; may be negative

    y:

    offset into Canvas writable pixels on y-axis; may be negative

    return:

    true if pixels were written to Canvas

Attributes

Canvas.kAll_QuadAAFlags = <QuadAAFlags.kAll_QuadAAFlags: 15>
Canvas.kBottom_QuadAAFlag = <QuadAAFlags.kBottom_QuadAAFlag: 8>
Canvas.kF16ColorType = <SaveLayerFlags.kF16ColorType: 16>
Canvas.kFast_SrcRectConstraint = <SrcRectConstraint.kFast_SrcRectConstraint: 1>
Canvas.kInitWithPrevious_SaveLayerFlag = <SaveLayerFlags.kInitWithPrevious_SaveLayerFlag: 4>
Canvas.kLeft_QuadAAFlag = <QuadAAFlags.kLeft_QuadAAFlag: 1>
Canvas.kLines_PointMode = <PointMode.kLines_PointMode: 1>
Canvas.kNone_QuadAAFlags = <QuadAAFlags.kNone_QuadAAFlags: 0>
Canvas.kPoints_PointMode = <PointMode.kPoints_PointMode: 0>
Canvas.kPolygon_PointMode = <PointMode.kPolygon_PointMode: 2>
Canvas.kPreserveLCDText_SaveLayerFlag = <SaveLayerFlags.kPreserveLCDText_SaveLayerFlag: 2>
Canvas.kRight_QuadAAFlag = <QuadAAFlags.kRight_QuadAAFlag: 4>
Canvas.kStrict_SrcRectConstraint = <SrcRectConstraint.kStrict_SrcRectConstraint: 0>
Canvas.kTop_QuadAAFlag = <QuadAAFlags.kTop_QuadAAFlag: 2>