Canvas
open class Canvas : NSImageView, CustomPlaygroundDisplayConvertible
Carries out the heavy lifting to generate bitmap graphics
-
Returns the bitmap image used for Xcode Playground quick looks; represents current state of the canvas at any given time.
Declaration
Swift
public var playgroundDescription: Any { get } -
Frame rate for animation on this canvas
Declaration
Swift
public var framesPerSecond: Int { get set } -
Keeps track of how many frames have been animated using this particular canvas
Declaration
Swift
public var frameCount: Int -
Default line width for lines drawn using drawLine()
Declaration
Swift
open var defaultLineWidth: Int { get set } -
Line color, default is black
Declaration
Swift
open var lineColor: Color -
Default border width for closed shapes
Declaration
Swift
open var defaultBorderWidth: Int { get set } -
Border color, default is black
Declaration
Swift
open var borderColor: Color -
Fill color, default is black
Declaration
Swift
open var fillColor: Color -
Text color, default is black
Declaration
Swift
open var textColor: Color -
Whether to draw shapes with borders
Declaration
Swift
open var drawShapesWithBorders: Bool -
Whether to draw shapes with fill
Declaration
Swift
open var drawShapesWithFill: Bool -
Undocumented
Declaration
Swift
public let width: Int -
Undocumented
Declaration
Swift
public let height: Int -
Undocumented
Declaration
Swift
public let scale: Int -
Draw in high performance mode. When true, canvas image does not get updated after every draw call. This should generally be kept at the default value of
false.Declaration
Swift
open var highPerformance: Bool { get set } -
Undocumented
Declaration
Swift
public var offscreenRepresentation: NSBitmapImageRep { get } -
Undocumented
Declaration
Swift
required public init?(coder: NSCoder) -
Undocumented
Declaration
Swift
open override func draw(_ dirtyRect: NSRect) -
Undocumented
Declaration
Swift
override open func viewDidMoveToSuperview() -
Draw text beginning at the point specified.
Declaration
Swift
open func drawText(message: String, at: Point, size: Int = 24, kerning: Float = 0.0)Parameters
messageThe text to be drawn on screen.
atText will be drawn starting at this location.
sizeThe size of the text, specified in points.
kerningThe spacing between letters of the text. 0.0 is neutral, negative values draw letters together, positive values move letters further apart.
-
Draw a line segment between the provided points.
Declaration
Parameters
fromStarting position of the line segment.
toEnding position of the line segment.
lineWidthWidth of the line segment.
capStyleThe shape of line segment endpoints (square, rounded, et cetera).
-
Draw an ellipse centred at the point specified.
Declaration
Swift
open func drawEllipse(at: Point, width: Int, height: Int, borderWidth: Int = 0)Parameters
atPoint over which the ellipse will be drawn.
widthHow wide the ellipse will be across its horizontal axis.
heightHow tall the ellipse will be across its vertical axis.
borderWidthHow thick the stroke of the border should be.
-
Draw a rectangle at the specified point and anchor position.
Declaration
Swift
open func drawRectangle(at: Point, width: Int, height: Int, anchoredBy: AnchorPosition = AnchorPosition.bottomLeft, borderWidth: Int = 1)Parameters
atPoint at which the rectangle will be drawn.
anchoredByDraw the rectangle from a point at the rectangle’s bottom left corner, or, the rectangle’s centre.
widthHow wide the rectangle will be across its horizontal axis.
heightHow tall the rectangle will be across its vertical axis.
borderWidthHow thick the stroke of the border should be.
-
Draw a rounded rectangle at the specified point and anchor position.
For the
xRadiusandyRadiusparameters, imagine a circle at each corner of the rectangle.An
xRadiusandyRadiusof 25 each means that the last 25 points of a typical rectangle have been replaced with the rounded edge of a circle with a radius of 25 points.When the
xRadiusandyRadiusvalues are different, you can imagine that the corners of a typical rectangle have been replaced by the rounded edge of an ellipse rather than a circle.For example:
canvas.drawRoundedRectangle(at: Point(x: 50, y: 50),width: 100,height: 75,anchoredBy: .bottomLeft,borderWidth: 2,xRadius: 20,yRadius: 30)… will produce:

Declaration
Swift
open func drawRoundedRectangle(at: Point, width: Int, height: Int, anchoredBy: AnchorPosition = AnchorPosition.bottomLeft, borderWidth: Int = 1, xRadius: Int = 10, yRadius: Int = 10)Parameters
atPoint at which the rectangle will be drawn.
anchoredByDraw the rectangle from a point at the rectangle’s bottom left corner, or, the rectangle’s centre.
widthHow wide the rectangle will be across its horizontal axis.
heightHow tall the rectangle will be across its vertical axis.
borderWidthHow thick the stroke of the border should be.
xRadiusHorizontal size of the rounded corner.
yRadiusVertical size of the rounded corner.
-
Draws a closed polygon from the given vertices.
At least three vertices must be provided.
For example:
var myPoints : [Point] = []myPoints.append(Point(x: 50, y: 50))myPoints.append(Point(x: 100, y: 50))myPoints.append(Point(x: 75, y: 100))canvas.drawCustomShape(with: myPoints)… will produce:

Declaration
Swift
open func drawCustomShape(with vertices: [Point])Parameters
verticesAn array of Point instances defining the vertices of the figure.
-
Rotate the canvas around the origin.
Positive value rotate the canvas clockwise, negative values rotate the canvas counter-clockwise.
Use
drawAxes()after invoking this method to understand how the canvas is changed by rotation.Declaration
Swift
open func rotate(by provided: Degrees)Parameters
byA value in degrees by which to rotate the canvas.
-
Translate the origin of the canvas to a new location.
Translating to (50, 100) would move the origin 50 steps “to the right” along the horizontal axis, and 100 steps “up” along the vertical axis.
Note that “to the right” and “up” are relative, since the canvas may be rotated.
Declaration
Swift
open func translate(to: Point)Parameters
toThe point that you wish to move the origin to.
-
Save the current state of the canvas (location of origin, rotation of canvas).
Works the same way as pushMatrix() in Processing.
From the Processing documentation:
“Pushes the current transformation matrix onto the matrix stack. Understanding pushMatrix() and popMatrix() requires understanding the concept of a matrix stack. The pushMatrix() function saves the current coordinate system to the stack and popMatrix() restores the prior coordinate system. pushMatrix() and popMatrix() are used in conjuction with the other transformation functions and may be embedded to control the scope of the transformations.”
Declaration
Swift
open func saveState() -
Restore a prior state of the canvas (location of origin, rotation of canvas).
Works the same way as popMatrix() in Processing.
From the Processing documentation:
“Pops the current transformation matrix off the matrix stack. Understanding pushing and popping requires understanding the concept of a matrix stack. The pushMatrix() function saves the current coordinate system to the stack and popMatrix() restores the prior coordinate system. pushMatrix() and popMatrix() are used in conjuction with the other transformation functions and may be embedded to control the scope of the transformations.”
Declaration
Swift
open func restoreState() -
Copies the contents of the canvas to the clipboard.
You can then paste the image into any other program, for example, Preview, and from there, save to disk, print, share with others, et cetera.
Declaration
Swift
open func copyToClipboard() -
Draws horizontal and vertical axes based on the current location of the origin and rotation of the canvas.
For example:

Declaration
Swift
open func drawAxes()
View on GitHub
Canvas Class Reference