@shopify/react-native-skia 1.3.6 → 1.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseView.java +14 -7
- package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseViewManager.java +1 -2
- package/lib/commonjs/dom/nodes/Node.d.ts +2 -3
- package/lib/commonjs/dom/nodes/Node.js +1 -2
- package/lib/commonjs/dom/nodes/Node.js.map +1 -1
- package/lib/commonjs/dom/nodes/PaintNode.js +1 -1
- package/lib/commonjs/dom/nodes/PaintNode.js.map +1 -1
- package/lib/commonjs/dom/nodes/drawings/Box.js +1 -1
- package/lib/commonjs/dom/nodes/drawings/Box.js.map +1 -1
- package/lib/commonjs/dom/nodes/paint/BlendNode.js +1 -1
- package/lib/commonjs/dom/nodes/paint/BlendNode.js.map +1 -1
- package/lib/commonjs/dom/nodes/paint/ColorFilters.js +1 -1
- package/lib/commonjs/dom/nodes/paint/ColorFilters.js.map +1 -1
- package/lib/commonjs/dom/nodes/paint/ImageFilters.js +1 -1
- package/lib/commonjs/dom/nodes/paint/ImageFilters.js.map +1 -1
- package/lib/commonjs/dom/nodes/paint/MaskFilters.js +1 -1
- package/lib/commonjs/dom/nodes/paint/MaskFilters.js.map +1 -1
- package/lib/commonjs/dom/nodes/paint/PathEffects.js +1 -1
- package/lib/commonjs/dom/nodes/paint/PathEffects.js.map +1 -1
- package/lib/commonjs/dom/nodes/paint/Shaders.js +1 -1
- package/lib/commonjs/dom/nodes/paint/Shaders.js.map +1 -1
- package/lib/commonjs/dom/types/Node.d.ts +1 -2
- package/lib/commonjs/dom/types/Node.js.map +1 -1
- package/lib/commonjs/dom/types/NodeType.d.ts +0 -9
- package/lib/commonjs/dom/types/NodeType.js +1 -11
- package/lib/commonjs/dom/types/NodeType.js.map +1 -1
- package/lib/commonjs/external/reanimated/useVideo.js +0 -2
- package/lib/commonjs/external/reanimated/useVideo.js.map +1 -1
- package/lib/commonjs/external/reanimated/useVideoLoading.js +6 -4
- package/lib/commonjs/external/reanimated/useVideoLoading.js.map +1 -1
- package/lib/commonjs/skia/core/SVG.web.d.ts +2 -0
- package/lib/commonjs/skia/core/SVG.web.js +22 -0
- package/lib/commonjs/skia/core/SVG.web.js.map +1 -0
- package/lib/commonjs/skia/types/NativeBuffer/NativeBufferFactory.js +1 -1
- package/lib/commonjs/skia/types/NativeBuffer/NativeBufferFactory.js.map +1 -1
- package/lib/commonjs/skia/web/JsiSkCanvas.d.ts +1 -1
- package/lib/commonjs/skia/web/JsiSkCanvas.js +3 -2
- package/lib/commonjs/skia/web/JsiSkCanvas.js.map +1 -1
- package/lib/commonjs/skia/web/JsiSkSVG.d.ts +9 -0
- package/lib/commonjs/skia/web/JsiSkSVG.js +28 -0
- package/lib/commonjs/skia/web/JsiSkSVG.js.map +1 -0
- package/lib/commonjs/skia/web/JsiSkSVGFactory.d.ts +4 -3
- package/lib/commonjs/skia/web/JsiSkSVGFactory.js +40 -4
- package/lib/commonjs/skia/web/JsiSkSVGFactory.js.map +1 -1
- package/lib/module/dom/nodes/Node.d.ts +2 -3
- package/lib/module/dom/nodes/Node.js +1 -2
- package/lib/module/dom/nodes/Node.js.map +1 -1
- package/lib/module/dom/nodes/PaintNode.js +2 -2
- package/lib/module/dom/nodes/PaintNode.js.map +1 -1
- package/lib/module/dom/nodes/drawings/Box.js +2 -2
- package/lib/module/dom/nodes/drawings/Box.js.map +1 -1
- package/lib/module/dom/nodes/paint/BlendNode.js +2 -2
- package/lib/module/dom/nodes/paint/BlendNode.js.map +1 -1
- package/lib/module/dom/nodes/paint/ColorFilters.js +2 -2
- package/lib/module/dom/nodes/paint/ColorFilters.js.map +1 -1
- package/lib/module/dom/nodes/paint/ImageFilters.js +2 -2
- package/lib/module/dom/nodes/paint/ImageFilters.js.map +1 -1
- package/lib/module/dom/nodes/paint/MaskFilters.js +2 -2
- package/lib/module/dom/nodes/paint/MaskFilters.js.map +1 -1
- package/lib/module/dom/nodes/paint/PathEffects.js +2 -2
- package/lib/module/dom/nodes/paint/PathEffects.js.map +1 -1
- package/lib/module/dom/nodes/paint/Shaders.js +2 -2
- package/lib/module/dom/nodes/paint/Shaders.js.map +1 -1
- package/lib/module/dom/types/Node.d.ts +1 -2
- package/lib/module/dom/types/Node.js.map +1 -1
- package/lib/module/dom/types/NodeType.d.ts +0 -9
- package/lib/module/dom/types/NodeType.js +0 -10
- package/lib/module/dom/types/NodeType.js.map +1 -1
- package/lib/module/external/reanimated/useVideo.js +0 -2
- package/lib/module/external/reanimated/useVideo.js.map +1 -1
- package/lib/module/external/reanimated/useVideoLoading.js +7 -5
- package/lib/module/external/reanimated/useVideoLoading.js.map +1 -1
- package/lib/module/skia/core/SVG.web.d.ts +2 -0
- package/lib/module/skia/core/SVG.web.js +15 -0
- package/lib/module/skia/core/SVG.web.js.map +1 -0
- package/lib/module/skia/types/NativeBuffer/NativeBufferFactory.js +1 -1
- package/lib/module/skia/types/NativeBuffer/NativeBufferFactory.js.map +1 -1
- package/lib/module/skia/web/JsiSkCanvas.d.ts +1 -1
- package/lib/module/skia/web/JsiSkCanvas.js +3 -2
- package/lib/module/skia/web/JsiSkCanvas.js.map +1 -1
- package/lib/module/skia/web/JsiSkSVG.d.ts +9 -0
- package/lib/module/skia/web/JsiSkSVG.js +21 -0
- package/lib/module/skia/web/JsiSkSVG.js.map +1 -0
- package/lib/module/skia/web/JsiSkSVGFactory.d.ts +4 -3
- package/lib/module/skia/web/JsiSkSVGFactory.js +41 -5
- package/lib/module/skia/web/JsiSkSVGFactory.js.map +1 -1
- package/lib/typescript/src/dom/nodes/Node.d.ts +2 -3
- package/lib/typescript/src/dom/types/Node.d.ts +1 -2
- package/lib/typescript/src/dom/types/NodeType.d.ts +0 -9
- package/lib/typescript/src/skia/core/SVG.web.d.ts +2 -0
- package/lib/typescript/src/skia/web/JsiSkCanvas.d.ts +1 -1
- package/lib/typescript/src/skia/web/JsiSkSVG.d.ts +9 -0
- package/lib/typescript/src/skia/web/JsiSkSVGFactory.d.ts +4 -3
- package/package.json +2 -2
- package/src/dom/nodes/Node.ts +2 -12
- package/src/dom/nodes/PaintNode.ts +2 -2
- package/src/dom/nodes/drawings/Box.ts +2 -2
- package/src/dom/nodes/paint/BlendNode.ts +2 -2
- package/src/dom/nodes/paint/ColorFilters.ts +2 -2
- package/src/dom/nodes/paint/ImageFilters.ts +2 -2
- package/src/dom/nodes/paint/MaskFilters.ts +2 -2
- package/src/dom/nodes/paint/PathEffects.ts +2 -2
- package/src/dom/nodes/paint/Shaders.ts +2 -2
- package/src/dom/types/Node.ts +2 -2
- package/src/dom/types/NodeType.ts +0 -10
- package/src/external/reanimated/useVideo.ts +0 -2
- package/src/external/reanimated/useVideoLoading.ts +11 -8
- package/src/skia/core/SVG.web.ts +29 -0
- package/src/skia/types/NativeBuffer/NativeBufferFactory.ts +1 -1
- package/src/skia/web/JsiSkCanvas.ts +6 -2
- package/src/skia/web/JsiSkSVG.ts +27 -0
- package/src/skia/web/JsiSkSVGFactory.ts +47 -6
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { SkRRect, Skia } from "../../../skia/types";
|
|
2
2
|
import { BlurStyle, ClipOp, isRRect } from "../../../skia/types";
|
|
3
3
|
import type { DrawingContext } from "../../types";
|
|
4
|
-
import {
|
|
4
|
+
import { NodeType } from "../../types";
|
|
5
5
|
import type { BoxShadowProps, BoxProps } from "../../types/Drawings";
|
|
6
6
|
import type { NodeContext } from "../Node";
|
|
7
7
|
import { JsiDeclarationNode } from "../Node";
|
|
@@ -38,7 +38,7 @@ const deflate = (
|
|
|
38
38
|
|
|
39
39
|
export class BoxShadowNode extends JsiDeclarationNode<BoxShadowProps> {
|
|
40
40
|
constructor(ctx: NodeContext, props: BoxShadowProps) {
|
|
41
|
-
super(ctx,
|
|
41
|
+
super(ctx, NodeType.BoxShadow, props);
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
decorate(_ctx: DeclarationContext) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { BlendProps } from "../../types/ImageFilters";
|
|
2
2
|
import { BlendMode } from "../../../skia/types";
|
|
3
|
-
import {
|
|
3
|
+
import { NodeType } from "../../types/NodeType";
|
|
4
4
|
import type { NodeContext } from "../Node";
|
|
5
5
|
import { JsiDeclarationNode } from "../Node";
|
|
6
6
|
import { enumKey } from "../datatypes";
|
|
@@ -9,7 +9,7 @@ import { composeDeclarations } from "../../types/DeclarationContext";
|
|
|
9
9
|
|
|
10
10
|
export class BlendNode extends JsiDeclarationNode<BlendProps> {
|
|
11
11
|
constructor(ctx: NodeContext, props: BlendProps) {
|
|
12
|
-
super(ctx,
|
|
12
|
+
super(ctx, NodeType.Blend, props);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
decorate(ctx: DeclarationContext) {
|
|
@@ -6,14 +6,14 @@ import type {
|
|
|
6
6
|
BlendColorFilterProps,
|
|
7
7
|
MatrixColorFilterProps,
|
|
8
8
|
} from "../../types";
|
|
9
|
-
import {
|
|
9
|
+
import { NodeType } from "../../types";
|
|
10
10
|
import { enumKey } from "../datatypes/Enum";
|
|
11
11
|
import type { LerpColorFilterProps } from "../../types/ColorFilters";
|
|
12
12
|
import type { DeclarationContext } from "../../types/DeclarationContext";
|
|
13
13
|
|
|
14
14
|
export abstract class ColorFilterDeclaration<P> extends JsiDeclarationNode<P> {
|
|
15
15
|
constructor(ctx: NodeContext, type: NodeType, props: P) {
|
|
16
|
-
super(ctx,
|
|
16
|
+
super(ctx, type, props);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
protected composeAndPush(ctx: DeclarationContext, cf1: SkColorFilter) {
|
|
@@ -15,7 +15,7 @@ import type {
|
|
|
15
15
|
OffsetImageFilterProps,
|
|
16
16
|
RuntimeShaderImageFilterProps,
|
|
17
17
|
} from "../../types";
|
|
18
|
-
import {
|
|
18
|
+
import { NodeType } from "../../types";
|
|
19
19
|
import { processRadius, enumKey } from "../datatypes";
|
|
20
20
|
import type { NodeContext } from "../Node";
|
|
21
21
|
import { JsiDeclarationNode } from "../Node";
|
|
@@ -58,7 +58,7 @@ const MakeInnerShadow = (
|
|
|
58
58
|
|
|
59
59
|
export abstract class ImageFilterDeclaration<P> extends JsiDeclarationNode<P> {
|
|
60
60
|
constructor(ctx: NodeContext, type: NodeType, props: P) {
|
|
61
|
-
super(ctx,
|
|
61
|
+
super(ctx, type, props);
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
protected input(ctx: DeclarationContext) {
|
|
@@ -2,13 +2,13 @@ import { BlurStyle } from "../../../skia/types";
|
|
|
2
2
|
import type { NodeContext } from "../Node";
|
|
3
3
|
import { JsiDeclarationNode } from "../Node";
|
|
4
4
|
import type { BlurMaskFilterProps } from "../../types";
|
|
5
|
-
import {
|
|
5
|
+
import { NodeType } from "../../types";
|
|
6
6
|
import { enumKey } from "../datatypes";
|
|
7
7
|
import type { DeclarationContext } from "../../types/DeclarationContext";
|
|
8
8
|
|
|
9
9
|
export class BlurMaskFilterNode extends JsiDeclarationNode<BlurMaskFilterProps> {
|
|
10
10
|
constructor(ctx: NodeContext, props: BlurMaskFilterProps) {
|
|
11
|
-
super(ctx,
|
|
11
|
+
super(ctx, NodeType.BlurMaskFilter, props);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
decorate(ctx: DeclarationContext) {
|
|
@@ -10,7 +10,7 @@ import type {
|
|
|
10
10
|
Path1DPathEffectProps,
|
|
11
11
|
Path2DPathEffectProps,
|
|
12
12
|
} from "../../types";
|
|
13
|
-
import {
|
|
13
|
+
import { NodeType } from "../../types";
|
|
14
14
|
import { enumKey } from "../datatypes/Enum";
|
|
15
15
|
import { processPath } from "../datatypes";
|
|
16
16
|
import type { DeclarationContext } from "../../types/DeclarationContext";
|
|
@@ -18,7 +18,7 @@ import { composeDeclarations } from "../../types/DeclarationContext";
|
|
|
18
18
|
|
|
19
19
|
abstract class PathEffectDeclaration<P> extends JsiDeclarationNode<P> {
|
|
20
20
|
constructor(ctx: NodeContext, type: NodeType, props: P) {
|
|
21
|
-
super(ctx,
|
|
21
|
+
super(ctx, type, props);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
protected composeAndPush(ctx: DeclarationContext, pe1: SkPathEffect) {
|
|
@@ -18,7 +18,7 @@ import type {
|
|
|
18
18
|
TurbulenceProps,
|
|
19
19
|
TwoPointConicalGradientProps,
|
|
20
20
|
} from "../../types";
|
|
21
|
-
import {
|
|
21
|
+
import { NodeType } from "../../types";
|
|
22
22
|
import {
|
|
23
23
|
enumKey,
|
|
24
24
|
fitRects,
|
|
@@ -30,7 +30,7 @@ import {
|
|
|
30
30
|
|
|
31
31
|
export abstract class ShaderDeclaration<P> extends JsiDeclarationNode<P> {
|
|
32
32
|
constructor(ctx: NodeContext, type: NodeType, props: P) {
|
|
33
|
-
super(ctx,
|
|
33
|
+
super(ctx, type, props);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
package/src/dom/types/Node.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { GroupProps } from "./Common";
|
|
2
|
-
import type {
|
|
2
|
+
import type { NodeType } from "./NodeType";
|
|
3
3
|
import type { DeclarationContext } from "./DeclarationContext";
|
|
4
4
|
import type { DrawingContext } from "./DrawingContext";
|
|
5
5
|
|
|
@@ -19,7 +19,7 @@ export interface Node<P> {
|
|
|
19
19
|
export type Invalidate = () => void;
|
|
20
20
|
|
|
21
21
|
export interface DeclarationNode<P> extends Node<P> {
|
|
22
|
-
declarationType: DeclarationType;
|
|
22
|
+
//declarationType: DeclarationType;
|
|
23
23
|
decorate(ctx: DeclarationContext): void;
|
|
24
24
|
|
|
25
25
|
setInvalidate(invalidate: Invalidate): void;
|
|
@@ -36,7 +36,6 @@ const setFrame = (video: Video, currentFrame: SharedValue<SkImage | null>) => {
|
|
|
36
36
|
currentFrame.value.dispose();
|
|
37
37
|
}
|
|
38
38
|
currentFrame.value = img;
|
|
39
|
-
} else {
|
|
40
39
|
copyFrameOnAndroid(currentFrame);
|
|
41
40
|
}
|
|
42
41
|
};
|
|
@@ -99,7 +98,6 @@ export const useVideo = (
|
|
|
99
98
|
() => seek.value,
|
|
100
99
|
(value) => {
|
|
101
100
|
if (value !== null) {
|
|
102
|
-
copyFrameOnAndroid(currentFrame);
|
|
103
101
|
video?.seek(value);
|
|
104
102
|
currentTime.value = value;
|
|
105
103
|
seek.value = null;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useState } from "react";
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
2
|
|
|
3
3
|
import type { Video } from "../../skia/types";
|
|
4
4
|
import { Skia } from "../../skia";
|
|
@@ -10,17 +10,20 @@ const runtime = Rea.createWorkletRuntime("video-metadata-runtime");
|
|
|
10
10
|
type VideoSource = string | null;
|
|
11
11
|
|
|
12
12
|
export const useVideoLoading = (source: VideoSource) => {
|
|
13
|
-
const runOnJS = Rea
|
|
13
|
+
const { runOnJS } = Rea;
|
|
14
14
|
const [video, setVideo] = useState<Video | null>(null);
|
|
15
|
-
const cb = (
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
const cb = useCallback(
|
|
16
|
+
(src: string) => {
|
|
17
|
+
"worklet";
|
|
18
|
+
const vid = Skia.Video(src) as Video;
|
|
19
|
+
runOnJS(setVideo)(vid);
|
|
20
|
+
},
|
|
21
|
+
[runOnJS]
|
|
22
|
+
);
|
|
20
23
|
useEffect(() => {
|
|
21
24
|
if (source) {
|
|
22
25
|
Rea.runOnRuntime(runtime, cb)(source);
|
|
23
26
|
}
|
|
24
|
-
}, [source]);
|
|
27
|
+
}, [cb, source]);
|
|
25
28
|
return video;
|
|
26
29
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Skia } from "../Skia";
|
|
2
|
+
import type { DataSourceParam } from "../types";
|
|
3
|
+
|
|
4
|
+
export const useSVG = (
|
|
5
|
+
source: DataSourceParam,
|
|
6
|
+
onError?: (err: Error) => void
|
|
7
|
+
) => {
|
|
8
|
+
if (source === null || source === undefined) {
|
|
9
|
+
throw new Error(`Invalid svg data source. Got: ${source}`);
|
|
10
|
+
}
|
|
11
|
+
if (
|
|
12
|
+
typeof source !== "object" ||
|
|
13
|
+
source instanceof Uint8Array ||
|
|
14
|
+
typeof source.default !== "string"
|
|
15
|
+
) {
|
|
16
|
+
throw new Error(
|
|
17
|
+
`Invalid svg data source. Make sure that the source resolves to a string. Got: ${JSON.stringify(
|
|
18
|
+
source,
|
|
19
|
+
null,
|
|
20
|
+
2
|
|
21
|
+
)}`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
const svg = Skia.SVG.MakeFromString(source.default);
|
|
25
|
+
if (svg === null && onError !== undefined) {
|
|
26
|
+
onError(new Error("Failed to create SVG from source."));
|
|
27
|
+
}
|
|
28
|
+
return svg;
|
|
29
|
+
};
|
|
@@ -25,7 +25,7 @@ export const isNativeBufferWeb = (
|
|
|
25
25
|
buffer instanceof HTMLCanvasElement ||
|
|
26
26
|
buffer instanceof ImageBitmap ||
|
|
27
27
|
buffer instanceof OffscreenCanvas ||
|
|
28
|
-
buffer instanceof VideoFrame ||
|
|
28
|
+
(typeof VideoFrame !== "undefined" && buffer instanceof VideoFrame) ||
|
|
29
29
|
buffer instanceof HTMLImageElement ||
|
|
30
30
|
buffer instanceof SVGImageElement ||
|
|
31
31
|
buffer instanceof CanvasKitWebGLBuffer;
|
|
@@ -42,6 +42,7 @@ import { JsiSkMatrix } from "./JsiSkMatrix";
|
|
|
42
42
|
import { JsiSkImageFilter } from "./JsiSkImageFilter";
|
|
43
43
|
import { JsiSkPoint } from "./JsiSkPoint";
|
|
44
44
|
import { JsiSkRSXform } from "./JsiSkRSXform";
|
|
45
|
+
import type { JsiSkSVG } from "./JsiSkSVG";
|
|
45
46
|
|
|
46
47
|
export class JsiSkCanvas
|
|
47
48
|
extends HostObject<Canvas, "Canvas">
|
|
@@ -302,8 +303,11 @@ export class JsiSkCanvas
|
|
|
302
303
|
);
|
|
303
304
|
}
|
|
304
305
|
|
|
305
|
-
drawSvg(
|
|
306
|
-
|
|
306
|
+
drawSvg(svg: SkSVG, _width?: number, _height?: number) {
|
|
307
|
+
const image = this.CanvasKit.MakeImageFromCanvasImageSource(
|
|
308
|
+
(svg as JsiSkSVG).ref
|
|
309
|
+
);
|
|
310
|
+
this.ref.drawImage(image, 0, 0);
|
|
307
311
|
}
|
|
308
312
|
|
|
309
313
|
save() {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { CanvasKit } from "canvaskit-wasm";
|
|
2
|
+
|
|
3
|
+
import type { SkSVG } from "../types";
|
|
4
|
+
|
|
5
|
+
import { HostObject } from "./Host";
|
|
6
|
+
|
|
7
|
+
export class JsiSkSVG
|
|
8
|
+
extends HostObject<HTMLImageElement, "SVG">
|
|
9
|
+
implements SkSVG
|
|
10
|
+
{
|
|
11
|
+
constructor(CanvasKit: CanvasKit, ref: HTMLImageElement) {
|
|
12
|
+
super(CanvasKit, ref, "SVG");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
width(): number {
|
|
16
|
+
return this.ref.width;
|
|
17
|
+
}
|
|
18
|
+
height(): number {
|
|
19
|
+
return this.ref.height;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
dispose = () => {
|
|
23
|
+
if (this.ref.parentNode) {
|
|
24
|
+
this.ref.parentNode.removeChild(this.ref);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -1,20 +1,61 @@
|
|
|
1
1
|
import type { CanvasKit } from "canvaskit-wasm";
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { SkSVG } from "../types";
|
|
4
4
|
import type { SVGFactory } from "../types/SVG/SVGFactory";
|
|
5
5
|
|
|
6
|
-
import { Host
|
|
6
|
+
import { Host } from "./Host";
|
|
7
|
+
import type { JsiSkData } from "./JsiSkData";
|
|
8
|
+
import { JsiSkSVG } from "./JsiSkSVG";
|
|
7
9
|
|
|
8
10
|
export class JsiSkSVGFactory extends Host implements SVGFactory {
|
|
9
11
|
constructor(CanvasKit: CanvasKit) {
|
|
10
12
|
super(CanvasKit);
|
|
11
13
|
}
|
|
12
14
|
|
|
13
|
-
MakeFromData(
|
|
14
|
-
|
|
15
|
+
MakeFromData(data: JsiSkData): SkSVG | null {
|
|
16
|
+
const decoder = new TextDecoder("utf-8");
|
|
17
|
+
const str = decoder.decode(data.ref);
|
|
18
|
+
return this.MakeFromString(str);
|
|
15
19
|
}
|
|
16
20
|
|
|
17
|
-
MakeFromString(
|
|
18
|
-
|
|
21
|
+
MakeFromString(str: string): SkSVG | null {
|
|
22
|
+
const parser = new DOMParser();
|
|
23
|
+
const svgDoc = parser.parseFromString(str, "image/svg+xml");
|
|
24
|
+
const svgElement = svgDoc.documentElement;
|
|
25
|
+
|
|
26
|
+
const attrWidth = svgElement.getAttribute("width");
|
|
27
|
+
const attrHeight = svgElement.getAttribute("height");
|
|
28
|
+
let width = attrWidth ? parseFloat(attrWidth) : null;
|
|
29
|
+
let height = attrHeight ? parseFloat(attrHeight) : null;
|
|
30
|
+
|
|
31
|
+
const svgDataUrl =
|
|
32
|
+
"data:image/svg+xml;charset=utf-8," + encodeURIComponent(str);
|
|
33
|
+
// Create a new HTMLImageElement
|
|
34
|
+
const img = new Image();
|
|
35
|
+
img.src = svgDataUrl;
|
|
36
|
+
|
|
37
|
+
// Optionally set styles or attributes on the image
|
|
38
|
+
img.style.display = "none";
|
|
39
|
+
img.alt = "SVG Image";
|
|
40
|
+
if (!width || !height) {
|
|
41
|
+
const viewBox = svgElement.getAttribute("viewBox");
|
|
42
|
+
if (viewBox) {
|
|
43
|
+
const viewBoxValues = viewBox.split(" ");
|
|
44
|
+
if (viewBoxValues.length === 4) {
|
|
45
|
+
width = width || parseFloat(viewBoxValues[2]);
|
|
46
|
+
height = height || parseFloat(viewBoxValues[3]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (width && height) {
|
|
51
|
+
img.width = width;
|
|
52
|
+
img.height = height;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
img.onerror = (e) => {
|
|
56
|
+
console.error("SVG failed to load", e);
|
|
57
|
+
};
|
|
58
|
+
document.body.appendChild(img);
|
|
59
|
+
return new JsiSkSVG(this.CanvasKit, img);
|
|
19
60
|
}
|
|
20
61
|
}
|