This example shows how to create gradient fills for a shape. By default, the fill
attribute of a shape instance will generate a solid fill for the shape. Setting the type
property on the fill
attribute to linear
or radial
will create corresponding
gradient fill. Each gradient type has its own set of properties for defining the fill. Both share the stop
property. The stop
property defines the colors and their opacity and position in a
gradient fill. It is an array of objects containing the following information.
Linear gradients included a rotation property which defines the direction of the gradient. (by default, linear gradients go left to right)
Radial gradients include the following additional properties:
Note: This property currently has no effect on Android or IE 6 - 8.
Note: This property currently has no effect on Android or IE 6 - 8.
<div id="mygraphiccontainer"></div>
#mygraphiccontainer { position: relative; width: 700px; height:400px; }
Add a rectangle with a linear gradient.
var mygraphic = new Y.Graphic({render:"#mygraphiccontainer"}); var myrect = mygraphic.addShape({ type: "rect", stroke: { color:"#000", weight: 1 }, fill: { type: "linear", rotation: 271, stops: [ {color: "#ff6666", opacity: 1, offset: 0}, {color: "#00ffff", opacity: 1, offset: 0.5}, {color: "#000000", opacity: 1, offset: 1} ] }, width:685, height:400 });
Add a circle with a radial gradient.
var mycircle = mygraphic.addShape({ type: "circle", radius: 50, x: 0, y: 0, fill: { type: "radial", stops: [ {color: "#ff6666", offset: 0}, {color: "#00ffff", offset: 0.4}, {color: "#000000", offset: 0.7} ], fx: 0.1, fy: 0.3, r: 0.5 }, stroke: { color: "#000" } });
<div id="mygraphiccontainer"></div> <script> YUI().use('graphics', function (Y) { var mygraphic = new Y.Graphic({render:"#mygraphiccontainer"}); var myrect = mygraphic.addShape({ type: "rect", stroke: { color:"#000", weight: 1 }, fill: { type: "linear", rotation: 271, stops: [ {color: "#ff6666", opacity: 1, offset: 0}, {color: "#00ffff", opacity: 1, offset: 0.5}, {color: "#000000", opacity: 1, offset: 1} ] }, width:685, height:400 }); var mycircle = mygraphic.addShape({ type: "circle", radius: 50, x: 0, y: 0, fill: { type: "radial", stops: [ {color: "#ff6666", offset: 0}, {color: "#00ffff", offset: 0.4}, {color: "#000000", offset: 0.7} ], fx: 0.1, fy: 0.3, r: 0.5 }, stroke: { color: "#000" } }); }); </script>