2

我正在尝试在我的 react native 项目中使用 SVG。这是我的组件的代码:

<Svg xmlns="http://www.w3.org/2000/svg" width="100%" height="507" viewBox="0 0 375 507" style={{position:'absolute', bottom:0}}>

  <Defs>

     <ClipPath id="a">
        <Rect class="a" fill='#fff' stroke='#707070' width="375" height="507" transform="translate(0 160)" d="M0 0h375v507H0z"/>
    </ClipPath>

    <LinearGradient id="b" clipPath='url(#a)' x1="0.5" x2="-0.031" y2="1.477" gradientUnits="objectBoundingBox">
        <Stop offset="0" stopColor="rgb(76,209,149)"  />
        <Stop offset="1" stopColor="rgb(170,221,100)"  />
    </LinearGradient>

  </Defs>

    <G class="b" transform="translate(0 -160)">
        <Circle class="c" cx="334.249" cy="334.249" r="334.249" transform="translate(-146.354 164.646)"  fill='url(#b)' />
    </G>

</Svg>

我得到的输出是:

https://i.stack.imgur.com/mGaBg.png

4

1 回答 1

2

我认为@enxaneta 是对的,clip-path似乎不存在react-native-svg请参阅文档,您可以在此处的文档中找到react-native-svg #LinearGradient

我认为您应该像这样引用它:

        <Rect class="a" fill="url(#yourGradientId)" stroke='#707070' width="375" height="507" transform="translate(0 160)" d="M0 0h375v507H0z"/>

fill应该在哪里引用您的渐变ID,即fill=url(#b)

我曾经<Path>实现以下结果

注意:我通过使用两个相互叠加的形状来实现这一点:

  1. 使用背景渐变的组件react-native-linear-gradient
  2. 和 LinearGradient 来自react-native-linear-gradient

请参考下面的代码。

RN线性梯度

上面的示例代码

import Svg, { Path, Defs, LinearGradient, Stop } from 'react-native-svg';
import Gradient from 'react-native-linear-gradient'
const { width, height } = Dimensions.get('window')

      <Gradient style={{height: height * .25,}} 
        colors={ ['#FFD080', 'red'] }
        start={{ x: 0, y: 0}}
        end={{ x:1, y: 1}}
        locations={[0.18, 1, 1]}>
          
            
        <Svg
            height={height * .44}
            width={width}
            viewBox="0 0 1440 320"
            style={{ position: 'relative', top: height * .069 }}
          >
          <Defs>
          <LinearGradient id="path" x1="0" y1="0" x2="1" y2="1">
              <Stop offset="0" stopColor="#FFD080" stopOpacity="1" />
              <Stop offset="1" stopColor="red" stopOpacity="1" />
            </LinearGradient>
          </Defs>
            <Path fill="url(#path)"
               d="M0,96L48,133.3C96,171,192,245,288,229.3C384,213,480,107,576,74.7C672,43,768,85,864,133.3C960,181,1056,235,1152,234.7C1248,235,1344,181,1392,154.7L1440,128L1440,0L1392,0C1344,0,1248,0,1152,0C1056,0,960,0,864,0C768,0,672,0,576,0C480,0,384,0,288,0C192,0,96,0,48,0L0,0Z"/>
        </Svg>
        </Gradient>
于 2020-07-11T17:32:00.580 回答