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 constructedSurface`.Bitmapis copied so that subsequently editing bitmap will not affect constructedCanvas.Declaration
Swift
public init(_ bitmap: Bitmap)Return Value
Canvasthat 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) -> CanvasParameters
baseCanvasThe canvas to draw on.
Return Value
the new overdrawn canvas
-
Creates a canvas that can draw into multiple canvases at once, the methods
addCanvas,removeCanvasandremoveAllcan be used in this caseDeclaration
Swift
public static func makeNway(width: Int32, height: Int32) -> CanvasParameters
widthwidht for the canvas
heightheight 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
canvasthe new canvas to add
-
Removes a canvas in an nway-canvas that was previously added with
addCanvasDeclaration
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
Rectrect, 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) -> BoolParameters
rectRectto compare with clipReturn Value
true if rect, transformed by
Matrix, does not intersect clip -
Saves
Matrixand clip. Callingrestorediscards changes toMatrixand clip, restoring theMatrixand clip to their state whensavewas called.Matrixmay be changed bytranslate,scale,rotate,skew,concat,setMatrix, andresetMatrixClip may be changed byclipRect,clipRRect,clipPath,clipRegion.Saved
Canvasstate 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() -> Int32Return Value
depth of saved stack
-
Saves
Matrixand clip, and allocates aBitmapfor subsequent drawing. Calling restore() discards changes toMatrixand clip, and draws theBitmap.Matrixmay be changed bytranslate,scale,rotate,skew,concat,setMatrix, andresetMatrixClip may be changed byclipRect,clipRRect,clipPath,clipRegion.Rectbounds suggests but does not define theBitmapsize. To clip drawing to a specific rectangle, use clipRect().Optional
Paintpaint applies alpha,ColorFilter,ImageFilter, andBlendModewhenrestoreis called.Call
restoreToCountwith returned value to restore this and subsequent saves.Parameters
limithint to limit the size of the layer; may be
nilpaintgraphics state for layer; may be
nilReturn Value
depth of saved stack
-
Fills clip with color color. mode determines how ARGB is combined with destination.
Parameters
colorunpremultiplied ARGB
blendModeBlendModeused to combine source color and destination -
Draws line segment from p0 to p1 using clip,
Matrix, andPaintpaint. In paint:Paintstroke width describes the line thickness;Paint.capdraws the end rounded or square;Paint.styleis ignored, as if were set to.strokeParameters
p0start of line segment
p1end of line segment
paintstroke, blend, color, and so on, used to draw
-
Draws line segment from (x0, y0) to (x1, y1) using clip,
Matrix, andPaintpaint. In paint:Paintstroke width describes the line thickness;Paint.capdraws the end rounded or square;Paint.styleis ignored, as if were set to.strokeDeclaration
Swift
public func drawLine(x0: Float, y0: Float, x1: Float, y1: Float, paint: Paint)Parameters
x0start of line segment on x-axis
y0start of line segment on y-axis
x1end of line segment on x-axis
y1end of line segment on y-axis
paintstroke, blend, color, and so on, used to draw
-
Undocumented
Declaration
Swift
public func clear() -
Removes changes to
Matrixand clip sinceCanvasstate 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
Matrixand clip values whensave(),saveLayer(),saveLayerPreserveLCDTextRequests(), orsaveLayerAlpha()returnedcount. Does nothing ifcountis greater than state stack count. Restores state to initial values ifcountis less than or equal to one.Declaration
Swift
public func restoreToCount(count: Int32)Parameters
countdepth of state stack to restore
-
Translates
Matrixby dx along the x-axis and dy along the y-axis. Mathematically, replacesMatrixwith 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
dxdistance to translate on x-axis
dydistance to translate on y-axis
-
Translates
Matrixby dx along the x-axis and dy along the y-axis. Mathematically, replacesMatrixwith a translation matrix premultiplied withMatrix. This has the effect of moving the drawing byptbefore transforming the result withMatrix.Declaration
Swift
public func translate(pt: Point)Parameters
ptdistance to translate on x-axis and y-axis
-
Scales
Matrixbyscaleon the x-axis and y-axis. Mathematically, replacesMatrixwith 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
scaleamount to scale on both axis
-
Scales
Matrixby sx on the x-axis and sy on the y-axis. Mathematically, replacesMatrixwith 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
sxamount to scale on x-axis
syamount to scale on y-axis
-
Scales
Matrixby sx on the x-axis and sy on the y-axis. Mathematically, replacesMatrixwith 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
factorthe scale encoded as a point
-
Declaration
Swift
public func scale(sx: Float, sy: Float, pivot: Point)Parameters
pivotthe pivot point for the scale to take place
-
Rotates
Matrixby degrees. Positive degrees rotates clockwise. Mathematically, replacesMatrixwith 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
degreesamount to rotate, in degrees
-
Rotates
Matrixby radians. Positive values rotates clockwise. Mathematically, replacesMatrixwith 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
degreesamount 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
Picturepicture, using clip andMatrix. Clip andMatrixare unchanged by picture contents, as if save() was called before and restore() was called after drawPicture().Picturerecords a series of draw commands for later playback.Parameters
picturerecorded drawing commands to play
paintPaintto apply transparency, filtering, and so on; may benil -
Draws
Picturepicture, using clip andMatrix; transforming picture withMatrixmatrix, if provided; and usePaintpaint 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
Rectsrc ofBitmapbitmap, scaled and translated to fillRectdst. Additionally transform draw using clip,Matrix, and optionalPaintpaint.If
Paintpaint 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
Shadermade fromShader::MakeBitmapShader withShader::kClamp_TileMode set replicates the bitmap edge color when it samples outside of its bounds.Declaration
Parameters
bitmapBitmapcontaining pixels, dimensions, and formatsourcesource
Rectof image to draw fromdestdestination
Rectof image to draw topaintPaintcontainingBlendMode,ColorFilter,ImageFilter, and so on; or nullptr -
Draws a surface on the canvas.
Declaration
Parameters
surfaceThe surface to draw
xThe destination x-coordinate for the surface.
yThe destination y-coordinate for the surface.
paintThe 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
TextBlobblob at (x, y), using clip,Matrix, andPaintpaint. blob contains glyphs, their positions, and paint attributes specific to text:Typeface,Painttext size,Painttext scale x,Painttext skew x,Paint::Align,Paint::Hinting, anti-alias,Paintfake bold,Paintfont embedded bitmaps,Paintfull hinting spacing, LCD text,Paintlinear text, andPaintsubpixel text.TextEncodingmust be set toTextEncoding::kGlyphID.Elements of paint: anti-alias,
BlendMode, color including alpha,ColorFilter,Paintdither,DrawLooper,MaskFilter,PathEffect,Shader, andPaint::Style; apply to blob. IfPaintcontainsPaint::kStroke_Style:Paintmiter limit,Paint::Cap,Paint::Join, andPaintstroke width; apply toPathcreated from blob.Declaration
Parameters
textBlobglyphs, positions, and their paints’ text size, typeface, and so on
xhorizontal offset applied to blob
yvertical offset applied to blob
paintblend, color, stroking, and so on, used to draw
-
Undocumented
-
Undocumented
Declaration
-
Triggers the immediate execution of all pending draw operations. If
Canvasis associated with GPU surface, resolves all pending GPU operations. IfCanvasis associated with raster surface, has no effect; raster draw operations are never deferred.Declaration
Swift
public func flush() -
Undocumented
Declaration
-
Undocumented
Declaration
-
Sets
Matrixto the identity matrix. Any prior matrix state is overwritten.Declaration
Swift
public func resetMatrix() -
Replaces
Matrixwith matrix. Unlike concat(), any prior matrix state is overwritten.Declaration
Swift
public func setMatrix(_ matrix: Matrix)Parameters
matrixmatrix to copy, replacing existing
Matrixexample: https://fiddle.skia.org/c/@Canvas_setMatrix -
Declaration
Swift
public var totalMatrix: Matrix { get }Return Value
MatrixinCanvasexample: https://fiddle.skia.org/c/@Canvas_getTotalMatrix example: https://fiddle.skia.org/c/@Clip -
Returns the number of saved states, each containing:
Matrixand 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
View on GitHub
Install in Dash
Canvas Class Reference