【问题标题】:I want to Transfer my SVG in React To React Native我想将我的 SVG 在 React 中传输到 React Native
【发布时间】:2021-11-19 15:17:26
【问题描述】:

我有一个简单的 SVG 代码,它是一个带有图像的圆形进度条,我用它来改变进度的值,如下所示,我想在 react native 中拥有这个具有此功能的 SVG, 我尝试过的事情:

  • react-native-SVG 包,但没有工作,我得到了一些奇怪的行为 在 React js 中的代码,道具是从外部更改的,因此我可以更改任何道具以更改进度值,以及内部的图像以及宽度,

    function CircularBar({ imgSrc, progress, width }) {
      return (
        <svg viewBox="0 0 200 210" width={width}>
          <defs>
            <mask id="m1">
              <circle cx="100" cy="105" r="55" fill="white" />
            </mask>
            <linearGradient
              id="lg1"
              gradientTransform="rotate(0) skewX(-20) skewY(-40)"
            >
              <stop offset="0" stop-color="red" />
              <stop offset="75%" stop-color="orange" />
            </linearGradient>
          </defs>
          <image mask="url(#m1)" href={imgSrc} width="200" />
          <path
            pathLength="360"
            d="M100 175 a 75 75 0 1 1 1 0"
            stroke="LightSlateGray"
            fill="none"
            stroke-width="30"
            stroke-linecap="round"
            stroke-dasharray="270 360"
            stroke-dashoffset="-45"
          />
          <path
            pathLength="360"
            d="M100 175 a 75 75 0 1 1 1 0"
            stroke="url(#lg1)"
            fill="none"
            stroke-width="15"
            stroke-linecap="round"
            stroke-dasharray={`${progress * 270} 360`}
            stroke-dashoffset="-45"
          />
          <g
            transform="translate(100 180)"
            font-size="16"
            font-family="sans-serif"
            font-weight="bold"
            text-anchor="middle"
          >
            <text>Overall</text>
            <text transform="translate(0 20)">Wellbeing</text>
          </g>
        </svg>
      );
    }
    
    export default CircularBar;

这是我卡住的地方,这是 react native 的代码


    import React from 'react';
    import Svg, {
      Circle,
      Defs,
      G,
      Image,
      LinearGradient,
      Mask,
      Path,
      Stop,
      Text,
    } from 'react-native-svg';
    
    function CircularBar({imgSrc, progress, width}) {
      return (
        <Svg width="200" height="400">
          <Defs>
            <Mask id="m1">
              <Circle cx="100" cy="105" r="500" fill="white" />
            </Mask>
            <LinearGradient
              id="lg1"
              gradientTransform="rotate(0) skewX(-20) skewY(-40)">
              <Stop offset="0" stop-color="red" />
              <Stop offset="75%" stop-color="orange" />
            </LinearGradient>
          </Defs>
          <Image mask="url(#m1)" href={imgSrc} width="200" />
          <Path
            pathLength="360"
            d="M100 175 a 75 75 0 1 1 1 0"
            stroke="#778899"
            fill="none"
            stroke-width="30"
            stroke-linecap="round"
            stroke-dasharray="270 360"
            stroke-dashoffset="-45"
          />
          <Path
            pathLength="360"
            d="M100 175 a 75 75 0 1 1 1 0"
            stroke="url(#lg1)"
            fill="none"
            stroke-width="15"
            stroke-linecap="round"
            stroke-dasharray={`200 360`}
            stroke-dashoffset="-45"
          />
          <G
            transform="translate(100 180)"
            font-size="16"
            font-family="sans-serif"
            font-weight="bold"
            text-anchor="middle">
            <Text>Overall</Text>
            <Text transform="translate(0 20)">Wellbeing</Text>
          </G>
        </Svg>
      );
    }

所以我希望你能帮我解决这个问题

【问题讨论】:

  • react-native-svg 没有替代品,这个方法有什么问题?

标签: javascript reactjs react-native svg


【解决方案1】:

