Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify svg linear gradient in terms of an angle

Tags:

css

gradient

svg

I'd like to specify an SVG linear gradient in a way that exactly duplicates CSS linear-gradient behavior. In a CSS gradient, for example, you may specify that a gradient start and end at the top left and bottom right of a box respectively. When a box resizes, the background gradient adapts automatically to the new size.

In my first attempt, I duplicated a CSS linear-gradient with SVG, by specifying an angle and by calculating the x1,y1,x2,y2 coordinates from the box size. But if the box is resized, the angle of the gradient does not change and is no longer correct. (I would have to recalc all the coordinates).

My next attempt was to use a transform to rotate the gradient. Here's some code:

<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
  <linearGradient id="g1" gradientUnits="userSpaceOnUse" 
      gradientTransform="rotate(-45 150 50)">
    <stop stop-color="#FF0000" offset="0"/>
    <stop stop-color="#00FF00" offset="0.5"/>
    <stop stop-color="#0000FF" offset="1"/>
  </linearGradient>
  <rect x="0" y="0" width="100%" height="100%" fill="url(#g1)" />
</svg>

Now, this works for a box of size (300,100) but you'll see that I'm having to specify absolute values for the centre of rotation (150,50).

Can I specify the centre in terms of a percentage? In the end I want the angle of the gradient to adapt to the dimensions of the box.

like image 685
Jules Avatar asked Feb 24 '12 16:02

Jules


People also ask

How do you find the angle of a linear gradient?

How to set the Angle of a Linear Gradient. You can set the angle of the gradient line so that the gradient runs along that angle. You can specify this with an angle value (e.g. 45deg , 90deg , 180deg ) or with one of the keywords for specifying the angle (such as to top , to bottom , to top left , etc).

How do you do a linear gradient in SVG?

Linear Gradient. Linear gradients change along a straight line. To insert one, you create a <linearGradient> node inside the definitions section of your SVG file.

Can SVG handle gradients?

SVG provides for two types of gradients: linear gradients and radial gradients. Once defined, gradients are then referenced using 'fill' or 'stroke' properties on a given graphics element to indicate that the given element shall be filled or stroked with the referenced gradient.

Which tag is used to define the linear gradient?

The <linearGradient> element is used to define a linear gradient. The <linearGradient> element must be nested within a <defs> tag. The <defs> tag is short for definitions and contains definition of special elements (such as gradients).


1 Answers

SVG only allows gradient transform rotation origins to be specified in terms of absolute coordinates. You will need to set the rotation origin dynamically with JavaScript in order to do what I think you're looking to do: which is to rotate the gradient, but also have the color stops be evenly distributed within the containing box.

like image 154
Michael Mullany Avatar answered Nov 15 '22 05:11

Michael Mullany