Simple fill pattern in svg : diagonal hatching

后端 未结 11 1831
轮回少年
轮回少年 2020-12-04 08:28

How would I fill an SVG shape, not with a single colour, an image or a gradient, but with a hatching pattern, diagonal if possible.

It\'s been 2 hours and I\'ve foun

相关标签:
11条回答
  • 2020-12-04 09:10

    This code from http://bl.ocks.org/jfsiii/7772281 seems very clean and reusable:

    svg {
      width: 500px;
      height: 500px;
    }
    
    rect.hbar {
      mask: url(#mask-stripe)
    }
    
    .thing-1 {
      fill: blue;
    }
    
    
    .thing-2 {
      fill: green;
    }
        <!DOCTYPE html>
        <html>
          <head>
            <meta charset=utf-8 />
            <title>SVG colored patterns via mask</title>
          </head>
          <body>
            <svg>
              <defs>
                <pattern id="pattern-stripe" 
                  width="4" height="4" 
                  patternUnits="userSpaceOnUse"
                  patternTransform="rotate(45)">
                  <rect width="2" height="4" transform="translate(0,0)" fill="white"></rect>
                </pattern>
                <mask id="mask-stripe">
                  <rect x="0" y="0" width="100%" height="100%" fill="url(#pattern-stripe)" />
                </mask>      
              </defs>
        
              <!-- bar chart -->
              <rect class="hbar thing-2" x="0" y="0" width="50" height="100"></rect>
              <rect class="hbar thing-2" x="51" y="50" width="50" height="50"></rect>
              <rect class="hbar thing-2" x="102" y="25" width="50" height="75"></rect>
              
              <!-- horizontal bar chart -->
              <rect class="hbar thing-1" x="0" y="200" width="10" height="50"></rect>
              <rect class="hbar thing-1" x="0" y="251" width="123" height="50"></rect>
              <rect class="hbar thing-1" x="0" y="302" width="41" height="50"></rect>
              
            </svg>
          </body>
        </html>

    0 讨论(0)
  • 2020-12-04 09:10

    I tried with this sample. Hopefully, It can help you much.

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset=utf-8 />
        <title>SVG colored patterns via mask</title>
      </head>
      <body>
        <svg viewBox="0 0 300 300" xmlns="http://www.w3.org/2000/svg">
          <defs>
            <pattern id="stripes" viewBox="0,0,8,8" width="16" height="16" patternUnits="userSpaceOnUse">
              <polygon points="0,0 4,0 0,4" fill="yellow"></polygon>
              <polygon points="0,8 8,0 8,4 4,8" fill="yellow"></polygon>
              <polygon points="0,4 0,8 8,0 4,0" fill="green"></polygon>
              <polygon points="4,8 8,8 8,4" fill="green"></polygon>
            </pattern>
          </defs>
    	  <rect fill="url(#stripes)" x="150" y="20" width="100" height="50" />
          <circle cx="50"  cy="50" r="50" fill="url(#stripes)"/>
        </svg>
      </body>
    </html> 

    Regards, Vu Phan

    0 讨论(0)
  • 2020-12-04 09:15

    Use the patternTransform attribute to rotate a vertical (or horizontal) line segment. This method tiles seamlessly and uses the simplest possible path. The pattern width attribute controls how close parallel hatches are.

    <pattern id="diagonalHatch" width="10" height="10" patternTransform="rotate(45 0 0)" patternUnits="userSpaceOnUse">
      <line x1="0" y1="0" x2="0" y2="10" style="stroke:black; stroke-width:1" />
    </pattern>
    
    0 讨论(0)
  • 2020-12-04 09:15

    You may be able to create, what you want using a <pattern> tag.

    As a starting point you might take this example of the respective MDN docu:

        <?xml version="1.0"?>
        <svg width="120" height="120" viewBox="0 0 120 120"
             xmlns="http://www.w3.org/2000/svg" version="1.1"
             xmlns:xlink="http://www.w3.org/1999/xlink">
         
            <defs>
                <pattern id="Triangle"
                         width="10" height="10"
                         patternUnits="userSpaceOnUse">
                    <polygon points="5,0 10,10 0,10"/>
                </pattern>
            </defs>
         
            <circle cx="60" cy="60" r="50"
                    fill="url(#Triangle)"/>
        </svg>

    0 讨论(0)
  • 2020-12-04 09:15

    For React Native use can use this component, for making background lines pattern. You should add to your project react-native-svg

    import PropTypes from 'prop-types';
    import React, { PureComponent } from "react";
    import { View } from "react-native";
    import Svg, { Defs, Line, Pattern, Rect } from 'react-native-svg';
    
    export default class PatternLineView extends PureComponent {
    
      static propTypes = {
        pattern: PropTypes.func.isRequired,
        space: PropTypes.number,
        backgroundColor: PropTypes.string,
        lineColor: PropTypes.string,
        lineWidth: PropTypes.number,
        rotation: PropTypes.number
      }
    
      static defaultProps = {
        pattern: () => { },
        space: 8,
        lineColor: "#D2D9E5",
        lineWidth: 3,
        rotation: 45
      }
    
      render() {
        const transform = `rotate(${this.props.rotation})`
        return <View style={{
          flex: 1,
          flexDirection: "row",
          height: "100%",
          width: "100%",
          position: "absolute",
          top: 0,
          start: 0,
          backgroundColor: this.props.backgroundColor
        }}>
          <Svg width="100%" height="100%">
            <Defs>
              <Pattern
                id="linePattern"
                patternUnits="userSpaceOnUse"
                patternTransform={transform}
                width={this.props.space}
                height={this.props.space}>
                <Line
                  x1="0"
                  y1="0"
                  x2="0"
                  y2="100%"
                  stroke={this.props.lineColor}
                  strokeWidth={this.props.lineWidth}
                />
              </Pattern>
            </Defs>
    
            <Rect
              fill="url(#linePattern)"
              x="0"
              y="0"
              width="100%"
              height="100%"
            />
          </Svg>
        </View>
      }
    }
    
    0 讨论(0)
提交回复
热议问题