我找到了解决方案,您必须采用原始 SVG,然后在 react_native_svg 的 SVGXML 中使用它

 

    let xml = `
      <svg viewBox="0 0 200 210" width="100%"   
      >
      <defs>
        <linearGradient id="b" gradientTransform="matrix(1.3054 -.8391 -.36397 1 0 0)">
          <stop offset="0" stop-color="red"/>
          <stop offset="75%" stop-color="orange"/>
        </linearGradient>
        <mask id="a">
          <circle cx="100" cy="100" r="55" fill="#fff"/>
        </mask>
      </defs>
      <image mask="url(#a)" height="250" width="200"  href="https://djelfa.cc/man.png" />
      <path pathLength="360" d="M100 175a75 75 0 1 1 1 0" stroke="#789" fill="none" stroke-width="30" stroke-linecap="round" stroke-dasharray="270 360" stroke-dashoffset="-45"/>
      <path pathLength="360" d="M100 175a75 75 0 1 1 1 0" stroke="url(#b)" fill="none" stroke-width="15" stroke-linecap="round" stroke-dasharray="${
        counter ? counter * 270 : 0
      } 360" stroke-dashoffset="-45"/>
      <g font-size="16" font-family="sans-serif" font-weight="bold" text-anchor="middle">
        <text transform="translate(100 180)">Overall</text>
        <text transform="translate(100 200)">Wellbeing</text>
      </g>
    </svg>
    `;
      return <SvgXml xml={xml} width="100%" height="100%" />;

这里唯一的问题是如何使图像适合蒙版,我的意思是如何使图像包含在其中,这就是我的反应代码和这段代码之间的区别,即使它们是相同的

