Question

Is there a way to make an elliptical gradient in SVG?

I tried the suggested code below, but it just displays a red ellipse, not a gradient:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20001102//EN"
 "http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd">

<svg width="100%" height="100%" 
    xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     xmlns:ev="http://www.w3.org/2001/xml-events" >

  <defs>
    <radialGradient id="gradientDefinition"
      gradientUnits="userSpaceOnUse">
      <stop stop-color="yellow" offset="0%" />
      <stop stop-color="red" offset="100%" />
    </radialGradient>
  </defs>

  <ellipse cx="250" cy="150" rx="200" ry="100" stroke="white"
    stroke-width="1" stroke-dasharray="1 1 1 1"
    style="fill:url(#gradientDefinition)" />
</svg>

I want an elliptical gradient, not a circular gradient inside an ellipse.

Was it helpful?

Solution

To summarize the findings from earlier, it appears that removing the gradientUnits="userSpaceOnUse" attribute and value pair from the <radialGradient> tag allows for the radial gradient to become (or at least appear to become) elliptical in shape. Also, adding stop-opacity attributes and values to each <stop> tag allows for the elliptical gradient effect to be more easily seen (at least for demonstration purposes.)

Here is the code with the above changes made:

<defs>
    <radialGradient id="gradientDefinition" >
        <stop stop-color="yellow" offset="0%" stop-opacity="0" />
        <stop stop-color="red" offset="100%" stop-opacity="1" />
    </radialGradient>
</defs>

To show that this code appears to work:

see: elliptical radialGradient vs circular radialGradient

There is also a tutorial online that appears to provide similar behavior for a similar elliptical gradient approach and the results from that tutorial can be found in this jsFiddle.

Note: If this approach does not work for your purposes, there may be some other, better approach (possibly having to do with gradient transformations, or something similar...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top