Gradients
An even more expressive way to work with colors is via gradients. Gradients allow us to blend one color into another, creating some stunning graphical effects.
Fabric supports gradients via the Gradient
class.
In the above example, we create a circle at 100,100 location, with 50px radius. We then set its fill to a gradient that spans throughout entire height of that circle, from black to white.
The argument passed to a method is an options object, which expects 2 coordinate pairs (x1, y1 and x2, y2), as well as “colorStops” object. Coordinates specify where gradient starts and where it ends. colorStops specify which colors a gradient is made of. You can define as many color stops as you want, as long as they range from 0 to 1 (e.g. 0, 0.1, 0.3, 0.5, 0.75, 1). 0 represents beginning of a gradient, 1 — its end.
Coordinates are relative to object top left corner, so the highest point of the circle is 0
, and the lowest point is circle.height
. setGradient
calculates width coordinates (x1, x2) in the same way.
Here’s an example of left-to-right red-blue gradient:
And here’s a 5-stops rainbow gradient, with colors spanning at even 20% intervals:
Which cool versions can you come up with?