Friday, November 3, 2006
Applying background gradients to VBox, HBox, Canvas etc.
Here's a little custom border class I created called SimpleGradientBorder which allows you to use non-solid fills with Flex 2 containers. Here's how it looks (the Container shown here is actually a Canvas):

By setting the border-skin style of a Container to the SimpleGradientBorder class, it's possible to fill Box/Canvas containers with a background gradient. This is just a basic example which only supports two fill colors and always draws a vertical gradient but it is actually pretty easy to modify this example if you need more advanced drawing routines. Note that you must set the border-style style attribute to "solid" on the Container, otherwise the background will not be drawn!
Dirk
Comments
Dirk, thanks a lot for the post. This showed me what I was doing differently (and apparently wrong) in my skinning.
Posted By Brian Deitte / Posted At 11/3/06 10:05 PM
Very useful Dirk. Thanks!
Posted By Marcel Fahle / Posted At 11/5/06 9:25 PM
Been looking for a way to do this. Thanks so much for the post.
Posted By Tom Sobut / Posted At 12/5/06 6:03 AM
Brilliant, this is a life saver. Thanks for sharing it.
Posted By Stefan Richter / Posted At 1/21/07 10:34 PM
Dirk, thanks for this example. However, it does not work for Canvas because the style fillColors does not exist for it.
Do you have any idea ?
Posted By Corentin J. / Posted At 1/31/07 3:16 PM
Great work! Thanks for the code. ~N
Posted By Nolan Love / Posted At 6/11/07 4:02 AM
Dirk, Awesome code. One question. is there a way to set an offset so that you can adjust the midline between the two gradient colors? For example, I'd like my gradient to appear just near the bottom, rather than the from top to bottom. Cannot discern how to do so in your code.
Posted By Andrew / Posted At 6/12/07 8:10 PM
Nice find! I wondered why you would extend HaloBorder and not just RectangularBorder.
@Andrew
you need to change the ratios ([0, 255]).
g.beginGradientFill("linear", fillColors, [1, 1], [0, 255], m);
take a look at the beginGradientFill method
Posted By Tink / Posted At 8/7/07 6:24 PM
Tink, Thanks. I figured that our right after posting. I modified the class to support using css properties to set the ratios. I created another to handle radial gradiants as well which we use to give a "shadowed corner" look.
Posted By Andrew / Posted At 8/7/07 6:59 PM
thanks so much for this. Very helpful. Is there a way to make the background alpha change? It seems to be solid the whole way through.
Posted By marissa / Posted At 2/11/08 7:23 PM
I just managed to mash up your gradient canvas with corners, and the more flexible GradientCanvas.mxml component on <a href="http://afoucal.free.fr/index.php/2008/04/29/flex-gradientcanvas-v2-more-background-gradients-more-borders/">Flex"i"MMi</a> which didn't have the ability to round corners to make an especially nice component. Thanks to both of you!
Posted By Adrian / Posted At 8/21/08 8:16 AM
I just managed to mash up your gradient canvas with corners, and the more flexible GradientCanvas.mxml component on <a href="http://afoucal.free.fr/index.php/2008/04/29/flex-gradientcanvas-v2-more-background-gradients-more-borders/">Flex"i"MMi</a> which didn't have the ability to round corners to make an especially nice component. Thanks to both of you!
Posted By Adrian / Posted At 8/21/08 8:16 AM
Posted By darkandlong / Posted At 10/1/08 12:58 PM
Posted By kevin / Posted At 12/17/08 2:42 PM