svg 使用图片做背景

34 篇文章 0 订阅

SVG Fill Patterns


By Jakob Jenkov
 
Connect with me:  
   Jenkov.com - News
 

SVG fill patterns are used to fill a shape with a pattern made up from images. This pattern can be made up from SVG images (shapes) or from bitmap images.

SVG fill patterns look like what you are used to from Photoshop etc. being called "tiles".

Table of contents:

Fill Pattern Example

Here is a simple svg fill pattern example:

<defs>
  <pattern id="pattern1"
           x="10" y="10" width="20" height="20"
           patternUnits="userSpaceOnUse" >

      <circle cx="10" cy="10" r="10" style="stroke: none; fill: #0000ff" />

  </pattern>
</defs>

<rect x="10" y="10" width="100" height="100"
    style="stroke: #000000; fill: url(#pattern1);" />    

First a <pattern> element is defined inside a <defs> element. The pattern contains a circle element. Thiscircle element will be used as a fill pattern.

Second, a <rect> element is declared which references the <pattern> element ID from its style attribute, in thefill CSS property.

Here is the resulting image:

Notice how the circle defined in the <pattern> element is used as fill for the rectangle. Notice also how the circle is repeated over and over again, from left to right, and top to bottom.

X, Y, Width and Height

The x and y attributes of the <pattern> element defines how far into the shape in the <pattern> element the pattern starts.

The width and height attributes of the <pattern> element defines the width and height of the pattern.

Here is the example from the beginning but with x and y set to 0:

<defs>
  <pattern id="pattern2"
           x="0" y="0" width="20" height="20"
           patternUnits="userSpaceOnUse" >

      <circle cx="10" cy="10" r="10" style="stroke: none; fill: #0000ff" />

  </pattern>
</defs>

<rect x="10" y="10" width="100" height="100"
    style="stroke: #000000; fill: url(#pattern1);" />

Here is the resulting image:

Notice how the pattern now starts in the middle of the circle (at the top and left edge of the rectangle). When you create your own fill patterns you will have to play around with the x and y values to achieve the effect you want.

The width and height attributes sets the width and height of the pattern.

In the previous examples width and height were both set to 20, which is the diameter of the circle. Thus the circles repeated over and over again with no space in between.

In this example the width of the pattern is set to 25 instead of 20. Notice that there is now a 5 pixel space between the circles horizontally.

Here is an example with height set to 25 too:

Here is the same example but with x and y set to 10 (the center of the circle inside the <pattern> element):

Now the pattern starts with a full circle, but still has the extra vertical and horizontal space.

Nested Patterns

It is possible to nest fill patterns, so that a fill pattern internally uses another fill pattern. Here is an example that has a rectangle which uses a circle as fill pattern. The circle internally uses a rectangle as fill pattern.

<defs>
    <pattern id="innerPattern"
             x="3" y="3" width="9" height="9"
             patternUnits="userSpaceOnUse"
            >
        <rect x="0" y="0" width="6" height="6"
              style="stroke: none; fill: #ff0000;" />

    </pattern>

    <pattern id="outerPattern"
             x="12" y="12" width="25" height="25"
             patternUnits="userSpaceOnUse"
            >

        <circle cx="10" cy="10" r="10"
            style="stroke: #0000ff; fill: url(#innerPattern)" />

    </pattern>
</defs>

<rect x="10" y="10" width="100" height="100"
      style="stroke: #000000; fill: url(#outerPattern);" />
    

Here is the resulting image:

As you can see, the outer rectangle is now filled with circles, which again are filled with rectangles.

Transforming Patterns

You can transform fill patterns using the standard SVG transformation functions. You do so using thepatternTransform attribute. Here is an SVG pattern transformation example:

<defs>
  <pattern id="transformedPattern"
         x="10" y="10" width="20" height="20"
         patternUnits="userSpaceOnUse"
         patternTransform="rotate(35)"
        >

    <circle cx="10" cy="10" r="10" style="stroke: none; fill: #0000ff" />

  </pattern>
</defs>

<rect x="10" y="10" width="100" height="100"
      style="stroke: #000000;
             fill: url(#transformedPattern);" />

This example defines a simple pattern which is rotated 35 degrees before being used as fill pattern in a rectangle. Here is the resulting image:

 

NextNext : SVG Clip Path


转自:http://tutorials.jenkov.com/svg/fill-patterns.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值