Class
Gradient
public class Gradient : CanvasIdentifiedObject
A Gradient
is used to create a FillStyle
for filling various objects.
Gradient
s have two flavors:
- Linear
- Radial
Usage:
There are several steps required to use a gradient:
- Create the Gradient object
- Add two or more ColorStops
- Setup the Gradient object
- Use the Gradient to initialize a StrokeStyle or FillStyle
- Because the Gradient is a CanvasIdentifiedObject, be sure it
isReady
prior to rendering - Render the associated FillStyle prior to the object(s) to be filled with the gradient
Example:
var gradient : Gradient
init() {
gradient = Gradient(mode:.linear(start:Point(x:0, y:60), end:Point(x:320, y:60)))
gradient.addColorStop(ColorStop(position:0.0, color:Color(.red)))
gradient.addColorStop(ColorStop(position:0.25, color:Color(.yellow)))
gradient.addColorStop(ColorStop(position:0.5, color:Color(.green)))
gradient.addColorStop(ColorStop(position:0.5, color:Color(.green)))
gradient.addColorStop(ColorStop(position:0.5, color:Color(.green)))
}
override func setup(canvas:Canvas) {
canvas.setup(gradient)
}
override func render(canvas:Canvas) {
if gradient.isReady {
let fillStyle = FillStyle(gradient:gradient)
canvas.render(fillStyle, rectangle)
}
}
Relationships
Nested Types
Gradient.Mode
The
Mode
is used to determine whether this is alinear
orradial
Gradient
.
Superclass
CanvasIdentifiedObject
Initializers
init(mode:)
public init(mode:Mode)
Creates a new Gradient
Parameters
Name | Type | Description |
---|---|---|
mode | Mode |
Specifies the |
Properties
mode
public let mode : Mode
colorStops
public private(set) var colorStops : [ColorStop]
Maintains a list of colorStops for this Gradient.
NB: This cannot be changed after setup() has been invoked.
Methods
addColorStop(_:)
public func addColorStop(_ colorStop:ColorStop)
Adds a ColorStop
to the Gradient
.
Parameters
Name | Type | Description |
---|---|---|
colorStop | ColorStop |
The |