Skip to content

Commit 9aba8f2

Browse files
committed
feat: add getDataURL support
1 parent 2170364 commit 9aba8f2

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/SVGRenderer.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import env from 'zrender/lib/core/env';
12
import SVGPainter from 'zrender/lib/svg/Painter';
2-
33
import { vNodeToString } from './SVGCore';
44
import { updateAttrs } from 'zrender/lib/svg/patch';
55
import type Storage from 'zrender/lib/Storage';
66
import { DOMParser } from '@xmldom/xmldom';
77

88
const isRn = globalThis.navigator?.product === 'ReactNative';
9+
env.svgSupported = true;
910

1011
interface SVGPainterOption {
1112
width?: number;
@@ -69,6 +70,15 @@ class CustomSVGPainter extends SVGPainter {
6970
super.refresh();
7071
}
7172
}
73+
toDataURL(base64?: boolean):string {
74+
// @ts-ignore
75+
if (isRn && this._svgDom.makeImageSnapshot) {
76+
// @ts-ignore
77+
return this._svgDom.makeImageSnapshot() || super.toDataURL(base64);
78+
} else {
79+
return super.toDataURL(base64);
80+
}
81+
}
7282
}
7383

7484
export function SVGRenderer(registers: any) {

src/skiaChart.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, {
88
useRef,
99
} from 'react';
1010

11-
import { Canvas, ImageSVG, Skia, SkSVG } from '@shopify/react-native-skia';
11+
import { Canvas, ImageSVG, Skia, SkSVG, useCanvasRef } from '@shopify/react-native-skia';
1212

1313
import { View } from 'react-native';
1414

@@ -55,6 +55,7 @@ function SkiaComponent(
5555
const [width, setWidth] = useState<number>(initialWidth ?? 0);
5656
const [height, setHeight] = useState<number>(initialHeight ?? 0);
5757
const zrenderId = useRef<number>();
58+
const canvasRef = useCanvasRef();
5859

5960
const dispatchEvents = useCallback<DispatchEvents>(
6061
(types, nativeEvent, eventArgs) => {
@@ -86,6 +87,10 @@ function SkiaComponent(
8687
setZrenderId: (id: number) => {
8788
zrenderId.current = id;
8889
},
90+
makeImageSnapshot: () => {
91+
const image = canvasRef.current?.makeImageSnapshot();
92+
return image ? `data:image/png;base64,${image.encodeToBase64()}` : null;
93+
}
8994
},
9095
viewprot: {},
9196
dispatchEvents,
@@ -101,7 +106,7 @@ function SkiaComponent(
101106

102107
return svgString ? (
103108
<View testID="component" style={{ ...style, width, height }}>
104-
<Canvas style={{ ...style, width, height }} pointerEvents="auto">
109+
<Canvas style={{ ...style, width, height }} pointerEvents="auto" ref={canvasRef}>
105110
<ImageSVG svg={svgString} x={0} y={0} width={width} height={height} />
106111
</Canvas>
107112
{handleGesture ? (

0 commit comments

Comments
 (0)