Gradients
The createGradient()
method returns a canvas gradient object, which is used as a fill or stroke style.
Gradient colors are defined using c1
, c2
, c3
, and so on. Optional color stops are defined using s1
, s2
, s3
, and so on (and can be any number from 0
through 1
).
Linear
Radial
Radial gradients are created when the r1
or r2
properties are defined.
Animating Layers with Gradients
There are cases where you may wish to animate a layer that has a gradient fill/stroke. However, animating the layer’s position or size will leave the gradient unaffected, because gradients are relative to the canvas.
To demonstrate this behavior, consider the following example:
To solve this problem, by using a function as the layer’s fill/stroke style, you can achieve a gradient which moves with its layer. Note that the below example assumes that the gradient’s coordinates are relative to the layer’s size and position.
Notes
Color stops are optional, and are measured from 0 to 1.