本节我们学习 SVG 中的渐变。首先我们需要弄清楚什么是渐变,SVG 中的渐变可以理解为一种颜色到另外一种颜色的过渡,而这种颜色的过渡又分为两种形式,即线性渐变和径向渐变。本节我们会先学习线性渐变。

什么是线性渐变

我们先来看线性渐变,线性渐变就是直线的渐变,例如将不同的颜色从左向右进行渐变。我们可以使用 <linearGradient> 元素来定义线性渐变,这个元素必须嵌套在 <defs> 元素内部,<defs> 元素可以对渐变这类元素进行定义。

示例:

我们先来看一个线性渐变的例子:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>SVG学习(9xkd.com)</title>
        <link rel="styleSheet" type="text/css" href="./style.css">
    </head>
    <body>
        <svg width="500" height="150">
            <defs>
                <linearGradient id="line" x1="0%" y1="0%" x2="100%" y2="0%">
                    <stop offset="0%"  style="stop-color:pink; "/>
                    <stop offset="50%"  style="stop-color:bisque; "/>
                    <stop offset="100%"  style="stop-color:aqua; "/>
                </linearGradient>
            </defs>
            <rect fill="url(#line)" x="10" y="10" width="200" height="100"/>
        </svg>
    </body>
</html>

在浏览器中的演示效果: 上述代码中,创建了一个渐变的矩形,其中 <linearGradient> 元素中的 id 属性是很重要的,可以为渐变定义一个唯一的名称,然后用到需要进行渐变的图形中。

线性渐变的属性

<linearGradient> 元素中有如下几个属性:

id 渐变色指定唯一的名称,便于引用该渐变色
x1 定义线起点的 x 坐标
y1 定义线起点的 y 坐标
x2 定义线终点的 x 坐标
y2 定义线终点的 y 坐标
gradientUnits 定义用于在渐变元素上指定的属性的坐标系,可用值有 userSpaceOnUseobjectBoundingBox(默认值)
gradientTransform 包含从渐变坐标系到目标坐标系的可选附加转换的定义
spreadMethod 确定如何填充超出定义的渐变边的形状,可选值有pad、reflect、repeat
  • x1x2 不同,而 y1y2 相等时,可创建水平渐变。
  • x1x2 相等,而 y1y2 不同时,可创建垂直渐变。
  • x1x2 不同,且 y1y2 不同时,可创建角形渐变。

stop元素

<stop> 元素可以是 <linearGradient><radialGradient> 元素的子元素,用于定义渐变的成员。可以同时定义多个 <stop> 元素,一个 <stop> 元素可以定义一个颜色。

元素中的几个属性如下所示:

  • offset 属性:用于定义该成员色的作用范围,取值为 0% 到 100% 之间(或者0 到 1)。一般将第一种颜色设置为 0%,最后一种颜色设置为 100%。
  • stop-color 属性:用于定义渐变成员的颜色。
  • stop-opacity 属性:用于定义渐变成员的透明度。

垂直渐变

上面那个示例中,我们定义了一个水平渐变的矩形,然后我们再来看一下如何设置垂直渐变。垂直渐变也很简单,只需要将 y1y2 设置不同的值即可。

示例:

设置一个垂直渐变的矩形:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>SVG学习(9xkd.com)</title>
        <link rel="styleSheet" type="text/css" href="./style.css">
    </head>
    <body>
        <svg width="500" height="200">
            <defs>
                <linearGradient id="line" x1="0%" y1="0%" x2="0%" y2="100%">
                    <stop offset="0%"  style="stop-color:pink; "/>
                    <stop offset="50%"  style="stop-color:bisque; "/>
                    <stop offset="100%"  style="stop-color:aqua; "/>
                </linearGradient>
            </defs>
            <rect fill="url(#line)" x="10" y="10" width="200" height="200"/>
        </svg>
    </body>
</html>

在浏览器中的演示效果:

角形渐变

角形渐变就是从对角线进行渐变,需要将 x1x2 设置不同的值, y1y2 设置不同的值。

示例:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>SVG学习(9xkd.com)</title>
        <link rel="styleSheet" type="text/css" href="./style.css">
    </head>
    <body>
        <svg width="500" height="200">
            <defs>
                <linearGradient id="line" x1="0%" y1="0%" x2="100%" y2="100%">
                    <stop offset="0%"  style="stop-color:rgb(253, 80, 67); "/>
                    <stop offset="30%"  style="stop-color:rgb(64, 135, 241); "/>
                    <stop offset="70%"  style="stop-color:rgb(235, 238, 67); "/>
                    <stop offset="100%"  style="stop-color:rgb(243, 87, 217); "/>
                </linearGradient>
            </defs>
            <rect fill="url(#line)" x="10" y="10" width="250" height="200"/>
        </svg>
    </body>
</html>

在浏览器中的演示效果:

04-20 03:00