Canvas
public final 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.
Typically you create the Canvas from a bitmap, but an empty canvas can be created with the
some fo the static make
methods, and serve special purposes:
makeEmpty(width:height)
- creates an empty canvas that ignores all drawing commandsmakeOverdraw(canvas:)
- A canvas that captures all drawing commands, and rather than draw the actual content, it increments the alpha channel of each pixel every time it would have been touched by a draw call.makeNway(width:height:)
- will create a passthrough n-way canvas. Once created you can add canvas withaddCanvas
, and remove them withremoveCanvas
-
Constructs a canvas that draws into bitmap. Sets
SurfaceProps
.kLegacyFontHost_InitTypein constructed
Surface`.Bitmap
is copied so that subsequently editing bitmap will not affect constructedCanvas
.Declaration
Swift
public init(_ bitmap: Bitmap)
Return Value
Canvas
that can be used to draw into bitmap -
A type of SKCanvas that provides a base type for canvases that do not need to rasterize.
Declaration
Swift
public static func makeEmpty(width: Int32, height: Int32) -> Canvas
-
A canvas that captures all drawing commands, and rather than draw the actual content, it increments the alpha channel of each pixel every time it would have been touched by a draw call.
Declaration
Swift
public static func makeOverdraw(baseCanvas: Canvas) -> Canvas
Parameters
baseCanvas
The canvas to draw on.
Return Value
the new overdrawn canvas
-
Creates a canvas that can draw into multiple canvases at once, the methods
addCanvas
,removeCanvas
andremoveAll
can be used in this caseDeclaration
Swift
public static func makeNway(width: Int32, height: Int32) -> Canvas
Parameters
width
widht for the canvas
height
height for the canvas
Return Value
the new n-way canvas
-
Adds a new canvas to an nway-canvas - any operations performed on the canvas from this point on will additionally be reflected in the additional canvas.
Declaration
Swift
public func addCanvas(_ canvas: Canvas)
Parameters
canvas
the new canvas to add
-
Removes a canvas in an nway-canvas that was previously added with
addCanvas
Declaration
Swift
public func removeCanvas(_ canvas: Canvas)
-
Removes all the additional canvases that have been added to an nway-canvas, and brings back the nway canvas to have no canvas to drawn on.
Declaration
Swift
public func removeAll()
-
Undocumented
Declaration
Swift
public func drawText(text: String, x: Float, y: Float, paint: Paint)
-
Returns true if
Rect
rect, transformed byMatrix
, 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.Declaration
Swift
public func quickReject(rect: Rect) -> Bool
Parameters
rect
Rect
to compare with clipReturn Value
true if rect, transformed by
Matrix
, does not intersect clip -
Saves
Matrix
and clip. Callingrestore
discards changes toMatrix
and clip, restoring theMatrix
and clip to their state whensave
was called.Matrix
may be changed bytranslate
,scale
,rotate
,skew
,concat
,setMatrix
, andresetMatrix
Clip may be changed byclipRect
,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.Declaration
Swift
public func save() -> Int32
Return Value
depth of saved stack
-
Saves
Matrix
and clip, and allocates aBitmap
for subsequent drawing. Calling restore() discards changes toMatrix
and clip, and draws theBitmap
.Matrix
may be changed bytranslate
,scale
,rotate
,skew
,concat
,setMatrix
, andresetMatrix
Clip may be changed byclipRect
,clipRRect
,clipPath
,clipRegion
.Rect
bounds suggests but does not define theBitmap
size. To clip drawing to a specific rectangle, use clipRect().Optional
Paint
paint applies alpha,ColorFilter
,ImageFilter
, andBlendMode
whenrestore
is called.Call
restoreToCount
with returned value to restore this and subsequent saves.Parameters
limit
hint to limit the size of the layer; may be
nil
paint
graphics state for layer; may be
nil
Return Value
depth of saved stack
-
Fills clip with color color. mode determines how ARGB is combined with destination.
Parameters
color
unpremultiplied ARGB
blendMode
BlendMode
used to combine source color and destination -
Draws line segment from p0 to p1 using clip,
Matrix
, andPaint
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.stroke
Parameters
p0
start of line segment
p1
end of line segment
paint
stroke, blend, color, and so on, used to draw
-
Draws line segment from (x0, y0) to (x1, y1) using clip,
Matrix
, andPaint
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.stroke
Declaration
Swift
public func drawLine(x0: Float, y0: Float, x1: Float, y1: Float, paint: Paint)
Parameters
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
-
Undocumented
Declaration
Swift
public func clear()
-
Removes changes to
Matrix
and clip sinceCanvas
state was last saved. The state is removed from the stack. Does nothing if the stack is empty. example: https://fiddle.skia.org/c/@AutoCanvasRestore_restore example: https://fiddle.skia.org/c/@Canvas_restoreDeclaration
Swift
public func restore()
-
Restores state to
Matrix
and clip values whensave()
,saveLayer()
,saveLayerPreserveLCDTextRequests()
, orsaveLayerAlpha()
returnedcount
. Does nothing ifcount
is greater than state stack count. Restores state to initial values ifcount
is less than or equal to one.Declaration
Swift
public func restoreToCount(count: Int32)
Parameters
count
depth of state stack to restore
-
Translates
Matrix
by dx along the x-axis and dy along the y-axis. Mathematically, replacesMatrix
with a translation matrix premultiplied withMatrix
. This has the effect of moving the drawing by (dx, dy) before transforming the result withMatrix
.Declaration
Swift
public func translate(dx: Float, dy: Float)
Parameters
dx
distance to translate on x-axis
dy
distance to translate on y-axis
-
Translates
Matrix
by dx along the x-axis and dy along the y-axis. Mathematically, replacesMatrix
with a translation matrix premultiplied withMatrix
. This has the effect of moving the drawing bypt
before transforming the result withMatrix
.Declaration
Swift
public func translate(pt: Point)
Parameters
pt
distance to translate on x-axis and y-axis
-
Scales
Matrix
byscale
on the x-axis and y-axis. Mathematically, replacesMatrix
with a scale matrix premultiplied withMatrix
. This has the effect of scaling the drawing by (sx, sy) before transforming the result withMatrix
.Declaration
Swift
public func scale(_ scale: Float)
Parameters
scale
amount to scale on both axis
-
Scales
Matrix
by sx on the x-axis and sy on the y-axis. Mathematically, replacesMatrix
with a scale matrix premultiplied withMatrix
. This has the effect of scaling the drawing by (sx, sy) before transforming the result withMatrix
.Declaration
Swift
public func scale(sx: Float, sy: Float)
Parameters
sx
amount to scale on x-axis
sy
amount to scale on y-axis
-
Scales
Matrix
by sx on the x-axis and sy on the y-axis. Mathematically, replacesMatrix
with a scale matrix premultiplied withMatrix
. This has the effect of scaling the drawing by (sx, sy) before transforming the result withMatrix
.Declaration
Swift
public func scale(factor: Point)
Parameters
factor
the scale encoded as a point
-
Declaration
Swift
public func scale(sx: Float, sy: Float, pivot: Point)
Parameters
pivot
the pivot point for the scale to take place
-
Rotates
Matrix
by degrees. Positive degrees rotates clockwise. Mathematically, replacesMatrix
with a rotation matrix premultiplied withMatrix
. This has the effect of rotating the drawing by degrees before transforming the result withMatrix
.Declaration
Swift
public func rotate(degrees: Float)
Parameters
degrees
amount to rotate, in degrees
-
Rotates
Matrix
by radians. Positive values rotates clockwise. Mathematically, replacesMatrix
with a rotation matrix premultiplied withMatrix
. This has the effect of rotating the drawing by radians before transforming the result withMatrix
.Declaration
Swift
public func rotate(radians: Float)
Parameters
degrees
amount to rotate, in radians
-
Undocumented
Declaration
Swift
public func rotate(degrees: Float, pivot: Point)
-
Undocumented
Declaration
Swift
public func rotate(radians: Float, pivot: Point)
-
Undocumented
Declaration
Swift
public func skew(sx: Float, sy: Float)
-
Undocumented
Declaration
Swift
public func clip(rect: Rect, operation: ClipOperation = .intersect, antialias: Bool = false)
-
Undocumented
Declaration
Swift
public func clip(roundedRect: RoundRect, operation: ClipOperation = .intersect, antialias: Bool = false)
-
Undocumented
Declaration
Swift
public func clip(path: Path, operation: ClipOperation = .intersect, antialias: Bool = false)
-
Undocumented
Declaration
Swift
public func clip(region: Region, operation: ClipOperation = .intersect)
-
Undocumented
Declaration
Swift
public var localClipBounds: Rect { get }
-
Undocumented
Declaration
Swift
public var deviceClipounds: IRect { get }
-
Undocumented
Declaration
Swift
public func getLocalClipBounds() -> (bounds: Rect, empty: Bool)
-
Undocumented
Declaration
Swift
public func getDeviceClipBounds() -> (bounds: IRect, empty: Bool)
-
Undocumented
Declaration
Swift
public func draw(_ paint: Paint)
-
Undocumented
-
Undocumented
-
Undocumented
-
Undocumented
-
Undocumented
-
Undocumented
Declaration
Swift
public func drawCircle(_ cx: Float, _ cy: Float, _ radius: Float, _ paint: Paint)
-
Undocumented
-
Undocumented
-
Undocumented
-
Undocumented
Declaration
Swift
public func drawPoint(_ x: Float, _ y: Float, _ paint: Paint)
-
Undocumented
Declaration
Swift
public func drawPoint(_ x: Float, _ y: Float, _ color: Color)
-
Undocumented
Declaration
-
Undocumented
-
Undocumented
Declaration
-
Draws
Picture
picture, using clip andMatrix
. Clip andMatrix
are unchanged by picture contents, as if save() was called before and restore() was called after drawPicture().Picture
records a series of draw commands for later playback.Parameters
picture
recorded drawing commands to play
paint
Paint
to apply transparency, filtering, and so on; may benil
-
Draws
Picture
picture, using clip andMatrix
; transforming picture withMatrix
matrix, if provided; and usePaint
paint alpha,ColorFilter
,ImageFilter
, andBlendMode
, if provided. matrix transformation is equivalent to: save(), concat(), drawPicture(), restore(). paint use is equivalent to: saveLayer(), drawPicture(), restore().Declaration
-
Undocumented
-
Undocumented
Declaration
-
Undocumented
-
Draws
Rect
src ofBitmap
bitmap, scaled and translated to fillRect
dst. Additionally transform draw using clip,Matrix
, and optionalPaint
paint.If
Paint
paint is supplied, applyColorFilter
, alpha,ImageFilter
,BlendMode
, andDrawLooper
. If bitmap is.alpha8
, applyShader
.If paint contains
MaskFilter
, generate mask from bitmap bounds.If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, just as
Shader
made fromShader
::MakeBitmapShader withShader
::kClamp_TileMode set replicates the bitmap edge color when it samples outside of its bounds.Declaration
Parameters
bitmap
Bitmap
containing pixels, dimensions, and formatsource
source
Rect
of image to draw fromdest
destination
Rect
of image to draw topaint
Paint
containingBlendMode
,ColorFilter
,ImageFilter
, and so on; or nullptr -
Draws a surface on the canvas.
Declaration
Parameters
surface
The surface to draw
x
The destination x-coordinate for the surface.
y
The destination y-coordinate for the surface.
paint
The paint to use when drawing the surface, or nil.
-
- Draws text on the canvas at the specified coordinates.
Declaration
Swift
public func drawText(_ text: String, _ x: Float, _ y: Float, paint: Paint)
-
Draws
TextBlob
blob at (x, y), using clip,Matrix
, andPaint
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, andPaint
subpixel text.TextEncoding
must be set toTextEncoding
::kGlyphID.Elements of paint: anti-alias,
BlendMode
, color including alpha,ColorFilter
,Paint
dither,DrawLooper
,MaskFilter
,PathEffect
,Shader
, andPaint
::Style; apply to blob. IfPaint
containsPaint
::kStroke_Style:Paint
miter limit,Paint
::Cap,Paint
::Join, andPaint
stroke width; apply toPath
created from blob.Declaration
Parameters
textBlob
glyphs, positions, and their paints’ text size, typeface, and so on
x
horizontal offset applied to blob
y
vertical offset applied to blob
paint
blend, color, stroking, and so on, used to draw
-
Undocumented
-
Undocumented
Declaration
-
Triggers the immediate execution of all pending draw operations. If
Canvas
is associated with GPU surface, resolves all pending GPU operations. IfCanvas
is associated with raster surface, has no effect; raster draw operations are never deferred.Declaration
Swift
public func flush()
-
Undocumented
Declaration
-
Undocumented
Declaration
-
Sets
Matrix
to the identity matrix. Any prior matrix state is overwritten.Declaration
Swift
public func resetMatrix()
-
Replaces
Matrix
with matrix. Unlike concat(), any prior matrix state is overwritten.Declaration
Swift
public func setMatrix(_ matrix: Matrix)
Parameters
matrix
matrix to copy, replacing existing
Matrix
example: https://fiddle.skia.org/c/@Canvas_setMatrix -
Declaration
Swift
public var totalMatrix: Matrix { get }
Return Value
Matrix
inCanvas
example: https://fiddle.skia.org/c/@Canvas_getTotalMatrix example: https://fiddle.skia.org/c/@Clip -
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.Declaration
Swift
public var saveCount: Int32 { get }
Return Value
depth of save state stack example: https://fiddle.skia.org/c/@Canvas_getSaveCount