Some of the trickiest bugs getting Azure canvas working were with radial gradients. Radial gradients in canvases are very flexible, more so than CSS radial gradients or as commonly found in paint software. Other than the spec,which is not particularly user-friendly, I did not find much explanation of how they work. So, here is my understanding.

A gradient is an interpolation between colours across a filled area. The simplest kind of gradient is linear, which is just a linear interpolation. For example, here is a rectangle with a left to right linear gradient from red to green:

A radial gradient interpolates circles, rather than lines, for example here is a simple radial gradient from red to green:

To add clarity, here is the same gradient with the start and end circles added:

The above example is simple because the two circles have the same centre and the 'start' circle is entirely within the 'finish' circle. In addition, there are only two colours involved, the start and end colours. These two issues are pretty much orthogonal, so we'll deal with multiple colours first, and we'll do so in the context of linear gradients, since the issues are the same independent of the type of gradient, and linear ones are simpler.

The start, end, and any intermediate colours are called colour stops. Each gradient must have at least two colour stops - the start and end colours - and may have any number of additional, intermediate colour stops. The syntax for these things is (I've elided the details of gradient creation for now):

var context = canvas.getContext('2d');

var gradient = context.create...Gradient(...);

gradient.addColorStop(0, 'red');

gradient.addColorStop(1, 'green');

context.fillStyle = gradient;

...

The gradient will blend smoothly from red to green. The first parameter to addColorStop can be thought of as the parameter in a parametric equation; 0 is the start of the gradient, and 1 is the end. In fact, the gradient will shade from negative to positive infinity. Outside the start and end colour stops, the colour is extrapolated. For example, here is a gradient fill that shows the gradient outside the 0,1 range; the black lines are at 0 and 1 on the gradient:

Additional colour stops add intermediate colours, for example adding

gradient.addColorStop(0.5, 'blue');

gives a gradient from red to blue to green (again the black lines are at the colour stops):

And here's a more complex example,

gradient.addColorStop(0, 'red');

gradient.addColorStop(0.1, 'blue');

gradient.addColorStop(0.3, 'red');

gradient.addColorStop(1, 'green');

in linear and radial versions:

OK, now back to just two colours, and more complex radial gradients. The syntax to create a radial gradient is:

createRadialGradient(x0, y0, r0, x1, y1, r1)

where *0 define the position and radius of the starting circle (where the gradient parameter is 0), and the *1 arguments define the end circle (where the gradient parameter is 1). If we offset the two circles (but circle 0 is still completely inside circle 1), then we get a skewed version of the simple radial gradient:

But, if the circles are not inside one another, then we get a cone:

Huh?

The gradient is interpolated from positive to negative infinity, just like with linear gradients. In the radial case, you can think of the gradient being drawn by stroking many incremental circles between the start and end circles. The position and radius of the circles are interpolated between x0, y0, r0 at t=0 (where t is the gradient parameters discussed above) and x1, y1, r1 at t=1. The interpolation is extrapolated to larger and larger circles as t tends to positive infinity, and to smaller and smaller circles as the radius disappears to 0, somewhere between t=0 and t=-infinity. This is what gives the cone shape. To illustrate, I have added the circles at t=0 and t=1 and a line along the centres of all the hypothetical circles:

The shading of the gradient is also kind of interesting; the shading is
limited to the cone and is a gradient from the 'left' side of the t=0
circle to the 'left' side of the t=1 circle. In particular, the
gradient extends across the t=0 circle, but the t=1 circle is a solid
colour. That is in contrast to the simple case (where t=0 circle is
entirely inside t=1 circle) where t=0 is a solid colour and t=1 is
shaded. To understand this we need two concepts: that the gradient is
drawn from positive infinity to negative infinity (technically, towards
negative infinity until the radius of circles becomes 0), and that what
is drawn is not overdrawn. In the example, the gradient is drawn
right-to-left, getting less green and more red as we go. The gradient
goes between the left side of the circles because we draw from the right
and do not overdraw. Note that left or right does not make a
difference, we draw from circle 1 to circle 0, so the gradient will go
from the side of circle 1 closest to circle 0, to the side of circle 0
furthest from circle 1.

There is nothing which says that circle 0
must be the smaller circle, so we can create a gradient from the
'opposite' sides of the circles by swapping the definition of the
circles in the createRadialGradient call and the colours of colour stops:

The final case is if the two circles overlap, but neither is entirely contained in the other. We can apply the same understanding as the separate circles case (and of couse, that understanding applies to the simplest case too). In fact the gradient looks similar, just with a more compressed cone:

We can see the cone stretch out as the circles get further apart (in the first example, the circles are just one pixel apart):

And there is the interesting case where the edges of the circles touch. This looks like the case where circle o is inside circle 1, except that the solid blue colour stops at the edge of the circles. One pixel further, and we are back to the simple case:

## 2 comments:

That's pretty neat. I've never really thought about how gradients work and this is a really good description!

"except that the solid blue colour stops"

working with colour gradients must be very hard for our.. ;)

Post a Comment