@shopify/react-native-skia 0.1.238 → 0.1.239
Sign up to get free protection for your applications and to get access to all the features.
- package/cpp/rnskia/dom/nodes/JsiAtlasNode.h +0 -1
- package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +4 -3
- package/ios/RNSkia-iOS/SkiaManager.h +3 -1
- package/ios/RNSkia-iOS/SkiaManager.mm +6 -5
- package/ios/RNSkiaModule.mm +7 -1
- package/lib/commonjs/external/reanimated/buffers.d.ts +6 -4
- package/lib/commonjs/external/reanimated/buffers.js +16 -6
- package/lib/commonjs/external/reanimated/buffers.js.map +1 -1
- package/lib/commonjs/external/reanimated/textures.d.ts +6 -3
- package/lib/commonjs/external/reanimated/textures.js +52 -7
- package/lib/commonjs/external/reanimated/textures.js.map +1 -1
- package/lib/module/external/reanimated/buffers.d.ts +6 -4
- package/lib/module/external/reanimated/buffers.js +18 -8
- package/lib/module/external/reanimated/buffers.js.map +1 -1
- package/lib/module/external/reanimated/textures.d.ts +6 -3
- package/lib/module/external/reanimated/textures.js +46 -4
- package/lib/module/external/reanimated/textures.js.map +1 -1
- package/lib/typescript/src/external/reanimated/buffers.d.ts +6 -4
- package/lib/typescript/src/external/reanimated/textures.d.ts +6 -3
- package/package.json +1 -1
- package/src/external/reanimated/buffers.ts +29 -26
- package/src/external/reanimated/textures.tsx +55 -6
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
#import <React/RCTBridge+Private.h>
|
4
4
|
#import <React/RCTBridge.h>
|
5
|
-
#import <ReactCommon/RCTTurboModule.h>
|
6
5
|
|
7
6
|
#include <functional>
|
8
7
|
#include <memory>
|
@@ -30,8 +29,10 @@ static void handleNotification(CFNotificationCenterRef center, void *observer,
|
|
30
29
|
|
31
30
|
class RNSkiOSPlatformContext : public RNSkPlatformContext {
|
32
31
|
public:
|
33
|
-
RNSkiOSPlatformContext(
|
34
|
-
|
32
|
+
RNSkiOSPlatformContext(
|
33
|
+
jsi::Runtime *runtime, RCTBridge *bridge,
|
34
|
+
std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker)
|
35
|
+
: RNSkPlatformContext(runtime, jsCallInvoker,
|
35
36
|
[[UIScreen mainScreen] scale]) {
|
36
37
|
|
37
38
|
// We need to make sure we invalidate when modules are freed
|
@@ -6,8 +6,6 @@
|
|
6
6
|
#import <React/RCTBridge.h>
|
7
7
|
#import <React/RCTUIManager.h>
|
8
8
|
|
9
|
-
#import <ReactCommon/RCTTurboModule.h>
|
10
|
-
|
11
9
|
#import "RNSkiOSPlatformContext.h"
|
12
10
|
|
13
11
|
@implementation SkiaManager {
|
@@ -26,7 +24,9 @@
|
|
26
24
|
_skManager = nullptr;
|
27
25
|
}
|
28
26
|
|
29
|
-
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
27
|
+
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
28
|
+
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)
|
29
|
+
jsInvoker {
|
30
30
|
self = [super init];
|
31
31
|
if (self) {
|
32
32
|
RCTCxxBridge *cxxBridge = (RCTCxxBridge *)bridge;
|
@@ -37,8 +37,9 @@
|
|
37
37
|
|
38
38
|
// Create the RNSkiaManager (cross platform)
|
39
39
|
_skManager = std::make_shared<RNSkia::RNSkManager>(
|
40
|
-
jsRuntime,
|
41
|
-
std::make_shared<RNSkia::RNSkiOSPlatformContext>(jsRuntime, bridge
|
40
|
+
jsRuntime, jsInvoker,
|
41
|
+
std::make_shared<RNSkia::RNSkiOSPlatformContext>(jsRuntime, bridge,
|
42
|
+
jsInvoker));
|
42
43
|
}
|
43
44
|
}
|
44
45
|
return self;
|
package/ios/RNSkiaModule.mm
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
|
2
2
|
#import "RNSkiaModule.h"
|
3
3
|
#import <React/RCTBridge+Private.h>
|
4
|
+
#import <ReactCommon/RCTTurboModule.h>
|
4
5
|
|
5
6
|
@implementation RNSkiaModule {
|
6
7
|
SkiaManager *skiaManager;
|
8
|
+
std::shared_ptr<facebook::react::CallInvoker> jsInvoker;
|
7
9
|
}
|
8
10
|
|
9
11
|
RCT_EXPORT_MODULE()
|
@@ -33,13 +35,17 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
|
|
33
35
|
return @true;
|
34
36
|
}
|
35
37
|
RCTBridge *bridge = [RCTBridge currentBridge];
|
36
|
-
|
38
|
+
if (!jsInvoker) {
|
39
|
+
jsInvoker = bridge.jsCallInvoker;
|
40
|
+
}
|
41
|
+
skiaManager = [[SkiaManager alloc] initWithBridge:bridge jsInvoker:jsInvoker];
|
37
42
|
return @true;
|
38
43
|
}
|
39
44
|
|
40
45
|
#ifdef RCT_NEW_ARCH_ENABLED
|
41
46
|
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
42
47
|
(const facebook::react::ObjCTurboModule::InitParams &)params {
|
48
|
+
jsInvoker = params.jsInvoker;
|
43
49
|
return std::make_shared<facebook::react::NativeSkiaModuleSpecJSI>(params);
|
44
50
|
}
|
45
51
|
#endif
|
@@ -1,5 +1,7 @@
|
|
1
1
|
import type { SkColor, SkHostRect, SkPoint, SkRSXform } from "../../skia/types";
|
2
|
-
|
3
|
-
export declare const
|
4
|
-
export declare const
|
5
|
-
export declare const
|
2
|
+
type Modifier<T> = (input: T, index: number) => void;
|
3
|
+
export declare const useRectBuffer: (size: number, modifier: Modifier<SkHostRect>) => import("react-native-reanimated").SharedValue<SkHostRect[]>;
|
4
|
+
export declare const useRSXformBuffer: (size: number, modifier: Modifier<SkRSXform>) => import("react-native-reanimated").SharedValue<SkRSXform[]>;
|
5
|
+
export declare const usePointBuffer: (size: number, modifier: Modifier<SkPoint>) => import("react-native-reanimated").SharedValue<SkPoint[]>;
|
6
|
+
export declare const useColorBuffer: (size: number, modifier: Modifier<SkColor>) => import("react-native-reanimated").SharedValue<Float32Array[]>;
|
7
|
+
export {};
|
@@ -5,22 +5,32 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.useRectBuffer = exports.useRSXformBuffer = exports.usePointBuffer = exports.useColorBuffer = void 0;
|
7
7
|
var _react = require("react");
|
8
|
+
var _reactNativeReanimated = require("react-native-reanimated");
|
8
9
|
var _skia = require("../../skia");
|
9
10
|
var _moduleWrapper = require("./moduleWrapper");
|
10
11
|
var _interpolators = require("./interpolators");
|
11
12
|
const useBuffer = (size, bufferInitializer, modifier) => {
|
13
|
+
var _mod$__closure;
|
12
14
|
const buffer = (0, _react.useMemo)(() => new Array(size).fill(0).map(bufferInitializer),
|
13
15
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
14
16
|
[size]);
|
15
|
-
const
|
16
|
-
|
17
|
+
const values = (0, _moduleWrapper.useSharedValue)(buffer);
|
18
|
+
const mod = modifier;
|
19
|
+
const deps = Object.values((_mod$__closure = mod.__closure) !== null && _mod$__closure !== void 0 ? _mod$__closure : {});
|
20
|
+
const mapperId = (0, _reactNativeReanimated.startMapper)(() => {
|
21
|
+
"worklet";
|
22
|
+
|
17
23
|
buffer.forEach((val, index) => {
|
18
24
|
modifier(val, index);
|
19
25
|
});
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
26
|
+
(0, _interpolators.notifyChange)(values);
|
27
|
+
}, deps);
|
28
|
+
(0, _react.useEffect)(() => {
|
29
|
+
return () => {
|
30
|
+
(0, _reactNativeReanimated.stopMapper)(mapperId);
|
31
|
+
};
|
32
|
+
}, [mapperId]);
|
33
|
+
return values;
|
24
34
|
};
|
25
35
|
const useRectBuffer = (size, modifier) => useBuffer(size, () => _skia.Skia.XYWHRect(0, 0, 0, 0), modifier);
|
26
36
|
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_react","require","_skia","_moduleWrapper","_interpolators","useBuffer","size","bufferInitializer","modifier","buffer","useMemo","Array","fill","map","
|
1
|
+
{"version":3,"names":["_react","require","_reactNativeReanimated","_skia","_moduleWrapper","_interpolators","useBuffer","size","bufferInitializer","modifier","_mod$__closure","buffer","useMemo","Array","fill","map","values","useSharedValue","mod","deps","Object","__closure","mapperId","startMapper","forEach","val","index","notifyChange","useEffect","stopMapper","useRectBuffer","Skia","XYWHRect","exports","useRSXformBuffer","RSXform","usePointBuffer","Point","useColorBuffer","Color"],"sources":["buffers.ts"],"sourcesContent":["import { useEffect, useMemo } from \"react\";\nimport { startMapper, stopMapper } from \"react-native-reanimated\";\nimport type { WorkletFunction } from \"react-native-reanimated/lib/typescript/reanimated2/commonTypes\";\n\nimport type { SkColor, SkHostRect, SkPoint, SkRSXform } from \"../../skia/types\";\nimport { Skia } from \"../../skia\";\n\nimport { useSharedValue } from \"./moduleWrapper\";\nimport { notifyChange } from \"./interpolators\";\n\ntype Modifier<T> = (input: T, index: number) => void;\n\nconst useBuffer = <T>(\n size: number,\n bufferInitializer: () => T,\n modifier: Modifier<T>\n) => {\n const buffer = useMemo(\n () => new Array(size).fill(0).map(bufferInitializer),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [size]\n );\n const values = useSharedValue(buffer);\n const mod = modifier as WorkletFunction;\n const deps = Object.values(mod.__closure ?? {});\n const mapperId = startMapper(() => {\n \"worklet\";\n buffer.forEach((val, index) => {\n modifier(val, index);\n });\n notifyChange(values);\n }, deps);\n\n useEffect(() => {\n return () => {\n stopMapper(mapperId);\n };\n }, [mapperId]);\n\n return values;\n};\n\nexport const useRectBuffer = (size: number, modifier: Modifier<SkHostRect>) =>\n useBuffer(size, () => Skia.XYWHRect(0, 0, 0, 0), modifier);\n\n// Usage for RSXform Buffer\nexport const useRSXformBuffer = (size: number, modifier: Modifier<SkRSXform>) =>\n useBuffer(size, () => Skia.RSXform(1, 0, 0, 0), modifier);\n\n// Usage for Point Buffer\nexport const usePointBuffer = (size: number, modifier: Modifier<SkPoint>) =>\n useBuffer(size, () => Skia.Point(0, 0), modifier);\n\n// Usage for Color Buffer\nexport const useColorBuffer = (size: number, modifier: Modifier<SkColor>) =>\n useBuffer(size, () => Skia.Color(\"black\"), modifier);\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC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AAC,EAAEvB,QAAQ,CAAC;;AAE5D;AAAAwB,OAAA,CAAAH,aAAA,GAAAA,aAAA;AACO,MAAMI,gBAAgB,GAAGA,CAAC3B,IAAY,EAAEE,QAA6B,KAC1EH,SAAS,CAACC,IAAI,EAAE,MAAMwB,UAAI,CAACI,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE1B,QAAQ,CAAC;;AAE3D;AAAAwB,OAAA,CAAAC,gBAAA,GAAAA,gBAAA;AACO,MAAME,cAAc,GAAGA,CAAC7B,IAAY,EAAEE,QAA2B,KACtEH,SAAS,CAACC,IAAI,EAAE,MAAMwB,UAAI,CAACM,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE5B,QAAQ,CAAC;;AAEnD;AAAAwB,OAAA,CAAAG,cAAA,GAAAA,cAAA;AACO,MAAME,cAAc,GAAGA,CAAC/B,IAAY,EAAEE,QAA2B,KACtEH,SAAS,CAACC,IAAI,EAAE,MAAMwB,UAAI,CAACQ,KAAK,CAAC,OAAO,CAAC,EAAE9B,QAAQ,CAAC;AAACwB,OAAA,CAAAK,cAAA,GAAAA,cAAA"}
|
@@ -1,5 +1,8 @@
|
|
1
1
|
import type { ReactElement } from "react";
|
2
2
|
import type { SharedValue } from "react-native-reanimated";
|
3
|
-
import type { SkImage, SkPicture, SkSize } from "../../skia/types";
|
4
|
-
export declare const
|
5
|
-
export declare const
|
3
|
+
import type { DataSourceParam, SkImage, SkPicture, SkSize } from "../../skia/types";
|
4
|
+
export declare const useTexture: (element: ReactElement, size: SkSize) => SharedValue<SkImage | null>;
|
5
|
+
export declare const useTextureAsValue: (element: ReactElement, size: SkSize) => SharedValue<SkImage | null>;
|
6
|
+
export declare const useTextureValueFromPicture: (picture: SkPicture | null, size: SkSize) => SharedValue<SkImage | null>;
|
7
|
+
export declare const usePictureAsTexture: (picture: SkPicture | null, size: SkSize) => SharedValue<SkImage | null>;
|
8
|
+
export declare const useImageAsTexture: (source: DataSourceParam) => SharedValue<SkImage | null>;
|
@@ -3,28 +3,73 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.useTextureValueFromPicture = exports.
|
6
|
+
exports.useTextureValueFromPicture = exports.useTextureAsValue = exports.useTexture = exports.usePictureAsTexture = exports.useImageAsTexture = void 0;
|
7
7
|
var _react = require("react");
|
8
8
|
var _Offscreen = require("../../renderer/Offscreen");
|
9
|
+
var _skia = require("../../skia");
|
9
10
|
var _moduleWrapper = require("./moduleWrapper");
|
10
|
-
const
|
11
|
+
const createTexture = (texture, picture, size) => {
|
11
12
|
"worklet";
|
12
13
|
|
13
14
|
texture.value = (0, _Offscreen.drawAsImageFromPicture)(picture, size);
|
14
15
|
};
|
15
|
-
const
|
16
|
+
const useTexture = (element, size) => {
|
16
17
|
const picture = (0, _react.useMemo)(() => {
|
17
18
|
return (0, _Offscreen.drawAsPicture)(element);
|
18
19
|
}, [element]);
|
19
|
-
return
|
20
|
+
return usePictureAsTexture(picture, size);
|
20
21
|
};
|
21
|
-
exports.
|
22
|
+
exports.useTexture = useTexture;
|
23
|
+
const useTextureAsValue = (element, size) => {
|
24
|
+
console.warn("useTextureAsValue has been renamed to use useTexture");
|
25
|
+
return useTexture(element, size);
|
26
|
+
};
|
27
|
+
exports.useTextureAsValue = useTextureAsValue;
|
22
28
|
const useTextureValueFromPicture = (picture, size) => {
|
29
|
+
console.warn("useTextureValueFromPicture has been renamed to use usePictureAsTexture");
|
30
|
+
return usePictureAsTexture(picture, size);
|
31
|
+
};
|
32
|
+
exports.useTextureValueFromPicture = useTextureValueFromPicture;
|
33
|
+
const usePictureAsTexture = (picture, size) => {
|
23
34
|
const texture = (0, _moduleWrapper.useSharedValue)(null);
|
24
35
|
(0, _react.useEffect)(() => {
|
25
|
-
|
36
|
+
if (picture !== null) {
|
37
|
+
(0, _moduleWrapper.runOnUI)(createTexture)(texture, picture, size);
|
38
|
+
}
|
26
39
|
}, [texture, picture, size]);
|
27
40
|
return texture;
|
28
41
|
};
|
29
|
-
exports.
|
42
|
+
exports.usePictureAsTexture = usePictureAsTexture;
|
43
|
+
const useImageAsTexture = source => {
|
44
|
+
const image = (0, _skia.useImage)(source);
|
45
|
+
const size = (0, _react.useMemo)(() => {
|
46
|
+
if (image) {
|
47
|
+
return {
|
48
|
+
width: image.width(),
|
49
|
+
height: image.height()
|
50
|
+
};
|
51
|
+
}
|
52
|
+
return {
|
53
|
+
width: 0,
|
54
|
+
height: 0
|
55
|
+
};
|
56
|
+
}, [image]);
|
57
|
+
const picture = (0, _react.useMemo)(() => {
|
58
|
+
if (image) {
|
59
|
+
const recorder = _skia.Skia.PictureRecorder();
|
60
|
+
const canvas = recorder.beginRecording({
|
61
|
+
x: 0,
|
62
|
+
y: 0,
|
63
|
+
width: size.width,
|
64
|
+
height: size.height
|
65
|
+
});
|
66
|
+
canvas.drawImage(image, 0, 0);
|
67
|
+
return recorder.finishRecordingAsPicture();
|
68
|
+
} else {
|
69
|
+
return null;
|
70
|
+
}
|
71
|
+
}, [size, image]);
|
72
|
+
return usePictureAsTexture(picture, size);
|
73
|
+
};
|
74
|
+
exports.useImageAsTexture = useImageAsTexture;
|
30
75
|
//# sourceMappingURL=textures.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_react","require","_Offscreen","_moduleWrapper","
|
1
|
+
{"version":3,"names":["_react","require","_Offscreen","_skia","_moduleWrapper","createTexture","texture","picture","size","value","drawAsImageFromPicture","useTexture","element","useMemo","drawAsPicture","usePictureAsTexture","exports","useTextureAsValue","console","warn","useTextureValueFromPicture","useSharedValue","useEffect","runOnUI","useImageAsTexture","source","image","useImage","width","height","recorder","Skia","PictureRecorder","canvas","beginRecording","x","y","drawImage","finishRecordingAsPicture"],"sources":["textures.tsx"],"sourcesContent":["import { useEffect, useMemo } from \"react\";\nimport type { ReactElement } from \"react\";\nimport type { SharedValue } from \"react-native-reanimated\";\n\nimport type {\n DataSourceParam,\n SkImage,\n SkPicture,\n SkSize,\n} from \"../../skia/types\";\nimport {\n drawAsImageFromPicture,\n drawAsPicture,\n} from \"../../renderer/Offscreen\";\nimport { Skia, useImage } from \"../../skia\";\n\nimport { runOnUI, useSharedValue } from \"./moduleWrapper\";\n\nconst createTexture = (\n texture: SharedValue<SkImage | null>,\n picture: SkPicture,\n size: SkSize\n) => {\n \"worklet\";\n texture.value = drawAsImageFromPicture(picture, size);\n};\n\nexport const useTexture = (element: ReactElement, size: SkSize) => {\n const picture = useMemo(() => {\n return drawAsPicture(element);\n }, [element]);\n return usePictureAsTexture(picture, size);\n};\n\nexport const useTextureAsValue = (element: ReactElement, size: SkSize) => {\n console.warn(\"useTextureAsValue has been renamed to use useTexture\");\n return useTexture(element, size);\n};\n\nexport const useTextureValueFromPicture = (\n picture: SkPicture | null,\n size: SkSize\n) => {\n console.warn(\n \"useTextureValueFromPicture has been renamed to use usePictureAsTexture\"\n );\n return usePictureAsTexture(picture, size);\n};\n\nexport const usePictureAsTexture = (\n picture: SkPicture | null,\n size: SkSize\n) => {\n const texture = useSharedValue<SkImage | null>(null);\n useEffect(() => {\n if (picture !== null) {\n runOnUI(createTexture)(texture, picture, size);\n }\n }, [texture, picture, size]);\n return texture;\n};\n\nexport const useImageAsTexture = (source: DataSourceParam) => {\n const image = useImage(source);\n const size = useMemo(() => {\n if (image) {\n return { width: image.width(), height: image.height() };\n }\n return { width: 0, height: 0 };\n }, [image]);\n const picture = useMemo(() => {\n if (image) {\n const recorder = Skia.PictureRecorder();\n const canvas = recorder.beginRecording({\n x: 0,\n y: 0,\n width: size.width,\n height: size.height,\n });\n canvas.drawImage(image, 0, 0);\n return recorder.finishRecordingAsPicture();\n } else {\n return null;\n }\n }, [size, image]);\n return usePictureAsTexture(picture, size);\n};\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAUA,IAAAC,UAAA,GAAAD,OAAA;AAIA,IAAAE,KAAA,GAAAF,OAAA;AAEA,IAAAG,cA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SXform } from "../../skia/types";
|
2
|
-
|
3
|
-
export declare const
|
4
|
-
export declare const
|
5
|
-
export declare const
|
2
|
+
type Modifier<T> = (input: T, index: number) => void;
|
3
|
+
export declare const useRectBuffer: (size: number, modifier: Modifier<SkHostRect>) => import("react-native-reanimated").SharedValue<SkHostRect[]>;
|
4
|
+
export declare const useRSXformBuffer: (size: number, modifier: Modifier<SkRSXform>) => import("react-native-reanimated").SharedValue<SkRSXform[]>;
|
5
|
+
export declare const usePointBuffer: (size: number, modifier: Modifier<SkPoint>) => import("react-native-reanimated").SharedValue<SkPoint[]>;
|
6
|
+
export declare const useColorBuffer: (size: number, modifier: Modifier<SkColor>) => import("react-native-reanimated").SharedValue<Float32Array[]>;
|
7
|
+
export {};
|
@@ -1,20 +1,30 @@
|
|
1
|
-
import { useMemo } from "react";
|
1
|
+
import { useEffect, useMemo } from "react";
|
2
|
+
import { startMapper, stopMapper } from "react-native-reanimated";
|
2
3
|
import { Skia } from "../../skia";
|
3
|
-
import {
|
4
|
+
import { useSharedValue } from "./moduleWrapper";
|
4
5
|
import { notifyChange } from "./interpolators";
|
5
6
|
const useBuffer = (size, bufferInitializer, modifier) => {
|
7
|
+
var _mod$__closure;
|
6
8
|
const buffer = useMemo(() => new Array(size).fill(0).map(bufferInitializer),
|
7
9
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
8
10
|
[size]);
|
9
|
-
const
|
10
|
-
|
11
|
+
const values = useSharedValue(buffer);
|
12
|
+
const mod = modifier;
|
13
|
+
const deps = Object.values((_mod$__closure = mod.__closure) !== null && _mod$__closure !== void 0 ? _mod$__closure : {});
|
14
|
+
const mapperId = startMapper(() => {
|
15
|
+
"worklet";
|
16
|
+
|
11
17
|
buffer.forEach((val, index) => {
|
12
18
|
modifier(val, index);
|
13
19
|
});
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
20
|
+
notifyChange(values);
|
21
|
+
}, deps);
|
22
|
+
useEffect(() => {
|
23
|
+
return () => {
|
24
|
+
stopMapper(mapperId);
|
25
|
+
};
|
26
|
+
}, [mapperId]);
|
27
|
+
return values;
|
18
28
|
};
|
19
29
|
export const useRectBuffer = (size, modifier) => useBuffer(size, () => Skia.XYWHRect(0, 0, 0, 0), modifier);
|
20
30
|
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["useMemo","
|
1
|
+
{"version":3,"names":["useEffect","useMemo","startMapper","stopMapper","Skia","useSharedValue","notifyChange","useBuffer","size","bufferInitializer","modifier","_mod$__closure","buffer","Array","fill","map","values","mod","deps","Object","__closure","mapperId","forEach","val","index","useRectBuffer","XYWHRect","useRSXformBuffer","RSXform","usePointBuffer","Point","useColorBuffer","Color"],"sources":["buffers.ts"],"sourcesContent":["import { useEffect, useMemo } from \"react\";\nimport { startMapper, stopMapper } from \"react-native-reanimated\";\nimport type { WorkletFunction } from \"react-native-reanimated/lib/typescript/reanimated2/commonTypes\";\n\nimport type { SkColor, SkHostRect, SkPoint, SkRSXform } from \"../../skia/types\";\nimport { Skia } from \"../../skia\";\n\nimport { useSharedValue } from \"./moduleWrapper\";\nimport { notifyChange } from \"./interpolators\";\n\ntype Modifier<T> = (input: T, index: number) => void;\n\nconst useBuffer = <T>(\n size: number,\n bufferInitializer: () => T,\n modifier: Modifier<T>\n) => {\n const buffer = useMemo(\n () => new Array(size).fill(0).map(bufferInitializer),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [size]\n );\n const values = useSharedValue(buffer);\n const mod = modifier as WorkletFunction;\n const deps = Object.values(mod.__closure ?? {});\n const mapperId = startMapper(() => {\n \"worklet\";\n buffer.forEach((val, index) => {\n modifier(val, index);\n });\n notifyChange(values);\n }, deps);\n\n useEffect(() => {\n return () => {\n stopMapper(mapperId);\n };\n }, [mapperId]);\n\n return values;\n};\n\nexport const useRectBuffer = (size: number, modifier: Modifier<SkHostRect>) =>\n useBuffer(size, () => Skia.XYWHRect(0, 0, 0, 0), modifier);\n\n// Usage for RSXform Buffer\nexport const useRSXformBuffer = (size: number, modifier: Modifier<SkRSXform>) =>\n useBuffer(size, () => Skia.RSXform(1, 0, 0, 0), modifier);\n\n// Usage for Point Buffer\nexport const usePointBuffer = (size: number, modifier: Modifier<SkPoint>) =>\n useBuffer(size, () => Skia.Point(0, 0), modifier);\n\n// Usage for Color Buffer\nexport const useColorBuffer = (size: number, modifier: Modifier<SkColor>) =>\n useBuffer(size, () => Skia.Color(\"black\"), modifier);\n"],"mappings":"AAAA,SAASA,SAAS,EAAEC,OAAO,QAAQ,OAAO;AAC1C,SAASC,WAAW,EAAEC,UAAU,QAAQ,yBAAyB;AAIjE,SAASC,IAAI,QAAQ,YAAY;AAEjC,SAASC,cAAc,QAAQ,iBAAiB;AAChD,SAASC,YAAY,QAAQ,iBAAiB;AAI9C,MAAMC,SAAS,GAAGA,CAChBC,IAAY,EACZC,iBAA0B,EAC1BC,QAAqB,KAClB;EAAA,IAAAC,cAAA;EACH,MAAMC,MAAM,GAAGX,OAAO,CACpB,MAAM,IAAIY,KAAK,CAACL,IAAI,CAAC,CAACM,IAAI,CAAC,CAAC,CAAC,CAACC,GAAG,CAACN,iBAAiB,CAAC;EACpD;EACA,CAACD,IAAI,CACP,CAAC;EACD,MAAMQ,MAAM,GAAGX,cAAc,CAACO,MAAM,CAAC;EACrC,MAAMK,GAAG,GAAGP,QAA2B;EACvC,MAAMQ,IAAI,GAAGC,MAAM,CAACH,MAAM,EAAAL,cAAA,GAACM,GAAG,CAACG,SAAS,cAAAT,cAAA,cAAAA,cAAA,GAAI,CAAC,CAAC,CAAC;EAC/C,MAAMU,QAAQ,GAAGnB,WAAW,CAAC,MAAM;IACjC,SAAS;;IACTU,MAAM,CAACU,OAAO,CAAC,CAACC,GAAG,EAAEC,KAAK,KAAK;MAC7Bd,QAAQ,CAACa,GAAG,EAAEC,KAAK,CAAC;IACtB,CAAC,CAAC;IACFlB,YAAY,CAACU,MAAM,CAAC;EACtB,CAAC,EAAEE,IAAI,CAAC;EAERlB,SAAS,CAAC,MAAM;IACd,OAAO,MAAM;MACXG,UAAU,CAACkB,QAAQ,CAAC;IACtB,CAAC;EACH,CAAC,EAAE,CAACA,QAAQ,CAAC,CAAC;EAEd,OAAOL,MAAM;AACf,CAAC;AAED,OAAO,MAAMS,aAAa,GAAGA,CAACjB,IAAY,EAAEE,QAA8B,KACxEH,SAAS,CAACC,IAAI,EAAE,MAAMJ,IAAI,CAACsB,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAEhB,QAAQ,CAAC;;AAE5D;AACA,OAAO,MAAMiB,gBAAgB,GAAGA,CAACnB,IAAY,EAAEE,QAA6B,KAC1EH,SAAS,CAACC,IAAI,EAAE,MAAMJ,IAAI,CAACwB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAElB,QAAQ,CAAC;;AAE3D;AACA,OAAO,MAAMmB,cAAc,GAAGA,CAACrB,IAAY,EAAEE,QAA2B,KACtEH,SAAS,CAACC,IAAI,EAAE,MAAMJ,IAAI,CAAC0B,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAEpB,QAAQ,CAAC;;AAEnD;AACA,OAAO,MAAMqB,cAAc,GAAGA,CAACvB,IAAY,EAAEE,QAA2B,KACtEH,SAAS,CAACC,IAAI,EAAE,MAAMJ,IAAI,CAAC4B,KAAK,CAAC,OAAO,CAAC,EAAEtB,QAAQ,CAAC"}
|
@@ -1,5 +1,8 @@
|
|
1
1
|
import type { ReactElement } from "react";
|
2
2
|
import type { SharedValue } from "react-native-reanimated";
|
3
|
-
import type { SkImage, SkPicture, SkSize } from "../../skia/types";
|
4
|
-
export declare const
|
5
|
-
export declare const
|
3
|
+
import type { DataSourceParam, SkImage, SkPicture, SkSize } from "../../skia/types";
|
4
|
+
export declare const useTexture: (element: ReactElement, size: SkSize) => SharedValue<SkImage | null>;
|
5
|
+
export declare const useTextureAsValue: (element: ReactElement, size: SkSize) => SharedValue<SkImage | null>;
|
6
|
+
export declare const useTextureValueFromPicture: (picture: SkPicture | null, size: SkSize) => SharedValue<SkImage | null>;
|
7
|
+
export declare const usePictureAsTexture: (picture: SkPicture | null, size: SkSize) => SharedValue<SkImage | null>;
|
8
|
+
export declare const useImageAsTexture: (source: DataSourceParam) => SharedValue<SkImage | null>;
|
@@ -1,22 +1,64 @@
|
|
1
1
|
import { useEffect, useMemo } from "react";
|
2
2
|
import { drawAsImageFromPicture, drawAsPicture } from "../../renderer/Offscreen";
|
3
|
+
import { Skia, useImage } from "../../skia";
|
3
4
|
import { runOnUI, useSharedValue } from "./moduleWrapper";
|
4
|
-
const
|
5
|
+
const createTexture = (texture, picture, size) => {
|
5
6
|
"worklet";
|
6
7
|
|
7
8
|
texture.value = drawAsImageFromPicture(picture, size);
|
8
9
|
};
|
9
|
-
export const
|
10
|
+
export const useTexture = (element, size) => {
|
10
11
|
const picture = useMemo(() => {
|
11
12
|
return drawAsPicture(element);
|
12
13
|
}, [element]);
|
13
|
-
return
|
14
|
+
return usePictureAsTexture(picture, size);
|
15
|
+
};
|
16
|
+
export const useTextureAsValue = (element, size) => {
|
17
|
+
console.warn("useTextureAsValue has been renamed to use useTexture");
|
18
|
+
return useTexture(element, size);
|
14
19
|
};
|
15
20
|
export const useTextureValueFromPicture = (picture, size) => {
|
21
|
+
console.warn("useTextureValueFromPicture has been renamed to use usePictureAsTexture");
|
22
|
+
return usePictureAsTexture(picture, size);
|
23
|
+
};
|
24
|
+
export const usePictureAsTexture = (picture, size) => {
|
16
25
|
const texture = useSharedValue(null);
|
17
26
|
useEffect(() => {
|
18
|
-
|
27
|
+
if (picture !== null) {
|
28
|
+
runOnUI(createTexture)(texture, picture, size);
|
29
|
+
}
|
19
30
|
}, [texture, picture, size]);
|
20
31
|
return texture;
|
21
32
|
};
|
33
|
+
export const useImageAsTexture = source => {
|
34
|
+
const image = useImage(source);
|
35
|
+
const size = useMemo(() => {
|
36
|
+
if (image) {
|
37
|
+
return {
|
38
|
+
width: image.width(),
|
39
|
+
height: image.height()
|
40
|
+
};
|
41
|
+
}
|
42
|
+
return {
|
43
|
+
width: 0,
|
44
|
+
height: 0
|
45
|
+
};
|
46
|
+
}, [image]);
|
47
|
+
const picture = useMemo(() => {
|
48
|
+
if (image) {
|
49
|
+
const recorder = Skia.PictureRecorder();
|
50
|
+
const canvas = recorder.beginRecording({
|
51
|
+
x: 0,
|
52
|
+
y: 0,
|
53
|
+
width: size.width,
|
54
|
+
height: size.height
|
55
|
+
});
|
56
|
+
canvas.drawImage(image, 0, 0);
|
57
|
+
return recorder.finishRecordingAsPicture();
|
58
|
+
} else {
|
59
|
+
return null;
|
60
|
+
}
|
61
|
+
}, [size, image]);
|
62
|
+
return usePictureAsTexture(picture, size);
|
63
|
+
};
|
22
64
|
//# sourceMappingURL=textures.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["useEffect","useMemo","drawAsImageFromPicture","drawAsPicture","runOnUI","useSharedValue","
|
1
|
+
{"version":3,"names":["useEffect","useMemo","drawAsImageFromPicture","drawAsPicture","Skia","useImage","runOnUI","useSharedValue","createTexture","texture","picture","size","value","useTexture","element","usePictureAsTexture","useTextureAsValue","console","warn","useTextureValueFromPicture","useImageAsTexture","source","image","width","height","recorder","PictureRecorder","canvas","beginRecording","x","y","drawImage","finishRecordingAsPicture"],"sources":["textures.tsx"],"sourcesContent":["import { useEffect, useMemo } from \"react\";\nimport type { ReactElement } from \"react\";\nimport type { SharedValue } from \"react-native-reanimated\";\n\nimport type {\n DataSourceParam,\n SkImage,\n SkPicture,\n SkSize,\n} from \"../../skia/types\";\nimport {\n drawAsImageFromPicture,\n drawAsPicture,\n} from \"../../renderer/Offscreen\";\nimport { Skia, useImage } from \"../../skia\";\n\nimport { runOnUI, useSharedValue } from \"./moduleWrapper\";\n\nconst createTexture = (\n texture: SharedValue<SkImage | null>,\n picture: SkPicture,\n size: SkSize\n) => {\n \"worklet\";\n texture.value = drawAsImageFromPicture(picture, size);\n};\n\nexport const useTexture = (element: ReactElement, size: SkSize) => {\n const picture = useMemo(() => {\n return drawAsPicture(element);\n }, [element]);\n return usePictureAsTexture(picture, size);\n};\n\nexport const useTextureAsValue = (element: ReactElement, size: SkSize) => {\n console.warn(\"useTextureAsValue has been renamed to use useTexture\");\n return useTexture(element, size);\n};\n\nexport const useTextureValueFromPicture = (\n picture: SkPicture | null,\n size: SkSize\n) => {\n console.warn(\n \"useTextureValueFromPicture has been renamed to use usePictureAsTexture\"\n );\n return usePictureAsTexture(picture, size);\n};\n\nexport const usePictureAsTexture = (\n picture: SkPicture | null,\n size: SkSize\n) => {\n const texture = useSharedValue<SkImage | null>(null);\n useEffect(() => {\n if (picture !== null) {\n runOnUI(createTexture)(texture, picture, size);\n }\n }, [texture, picture, size]);\n return texture;\n};\n\nexport const useImageAsTexture = (source: DataSourceParam) => {\n const image = useImage(source);\n const size = useMemo(() => {\n if (image) {\n return { width: image.width(), height: image.height() };\n }\n return { width: 0, height: 0 };\n }, [image]);\n const picture = useMemo(() => {\n if (image) {\n const recorder = Skia.PictureRecorder();\n const canvas = recorder.beginRecording({\n x: 0,\n y: 0,\n width: size.width,\n height: size.height,\n });\n canvas.drawImage(image, 0, 0);\n return recorder.finishRecordingAsPicture();\n } else {\n return null;\n }\n }, [size, image]);\n return usePictureAsTexture(picture, size);\n};\n"],"mappings":"AAAA,SAASA,SAAS,EAAEC,OAAO,QAAQ,OAAO;AAU1C,SACEC,sBAAsB,EACtBC,aAAa,QACR,0BAA0B;AACjC,SAASC,IAAI,EAAEC,QAAQ,QAAQ,YAAY;AAE3C,SAASC,OAAO,EAAEC,cAAc,QAAQ,iBAAiB;AAEzD,MAAMC,aAAa,GAAGA,CACpBC,OAAoC,EACpCC,OAAkB,EAClBC,IAAY,KACT;EACH,SAAS;;EACTF,OAAO,CAACG,KAAK,GAAGV,sBAAsB,CAACQ,OAAO,EAAEC,IAAI,CAAC;AACvD,CAAC;AAED,OAAO,MAAME,UAAU,GAAGA,CAACC,OAAqB,EAAEH,IAAY,KAAK;EACj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EC,MAAM,EAAE;IAAE,CAAC;EAChC,CAAC,EAAE,CAACF,KAAK,CAAC,CAAC;EACX,MAAMZ,OAAO,GAAGT,OAAO,CAAC,MAAM;IAC5B,IAAIqB,KAAK,EAAE;MACT,MAAMG,QAAQ,GAAGrB,IAAI,CAACsB,eAAe,CAAC,CAAC;MACvC,MAAMC,MAAM,GAAGF,QAAQ,CAACG,cAAc,CAAC;QACrCC,CAAC,EAAE,CAAC;QACJC,CAAC,EAAE,CAAC;QACJP,KAAK,EAAEZ,IAAI,CAACY,KAAK;QACjBC,MAAM,EAAEb,IAAI,CAACa;MACf,CAAC,CAAC;MACFG,MAAM,CAACI,SAAS,CAACT,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;MAC7B,OAAOG,QAAQ,CAACO,wBAAwB,CAAC,CAAC;IAC5C,CAAC,MAAM;MACL,OAAO,IAAI;IACb;EACF,CAAC,EAAE,CAACrB,IAAI,EAAEW,KAAK,CAAC,CAAC;EACjB,OAAOP,mBAAmB,CAACL,OAAO,EAAEC,IAAI,CAAC;AAC3C,CAAC"}
|
@@ -1,5 +1,7 @@
|
|
1
1
|
import type { SkColor, SkHostRect, SkPoint, SkRSXform } from "../../skia/types";
|
2
|
-
|
3
|
-
export declare const
|
4
|
-
export declare const
|
5
|
-
export declare const
|
2
|
+
type Modifier<T> = (input: T, index: number) => void;
|
3
|
+
export declare const useRectBuffer: (size: number, modifier: Modifier<SkHostRect>) => import("react-native-reanimated").SharedValue<SkHostRect[]>;
|
4
|
+
export declare const useRSXformBuffer: (size: number, modifier: Modifier<SkRSXform>) => import("react-native-reanimated").SharedValue<SkRSXform[]>;
|
5
|
+
export declare const usePointBuffer: (size: number, modifier: Modifier<SkPoint>) => import("react-native-reanimated").SharedValue<SkPoint[]>;
|
6
|
+
export declare const useColorBuffer: (size: number, modifier: Modifier<SkColor>) => import("react-native-reanimated").SharedValue<Float32Array[]>;
|
7
|
+
export {};
|
@@ -1,5 +1,8 @@
|
|
1
1
|
import type { ReactElement } from "react";
|
2
2
|
import type { SharedValue } from "react-native-reanimated";
|
3
|
-
import type { SkImage, SkPicture, SkSize } from "../../skia/types";
|
4
|
-
export declare const
|
5
|
-
export declare const
|
3
|
+
import type { DataSourceParam, SkImage, SkPicture, SkSize } from "../../skia/types";
|
4
|
+
export declare const useTexture: (element: ReactElement, size: SkSize) => SharedValue<SkImage | null>;
|
5
|
+
export declare const useTextureAsValue: (element: ReactElement, size: SkSize) => SharedValue<SkImage | null>;
|
6
|
+
export declare const useTextureValueFromPicture: (picture: SkPicture | null, size: SkSize) => SharedValue<SkImage | null>;
|
7
|
+
export declare const usePictureAsTexture: (picture: SkPicture | null, size: SkSize) => SharedValue<SkImage | null>;
|
8
|
+
export declare const useImageAsTexture: (source: DataSourceParam) => SharedValue<SkImage | null>;
|
package/package.json
CHANGED
@@ -1,53 +1,56 @@
|
|
1
|
-
import { useMemo } from "react";
|
1
|
+
import { useEffect, useMemo } from "react";
|
2
|
+
import { startMapper, stopMapper } from "react-native-reanimated";
|
3
|
+
import type { WorkletFunction } from "react-native-reanimated/lib/typescript/reanimated2/commonTypes";
|
2
4
|
|
3
5
|
import type { SkColor, SkHostRect, SkPoint, SkRSXform } from "../../skia/types";
|
4
6
|
import { Skia } from "../../skia";
|
5
7
|
|
6
|
-
import {
|
8
|
+
import { useSharedValue } from "./moduleWrapper";
|
7
9
|
import { notifyChange } from "./interpolators";
|
8
10
|
|
11
|
+
type Modifier<T> = (input: T, index: number) => void;
|
12
|
+
|
9
13
|
const useBuffer = <T>(
|
10
14
|
size: number,
|
11
15
|
bufferInitializer: () => T,
|
12
|
-
modifier:
|
16
|
+
modifier: Modifier<T>
|
13
17
|
) => {
|
14
18
|
const buffer = useMemo(
|
15
19
|
() => new Array(size).fill(0).map(bufferInitializer),
|
16
20
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
17
21
|
[size]
|
18
22
|
);
|
19
|
-
const
|
20
|
-
|
21
|
-
|
23
|
+
const values = useSharedValue(buffer);
|
24
|
+
const mod = modifier as WorkletFunction;
|
25
|
+
const deps = Object.values(mod.__closure ?? {});
|
26
|
+
const mapperId = startMapper(() => {
|
27
|
+
"worklet";
|
22
28
|
buffer.forEach((val, index) => {
|
23
29
|
modifier(val, index);
|
24
30
|
});
|
25
|
-
|
26
|
-
|
27
|
-
});
|
31
|
+
notifyChange(values);
|
32
|
+
}, deps);
|
28
33
|
|
29
|
-
|
34
|
+
useEffect(() => {
|
35
|
+
return () => {
|
36
|
+
stopMapper(mapperId);
|
37
|
+
};
|
38
|
+
}, [mapperId]);
|
39
|
+
|
40
|
+
return values;
|
30
41
|
};
|
31
42
|
|
32
|
-
export const useRectBuffer = (
|
33
|
-
size
|
34
|
-
modifier: (input: SkHostRect, index: number) => void
|
35
|
-
) => useBuffer(size, () => Skia.XYWHRect(0, 0, 0, 0), modifier);
|
43
|
+
export const useRectBuffer = (size: number, modifier: Modifier<SkHostRect>) =>
|
44
|
+
useBuffer(size, () => Skia.XYWHRect(0, 0, 0, 0), modifier);
|
36
45
|
|
37
46
|
// Usage for RSXform Buffer
|
38
|
-
export const useRSXformBuffer = (
|
39
|
-
size
|
40
|
-
modifier: (input: SkRSXform, index: number) => void
|
41
|
-
) => useBuffer(size, () => Skia.RSXform(1, 0, 0, 0), modifier);
|
47
|
+
export const useRSXformBuffer = (size: number, modifier: Modifier<SkRSXform>) =>
|
48
|
+
useBuffer(size, () => Skia.RSXform(1, 0, 0, 0), modifier);
|
42
49
|
|
43
50
|
// Usage for Point Buffer
|
44
|
-
export const usePointBuffer = (
|
45
|
-
size
|
46
|
-
modifier: (input: SkPoint, index: number) => void
|
47
|
-
) => useBuffer(size, () => Skia.Point(0, 0), modifier);
|
51
|
+
export const usePointBuffer = (size: number, modifier: Modifier<SkPoint>) =>
|
52
|
+
useBuffer(size, () => Skia.Point(0, 0), modifier);
|
48
53
|
|
49
54
|
// Usage for Color Buffer
|
50
|
-
export const useColorBuffer = (
|
51
|
-
size
|
52
|
-
modifier: (input: SkColor, index: number) => void
|
53
|
-
) => useBuffer(size, () => Skia.Color("black"), modifier);
|
55
|
+
export const useColorBuffer = (size: number, modifier: Modifier<SkColor>) =>
|
56
|
+
useBuffer(size, () => Skia.Color("black"), modifier);
|
@@ -2,15 +2,21 @@ import { useEffect, useMemo } from "react";
|
|
2
2
|
import type { ReactElement } from "react";
|
3
3
|
import type { SharedValue } from "react-native-reanimated";
|
4
4
|
|
5
|
-
import type {
|
5
|
+
import type {
|
6
|
+
DataSourceParam,
|
7
|
+
SkImage,
|
8
|
+
SkPicture,
|
9
|
+
SkSize,
|
10
|
+
} from "../../skia/types";
|
6
11
|
import {
|
7
12
|
drawAsImageFromPicture,
|
8
13
|
drawAsPicture,
|
9
14
|
} from "../../renderer/Offscreen";
|
15
|
+
import { Skia, useImage } from "../../skia";
|
10
16
|
|
11
17
|
import { runOnUI, useSharedValue } from "./moduleWrapper";
|
12
18
|
|
13
|
-
const
|
19
|
+
const createTexture = (
|
14
20
|
texture: SharedValue<SkImage | null>,
|
15
21
|
picture: SkPicture,
|
16
22
|
size: SkSize
|
@@ -19,20 +25,63 @@ const createTextureValue = (
|
|
19
25
|
texture.value = drawAsImageFromPicture(picture, size);
|
20
26
|
};
|
21
27
|
|
22
|
-
export const
|
28
|
+
export const useTexture = (element: ReactElement, size: SkSize) => {
|
23
29
|
const picture = useMemo(() => {
|
24
30
|
return drawAsPicture(element);
|
25
31
|
}, [element]);
|
26
|
-
return
|
32
|
+
return usePictureAsTexture(picture, size);
|
33
|
+
};
|
34
|
+
|
35
|
+
export const useTextureAsValue = (element: ReactElement, size: SkSize) => {
|
36
|
+
console.warn("useTextureAsValue has been renamed to use useTexture");
|
37
|
+
return useTexture(element, size);
|
27
38
|
};
|
28
39
|
|
29
40
|
export const useTextureValueFromPicture = (
|
30
|
-
picture: SkPicture,
|
41
|
+
picture: SkPicture | null,
|
42
|
+
size: SkSize
|
43
|
+
) => {
|
44
|
+
console.warn(
|
45
|
+
"useTextureValueFromPicture has been renamed to use usePictureAsTexture"
|
46
|
+
);
|
47
|
+
return usePictureAsTexture(picture, size);
|
48
|
+
};
|
49
|
+
|
50
|
+
export const usePictureAsTexture = (
|
51
|
+
picture: SkPicture | null,
|
31
52
|
size: SkSize
|
32
53
|
) => {
|
33
54
|
const texture = useSharedValue<SkImage | null>(null);
|
34
55
|
useEffect(() => {
|
35
|
-
|
56
|
+
if (picture !== null) {
|
57
|
+
runOnUI(createTexture)(texture, picture, size);
|
58
|
+
}
|
36
59
|
}, [texture, picture, size]);
|
37
60
|
return texture;
|
38
61
|
};
|
62
|
+
|
63
|
+
export const useImageAsTexture = (source: DataSourceParam) => {
|
64
|
+
const image = useImage(source);
|
65
|
+
const size = useMemo(() => {
|
66
|
+
if (image) {
|
67
|
+
return { width: image.width(), height: image.height() };
|
68
|
+
}
|
69
|
+
return { width: 0, height: 0 };
|
70
|
+
}, [image]);
|
71
|
+
const picture = useMemo(() => {
|
72
|
+
if (image) {
|
73
|
+
const recorder = Skia.PictureRecorder();
|
74
|
+
const canvas = recorder.beginRecording({
|
75
|
+
x: 0,
|
76
|
+
y: 0,
|
77
|
+
width: size.width,
|
78
|
+
height: size.height,
|
79
|
+
});
|
80
|
+
canvas.drawImage(image, 0, 0);
|
81
|
+
return recorder.finishRecordingAsPicture();
|
82
|
+
} else {
|
83
|
+
return null;
|
84
|
+
}
|
85
|
+
}, [size, image]);
|
86
|
+
return usePictureAsTexture(picture, size);
|
87
|
+
};
|