Flash Scripted Conic Angle Gradient
One of the quirkier aspects of Flash is its lack of a 'conic' gradient fill. The conic gradient has been a mainstay of drawing apps like CorelDraw and Photoshop (where it's called an "Angle Gradient") for ages but Flash still hasn't implemented this basic feature. Go ahead and check... it's not there.
NOTE: This tutorial uses some of the code and concepts from my previous Color Gradient and Circle Drawing tutorials so I highly recommend checking them out for a more in-depth explanation of the following code.
You can't make Flash fill a shape with a conic gradient but you can create a similar effect by masking a 'pie-chart-esque' shape composed of small multicolored slices. Of course, the more slices your pie chart has, the smoother your gradient will appear. For example:
The graphic above was created with the following code:
//
// "FadeHexes" accepts an array of hexadecimal colors
// separated by numbers specifying how many new colors
// should be generated between the adjacent colors.
// For example:
// [Red, 1, Black, 3, White] yields...
// [Red, DARK-RED, Black, DARK-GRAY, MIDDLE-GRAY, LIGHT-GRAY, White].
function fadeHexes (arry){
var len = arry.length-1;
var newArry = [];
for(var i=0; i<len; i+=2){
var hex = arry[i];
var hex2 = arry[i+2];
newArry.push(hex);
var r = hex >> 16;
var g = hex >> 8 & 0xFF;
var b = hex & 0xFF;
var rd = (hex2 >> 16)-r;
var gd = (hex2 >> 8 & 0xFF)-g;
var bd = (hex2 & 0xFF)-b;
var steps = arry[i+1]+1;
for (var j=1; j<steps; j++){
var ratio = j/steps;
newArry.push((r+rd*ratio)<<16 | (g+gd*ratio)<<8 | (b+bd*ratio));
}
}
newArry.push(arry[len]);
return newArry;
}
//
//
// The "ConicPattern" function reads the 'colors' array and draws an
// 'N' sided polygon, one side for each color. The pie-wedge formed
// by each side and the center point is filled with the specified color.
/////
// X = Center of the conic pattern on the X axis.
// Y = Center of the conic pattern on the Y axis.
// XR = Width of the radius on the X axis.
// YR = Width of the radius on the Y axis.
// ANGLE = Angle of the first color (0= 3 O'clock), (90/360= 6 O'clock), (-60/360= 1 O'clock).
// COLORS = Array of colors used to fill each slice of the pie. 1 slice for each color.
function conicGradient(x, y, xr, yr, angle, colors){
//
// Record the length of the colors array.
var len = colors.length;
//
// Convert the initial angle of rotation to radians.
var startRadians = angle * 2 * Math.PI;
//
// Determine the size of each pie slice.
var stepRadians = (2 * Math.PI)/len;
//
// Find the first point on the circumference.
var oldX = x + Math.cos(startRadians) * xr;
var oldY = y + Math.sin(startRadians) * yr;
//
// Prepare some variables for future use.
var newX, newY;
for(var i=0; i<=len; i++){
//
// Determine the angle of the next point on the circumference.
newRadians = startRadians + stepRadians*(i+1);
//
// Locate the next point on the circumference.
newX = x + Math.cos(newRadians) * xr;
newY = y + Math.sin(newRadians) * yr;
//
// Get color of pie slice from provided list.
beginFill(colors[i]);
//
// Draw wedge shape.
with (this){
moveTo(oldX, oldY);
lineTo(x, y);
lineTo(newX, newY);
lineTo(oldX, oldY);
endFill();
}
//
// Store new point cordinates in old variables
oldX = newX;
oldY = newY;
}
}
//
// Generate 4 lists of hex colors with different
// quantities of "new" colors between each "key" color.
var colors1 = fadeHexes ([0xFFFF00, 4, 0x00FFFF, 4, 0xFF00FF, 4, 0xFFFF00]);
var colors2 = fadeHexes ([0xFFFF00, 8, 0x00FFFF, 8, 0xFF00FF, 8, 0xFFFF00]);
var colors3 = fadeHexes ([0xFFFF00, 16, 0x00FFFF, 16, 0xFF00FF, 16, 0xFFFF00]);
var colors4 = fadeHexes ([0xFFFF00, 32, 0x00FFFF, 32, 0xFF00FF, 32, 0xFFFF00]);
//
// Draw 4 conic gradients using the colors generated above.
conicGradient( 70, 70, 50, 50, 0, colors1);
conicGradient(190, 70, 50, 50, 0, colors2);
conicGradient(310, 70, 50, 50, 0, colors3);
conicGradient(430, 70, 50, 50, 0, colors4);
//
//
You may have noticed that the color yellow is duplicated in the above example because the first and last colors in the "colors" array are the same (0xFFFF00). To get the gradient to loop perfectly you can pop off the last element of the colors array like so:
//
var colors1 = fadeHexes ([0xFFFF00, 4, 0x00FFFF, 4, 0xFF00FF, 4, 0xFFFF00]);
colors1.pop();
conicGradient( 70, 70, 50, 50, 0, colors1);
//
//
So there you have it. If you ever need a conic (or angle) gradient you'll know how to make one on your own. I hope you find it handy.
Hey. Very nice code. I modified it a bit to also calculate alpha vlaues. I am using it to emulate an oldish radar scope so all I want is for it to have a zero alpha that fades into something black with alpha 0.6ish. Works like a dream 🙂
[…] and I don’t understand it enough to repair it.The code for a Conic Angle Gradient is here: on http://www.pixelwit.com Does anyone have any ideas or suggestions or even solutions on how to accomplish this? […]
Greetings to you.
Great work.Fine&pleasing.
Nice. I’ve only just found your site, you have some interesting posts, well done!
Wow, very cool. Thanks for sharing.