svg pattern
时间: 2023-06-28 08:13:47 浏览: 54
SVG patterns are a way to fill a shape or area with a repeated design or image. Patterns are defined using the `<pattern>` element and can contain any number of other SVG elements, such as `<rect>`, `<line>`, or even other `<pattern>` elements.
Here's an example of how to create a simple pattern in SVG:
```
<svg>
<defs>
<pattern id="checkerboard" width="20" height="20" patternUnits="userSpaceOnUse">
<rect x="0" y="0" width="10" height="10" fill="#fff"></rect>
<rect x="10" y="10" width="10" height="10" fill="#fff"></rect>
<rect x="0" y="10" width="10" height="10" fill="#000"></rect>
<rect x="10" y="0" width="10" height="10" fill="#000"></rect>
</pattern>
</defs>
<rect x="50" y="50" width="200" height="200" fill="url(#checkerboard)"></rect>
</svg>
```
In this example, we define a pattern called `checkerboard` that is 20 pixels wide and 20 pixels tall (`width` and `height` attributes). We set the `patternUnits` attribute to `userSpaceOnUse` so that the pattern is scaled based on the size of the object it is applied to.
Inside the `<pattern>` element, we define four `<rect>` elements that alternate between white and black, creating a checkerboard pattern.
Finally, we apply the pattern to a `<rect>` element by setting its `fill` attribute to `"url(#checkerboard)"`.
This is just a basic example of what you can do with SVG patterns. There are many other options and attributes you can use to create more complex patterns and designs.