react-native-svg中的LinearGradient不起作用

时间:2019-03-11 14:31:12

标签: react-native svg react-native-svg

我正在尝试在我的本机项目中使用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

1 个答案:

答案 0 :(得分:0)

我认为@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. 还有react-native-linear-gradient中的LinearGradient

请参考以下代码。

RNLinearGradient

上面的示例代码

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>