XML technologies / SVG / Filters, transformations and gradients / Defining and assigning gradients / Defining a radial gradient

(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)

Different gradient types use different tags. Enclose their definitions in a <defs> tag at the document’s beginning, as the SVG standard recommends.

For a radial gradient, use the <radialGradient> tag. Name it with the id attribute so you can assign it to an object. The attributes cx, cy and r define the gradient’s structure. cx and cy set its position within the object. The parameters fx and fy set its center, the 0% position. Absolute or percentage values are allowed; the example uses percentages to position the center relative to the object’s size. The attribute r sets the gradient’s radius.

Inside this tag, use the <stop> tag to specify color and position. The gradient’s center is at 0% and its beginning at 100%. For a red-to-white gradient, set red at 100% and white at 0%. Define intermediate colors if needed; otherwise the two blend evenly.

The <stop> tag has these attributes for properties at each position:

The instruction

<stop offset="0%" style="stop-color:rgb(255,255,255);stop-opacity:1"/>

sets the gradient’s center to white with 100% opacity.

<?xml version="1.0" standalone="no"?>
...
<svg width="500" height="500">
  <radialGradient id="runderVerlauf" cx="50%" cy="90%" r="60%" fx="60%" fy="80%">
    <stop offset="0%" style="stop-color:rgb(255,150,150);stop-opacity:1"/>
    <stop offset="1%" style="stop-color:rgb(255,128,128);stop-opacity:1"/>
    <stop offset="50%" style="stop-color:rgb(255,0,0);stop-opacity:1"/>
  </radialGradient>
</svg>

   

 

 

 

back to top