8

I have the following React Native project:

https://snack.expo.io/BkBU8fAlV

where I have the following code:

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';

// You can import from local files
import AssetExample from './components/AssetExample';

// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';

import HomerSvg from './assets/HomerSvg';

export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.paragraph}>
          Change code in the editor and watch it change on your phone! Save to get a shareable url.
        </Text>
        <View style={{ width: 80, height: 80 }}>
          <HomerSvg />
        </View>
        <Card>
          <AssetExample />
        </Card>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
});

I display the SVG image through the component: HomerSvg which uses the react-native-svg package.

What I need is to resize somehow the SVG image. On the code above I did a try with no success.

I tried by giving the container view some width and height with no success.

Do you have any idea on how can I achieve that?

Thanks!

2
  • Resize works with width and height which is you already applied, can you explain more? Commented Dec 24, 2018 at 8:28
  • can you show your resolved code? Commented Jan 14, 2020 at 9:54

5 Answers 5

12

This is actually very easy to solve without React Native but SVG itself.

Just set the preserveAspectRatio tag to none.

Example:

<Svg
  width={this.props.width}
  height={this.heights.h1}
  xmlns="http://www.w3.org/2000/svg" 
  viewBox="0 0 750 352"
  preserveAspectRatio="none">
...
</Svg>
Sign up to request clarification or add additional context in comments.

Comments

12

I know this is an old question but maybe we're forgetting the fundamentals here, remember that imported SVGs can be provided a width and height.

import HomeSvg from './assets/HomeSvg';

<HomeSvg height={20} width={20}/>

1 Comment

I needed my SVG to stretch and I ended up passing window width. Thanks a lot for the reminder :)
3

For me the problem was that SVG optimizations removed viewBox.
Try adding viewBox="0 0 width height" prop to the Svg (width and height being the original dimensions).

Comments

1

You can wrap all the <Path /> elements inside a <G /> and scale the <G /> component by calculating the scale factor using Dimensions i.e width and height of the device.

<G transform="scale(scaleFactor) translate(offsetX,offsetY)>
<Path/>
<Path/>
.
.
.
<G/>

where scaleFactor, offsetX and offsetY can be calculated via const { height, width } = Dimensions.get("window")

2 Comments

Hi, can you please show a better example, I am struggling with this
Where does G come from?
0

Unless the SVG's dimensions are based on the containers dimensions, they will not change. Images with set dimensions ignore their parent's dimensions, and you set the dimensions in HomerSvg.js

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.