【讨论】:

    【解决方案2】:

    我通常使用https://react-svgr.com/playground/?native=true 将我的 svg 转换为 react 原生组件。

    确保在左侧选项卡上选中 react-native。

    示例原始 svg

    <svg width="40" height="40" viewBox="0 0 40 40" fill="none" 
     xmlns="http://www.w3.org/2000/svg">
     <path opacity="0.1" d="M40 20C40 31.0457 31.0457 40 20 40C8.9543 40 0 31.0457 0 
     20C0 8.9543 8.9543 0 20 0C31.0457 0 40 8.9543 40 20ZM4 20C4 28.8366 11.1634 36 
     20 36C28.8366 36 36 28.8366 36 20C36 11.1634 28.8366 4 20 4C11.1634 4 4 11.1634 
     4 20Z" fill="#2B86C3"/>
     <path d="M20 40C16.0979 40 12.281 38.8585 9.01954 36.7161C5.75811 34.5738 
     3.19463 31.5242 1.64491 27.943C0.0951845 24.3618 -0.373076 20.4054 0.297814 
     16.5614C0.968704 12.7174 2.74944 9.15359 5.42063 6.30906C8.09182 3.46453 
     11.5368 1.46353 15.3311 0.552601C19.1254 -0.358333 23.1033 -0.139414 26.7748 
     1.18239C30.4462 2.50419 33.6508 4.87112 35.9937 7.9916C38.3366 11.1121 39.7155 
     14.8498 39.9605 18.7442L35.9684 18.9954C35.7724 15.8798 34.6693 12.8897 32.795 
     10.3933C30.9206 7.8969 28.357 6.00335 25.4198 4.94591C22.4827 3.88847 19.3003 
     3.71333 16.2649 4.44208C13.2294 5.17083 10.4735 6.77162 8.3365 9.04725C6.19955 
     11.3229 4.77496 14.1739 4.23825 17.2491C3.70154 20.3244 4.07615 23.4894 5.31593 
      26.3544C6.5557 29.2193 8.60649 31.659 11.2156 33.3729C13.8248 35.0868 16.8783 
       36 20 36V40Z" fill="#2B86C3"/>
     <path d="M12.6744 14.8182H11.1779L8.63743 16.4787V17.9503L11.0735 
      16.3594H11.1332V25H12.6744V14.8182ZM18.7894 25.1392C20.8377 25.1392 22.2994 
      23.6925 22.2944 21.7088C22.2994 19.7401 20.9272 18.3082 19.0728 
      18.3082C18.3171 18.3082 17.6161 18.5966 17.2184 18.9844H17.1587L17.4819 
      16.1357H21.8022V14.8182H16.2042L15.6325 19.9489L17.0394 20.1577C17.4222 
      19.8146 18.1083 19.581 18.7248 19.581C19.9329 19.5909 20.8079 20.4957 20.8079 
      21.7386C20.8079 22.9616 19.9577 23.8466 18.7894 23.8466C17.805 23.8466 17.0245 
      23.2202 16.945 22.3452H15.4535C15.5131 23.9659 16.9201 25.1392 18.7894 
       25.1392ZM27.655 25.169C30.0066 25.169 31.3837 23.25 31.3837 19.9141C31.3837 
      16.603 29.9867 14.679 27.655 14.679C25.3184 14.679 23.9263 16.598 23.9213 
      19.9141C23.9213 23.245 25.2985 25.1641 27.655 25.169ZM27.655 23.8416C26.2779 
      23.8416 25.4476 22.4595 25.4476 19.9141C25.4526 17.3786 26.2828 15.9815 27.655 
      15.9815C29.0222 15.9815 29.8574 17.3786 29.8574 19.9141C29.8574 22.4595 
     29.0272 23.8416 27.655 23.8416Z" fill="#2B86C3"/>
     </svg>
    

    转换后:

    import * as React from "react"
    import Svg, { Path } from "react-native-svg"
    
     function SvgComponent(props) {
      return (
        <Svg
         width={props.width}
         height={40}
         fill="none"
         xmlns="http://www.w3.org/2000/svg"
         {...props}
        >
        <Path
        opacity={0.1}
        d="M40 20c0 11.046-8.954 20-20 20S0 31.046 0 20 8.954 0 20 0s20 8.954 20 20zM4 20c0 8.837 7.163 16 16 16s16-7.163 16-16S28.837 4 20 4 4 11.163 4 20z"
        fill="#2B86C3"
      />
      <Path
        d="M20 40a20 20 0 1119.96-21.256l-3.992.251A16 16 0 1020 36v4z"
        fill="#2B86C3"
      />
      <Path
        d="M12.674 14.818h-1.496l-2.54 1.66v1.472l2.435-1.59h.06V25h1.541V14.818zM18.79 25.14c2.049 0 3.51-1.447 3.505-3.43.005-1.969-1.367-3.4-3.221-3.4-.756 0-1.457.288-1.855.675h-.06l.324-2.848h4.32v-1.318h-5.598l-.572 5.13 1.407.21c.383-.343 1.07-.577 1.686-.577 1.208.01 2.083.915 2.083 2.158 0 1.223-.85 2.108-2.019 2.108-.984 0-1.765-.627-1.844-1.502h-1.492c.06 1.62 1.467 2.794 3.336 2.794zm8.866.03c2.352 0 3.729-1.919 3.729-5.255 0-3.311-1.397-5.235-3.729-5.235-2.337 0-3.729 1.919-3.734 5.235 0 3.331 1.378 5.25 3.734 5.255zm0-1.327c-1.377 0-2.207-1.383-2.207-3.928.005-2.535.835-3.933 2.207-3.933 1.367 0 2.202 1.398 2.202 3.933 0 2.545-.83 3.928-2.202 3.928z"
        fill="#2B86C3"
      />
    </Svg>
    )
    }
    
    export default SvgComponent
    

    并在您的组件中使用,例如:

    import SvgComponent from '...path.../SvgComponent'
    .
    .
    .
    <View>
    <SvgComponent width={40} />
    <Text>your number</Text>
    </View>
    

    用绝对位置让你的号码超过进度 我指定宽度,因为我的 svg 组件中的宽度值为 {props.width}

    同样适用于数字并使用您自己的逻辑。

    【讨论】:

    • 但我的 SVG 处于反应状态,我想将其转换为 react-native ??我如何获得原始 SVG ??
    • 我试过你的方法,屏幕上什么也没有显示
    • 如果你有 figma 或其他东西的设计......你可以复制 svg.. 如果你尝试将 svg 从 react 转换为 react native,svg 路径和其他道具会中断,因为你已经在你的 svg 中有道具。所以它不是一个svg文件,而是一个jsx文件。尝试从源设计中获取 svg。
    • 确保您已将正确的 fillstroke 属性分配给 SVG。例如fill="red" stroke="red"
    • 我做到了,我现在可以显示 SVG,但图像不包含在圆圈内,如何使对象适合:包含在此图像中,与相同的代码反应不会被裁剪,但在 React native 中不包含任何想法
    猜你喜欢
    • 2020-03-16
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2019-03-15
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多