Home > Blockchain >  I want to Transfer my SVG in React To React Native
I want to Transfer my SVG in React To React Native

Time:09-28

I have a simple SVG code, which is a circle progress bar with an image, and I used it to change the value of progress as below, I want to have this SVG with this feature in react native, things I tried :

  • react-native-SVG package, but didn't work and I get some weird behavior with it the code in React js , the props is changed from outside so I can change any props to change progress value , and image inside , as well as the width too ,

    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;

this is where I'm stuck, this is the code for react native side


    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>
      );
    }

so I hope you could help me with it

CodePudding user response:

I normally use https://react-svgr.com/playground/?native=true to convert my svg to a react native component.

Make sure you check react-native on the left tab.

Example raw 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>

after converting :

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

and use in your component like:

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

make your number over progress with absolute position I specify width since I have value of width in my svg component is {props.width}

Similarly for numbers and use your own logic.

Cheers.

CodePudding user response:

I found the solution you will have to take a raw SVG , and then used it in SVGXML from react_native_svg

 

    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%" />;

the only problem here is how to make the image fit inside the mask, I mean how to make the image contained inside it, that's the difference between my react code and this code even they are the same

  • Related