-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to save created image to SVG file ? #212
Comments
You can convert the base64 to PNG format by using other libraries. |
@magicismight Thank you for your tip
Not sure I understand. I could call toDataURL() on the SVG serialized result? What would a traverse SVG children and serialize snippet look like? |
Can any react native experts share suggestions on how to invisibly render a SVG for image generation purposes using toDataURL(base64) ? The VictorChart I use supports a chart.containerRef.svgRef.toDataURL. The toDataURL(base64) works well to write image to a file using RNFS, then I add image reference to HTML content, and rendering HTML to PDF.. All good so far. The hard part, is understanding how to create "dummy view" or a technique to invisibly render an SVG image offscreen ( with width set to match desired PDF page width ) ? Example: Save SVG as file image using RNFS
In render method, call svgRef.toDataURL()
Thanks in advance, |
Exposing the |
This appears broken after upgrading react-native and victory-native from 0.7.0 to 0.15.0 It renders however chart.containerRef is undefined? Is there another way to get chart.containerRef.svgRef ?
I hope I do not have to roll back to victory-native 0.7.0 :-( |
@boygirl explained on Gitter
I do not understand this. I will do some research on containerComponent |
I cannot figure this out how to get a svgRef from containerComponent Rolling-back to victory-native 0.7.0 I expected this to be easy. I expect creating a PDF containing a VictoryChart image is a common use case of any charting library. |
@esutton adding an optional |
For capturing a VictoryChart chart react-native-view-shot worked for me: https://github.com/gre/react-native-view-shot
|
Closing as the original issues seems fixed. |
An alternative solution if you want vector/svg rather than raster/png: import * as React from "react";
import { Platform, StyleSheet, TouchableOpacity } from "react-native";
import { Svg, Rect } from "react-native-svg";
import ReactDOMServer from "react-dom/server";
const isWeb = Platform.OS === "web";
const childToWeb = child => {
const { type, props } = child;
const name = type && (type.displayName || type.name);
const Tag = name && name[0].toLowerCase() + name.slice(1);
return <Tag {...props}>{toWeb(props.children)}</Tag>;
};
const toWeb = children => React.Children.map(children, childToWeb);
export default class App extends React.Component {
renderSvg() {
return (
<Svg height="100%" width="100%" style={{ backgroundColor: "#33AAFF" }}>
<Rect
x="50"
y="50"
width="50"
height="50"
fill="#3399ff"
strokeWidth="3"
stroke="rgb(0,0,0)"
/>
</Svg>
);
}
serialize = () => {
const element = this.renderSvg();
const webJsx = isWeb ? element : toWeb(element);
const svgString = ReactDOMServer.renderToStaticMarkup(webJsx);
console.log(svgString);
};
render() {
return (
<TouchableOpacity style={styles.container} onPress={this.serialize}>
{this.renderSvg()}
</TouchableOpacity>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
backgroundColor: "#ecf0f1",
padding: 8
}
}); |
Is there a way to export a created image drawn using react-native-svg to a SVG file?
My goal is to:
I ran across a brief reference to a new method toDataURL.
While I think writing to a SVG file would be more versatile for PDF report generation,
what format does toDataURL output to? SVG? PNG?
Thank you for a great component!
The text was updated successfully, but these errors were encountered: