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
  • 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 }
  • Creates a canvas object that can be drawn upon.

    Declaration

    Swift

    public init(width: Int = 300, height: Int = 200, quality: Quality = Quality.Standard)

    Parameters

    width

    Width of the canvas

    height

    Height of the canvas

    qualty

    When generating output for printing, use High or Ultra.

  • Draw text beginning at the point specified.

    Declaration

    Swift

    open func drawText(message: String, at: Point, size: Int = 24, kerning: Float = 0.0)

    Parameters

    message

    The text to be drawn on screen.

    at

    Text will be drawn starting at this location.

    size

    The size of the text, specified in points.

    kerning

    The 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

    Swift

    open func drawLine(from: Point, to: Point, lineWidth: Int = 0, capStyle: NSBezierPath.LineCapStyle = NSBezierPath.LineCapStyle.square)

    Parameters

    from

    Starting position of the line segment.

    to

    Ending position of the line segment.

    lineWidth

    Width of the line segment.

    capStyle

    The 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

    at

    Point over which the ellipse will be drawn.

    width

    How wide the ellipse will be across its horizontal axis.

    height

    How tall the ellipse will be across its vertical axis.

    borderWidth

    How 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

    at

    Point at which the rectangle will be drawn.

    anchoredBy

    Draw the rectangle from a point at the rectangle’s bottom left corner, or, the rectangle’s centre.

    width

    How wide the rectangle will be across its horizontal axis.

    height

    How tall the rectangle will be across its vertical axis.

    borderWidth

    How thick the stroke of the border should be.

  • Draw a rounded rectangle at the specified point and anchor position.

    For the xRadius and yRadius parameters, imagine a circle at each corner of the rectangle.

    An xRadius and yRadius of 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 xRadius and yRadius values 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:

    drawCustomShape

    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

    at

    Point at which the rectangle will be drawn.

    anchoredBy

    Draw the rectangle from a point at the rectangle’s bottom left corner, or, the rectangle’s centre.

    width

    How wide the rectangle will be across its horizontal axis.

    height

    How tall the rectangle will be across its vertical axis.

    borderWidth

    How thick the stroke of the border should be.

    xRadius

    Horizontal size of the rounded corner.

    yRadius

    Vertical 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:

    drawCustomShape

    Declaration

    Swift

    open func drawCustomShape(with vertices: [Point])

    Parameters

    vertices

    An 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

    by

    A 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

    to

    The 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:

    axes

    Declaration

    Swift

    open func drawAxes()