@shopify/react-native-skia 0.1.115 → 0.1.119

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. package/README.md +39 -2
  2. package/cpp/api/JsiSkApi.h +13 -9
  3. package/cpp/api/JsiSkCanvas.h +9 -1
  4. package/cpp/api/JsiSkPicture.h +71 -0
  5. package/cpp/api/JsiSkPictureFactory.h +50 -0
  6. package/cpp/api/JsiSkPictureRecorder.h +53 -0
  7. package/lib/commonjs/renderer/Canvas.js +22 -36
  8. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  9. package/lib/commonjs/renderer/HostConfig.js +3 -25
  10. package/lib/commonjs/renderer/HostConfig.js.map +1 -1
  11. package/lib/commonjs/renderer/components/{Defs.js → Drawing.js} +13 -12
  12. package/lib/commonjs/renderer/components/Drawing.js.map +1 -0
  13. package/lib/commonjs/renderer/components/Group.js +31 -20
  14. package/lib/commonjs/renderer/components/Group.js.map +1 -1
  15. package/lib/commonjs/renderer/components/Paint.js +1 -21
  16. package/lib/commonjs/renderer/components/Paint.js.map +1 -1
  17. package/lib/commonjs/renderer/components/Picture.js +35 -0
  18. package/lib/commonjs/renderer/components/Picture.js.map +1 -0
  19. package/lib/commonjs/renderer/components/imageFilters/InnerShadow.js +5 -34
  20. package/lib/commonjs/renderer/components/imageFilters/InnerShadow.js.map +1 -1
  21. package/lib/commonjs/renderer/components/imageFilters/{DropShadow.js → Shadow.js} +19 -8
  22. package/lib/commonjs/renderer/components/imageFilters/Shadow.js.map +1 -0
  23. package/lib/commonjs/renderer/components/imageFilters/index.js +4 -17
  24. package/lib/commonjs/renderer/components/imageFilters/index.js.map +1 -1
  25. package/lib/commonjs/renderer/components/index.js +17 -4
  26. package/lib/commonjs/renderer/components/index.js.map +1 -1
  27. package/lib/commonjs/renderer/components/maskFilters/Blur.js +1 -1
  28. package/lib/commonjs/renderer/components/maskFilters/Blur.js.map +1 -1
  29. package/lib/commonjs/renderer/components/shapes/Box.js +126 -0
  30. package/lib/commonjs/renderer/components/shapes/Box.js.map +1 -0
  31. package/lib/commonjs/renderer/components/shapes/FitBox.js +9 -5
  32. package/lib/commonjs/renderer/components/shapes/FitBox.js.map +1 -1
  33. package/lib/commonjs/renderer/components/shapes/index.js +13 -0
  34. package/lib/commonjs/renderer/components/shapes/index.js.map +1 -1
  35. package/lib/commonjs/renderer/index.js +13 -0
  36. package/lib/commonjs/renderer/index.js.map +1 -1
  37. package/lib/commonjs/renderer/nodes/Declaration.js +12 -1
  38. package/lib/commonjs/renderer/nodes/Declaration.js.map +1 -1
  39. package/lib/commonjs/renderer/nodes/Drawing.js +9 -24
  40. package/lib/commonjs/renderer/nodes/Drawing.js.map +1 -1
  41. package/lib/commonjs/renderer/nodes/Node.js +9 -16
  42. package/lib/commonjs/renderer/nodes/Node.js.map +1 -1
  43. package/lib/commonjs/renderer/processors/Paint.js +24 -26
  44. package/lib/commonjs/renderer/processors/Paint.js.map +1 -1
  45. package/lib/commonjs/renderer/processors/math/Math.js +19 -1
  46. package/lib/commonjs/renderer/processors/math/Math.js.map +1 -1
  47. package/lib/commonjs/renderer/processors/math/Matrix3.js +11 -5
  48. package/lib/commonjs/renderer/processors/math/Matrix3.js.map +1 -1
  49. package/lib/commonjs/renderer/typeddash.js +26 -2
  50. package/lib/commonjs/renderer/typeddash.js.map +1 -1
  51. package/lib/commonjs/renderer/useContextBridge.js +35 -0
  52. package/lib/commonjs/renderer/useContextBridge.js.map +1 -0
  53. package/lib/commonjs/skia/Canvas.js.map +1 -1
  54. package/lib/commonjs/skia/Picture/Picture.js +6 -0
  55. package/lib/commonjs/skia/Picture/Picture.js.map +1 -0
  56. package/lib/commonjs/skia/Picture/PictureFactory.js +6 -0
  57. package/lib/commonjs/skia/Picture/PictureFactory.js.map +1 -0
  58. package/lib/commonjs/skia/Picture/PictureRecorder.js +6 -0
  59. package/lib/commonjs/skia/Picture/PictureRecorder.js.map +1 -0
  60. package/lib/commonjs/skia/Picture/index.js +58 -0
  61. package/lib/commonjs/skia/Picture/index.js.map +1 -0
  62. package/lib/commonjs/skia/Picture/usePicture.js +30 -0
  63. package/lib/commonjs/skia/Picture/usePicture.js.map +1 -0
  64. package/lib/commonjs/skia/Skia.js +2 -0
  65. package/lib/commonjs/skia/Skia.js.map +1 -1
  66. package/lib/commonjs/skia/index.js +28 -0
  67. package/lib/commonjs/skia/index.js.map +1 -1
  68. package/lib/commonjs/values/hooks/useDerivedValue.js +2 -2
  69. package/lib/commonjs/values/hooks/useDerivedValue.js.map +1 -1
  70. package/lib/commonjs/views/SkiaView.js +6 -44
  71. package/lib/commonjs/views/SkiaView.js.map +1 -1
  72. package/lib/commonjs/views/types.js.map +1 -1
  73. package/lib/module/renderer/Canvas.js +20 -32
  74. package/lib/module/renderer/Canvas.js.map +1 -1
  75. package/lib/module/renderer/HostConfig.js +2 -24
  76. package/lib/module/renderer/HostConfig.js.map +1 -1
  77. package/lib/module/renderer/components/Drawing.js +17 -0
  78. package/lib/module/renderer/components/Drawing.js.map +1 -0
  79. package/lib/module/renderer/components/Group.js +32 -22
  80. package/lib/module/renderer/components/Group.js.map +1 -1
  81. package/lib/module/renderer/components/Paint.js +2 -22
  82. package/lib/module/renderer/components/Paint.js.map +1 -1
  83. package/lib/module/renderer/components/Picture.js +21 -0
  84. package/lib/module/renderer/components/Picture.js.map +1 -0
  85. package/lib/module/renderer/components/imageFilters/InnerShadow.js +4 -26
  86. package/lib/module/renderer/components/imageFilters/InnerShadow.js.map +1 -1
  87. package/lib/module/renderer/components/imageFilters/{DropShadow.js → Shadow.js} +16 -6
  88. package/lib/module/renderer/components/imageFilters/Shadow.js.map +1 -0
  89. package/lib/module/renderer/components/imageFilters/index.js +1 -2
  90. package/lib/module/renderer/components/imageFilters/index.js.map +1 -1
  91. package/lib/module/renderer/components/index.js +2 -1
  92. package/lib/module/renderer/components/index.js.map +1 -1
  93. package/lib/module/renderer/components/maskFilters/Blur.js +1 -1
  94. package/lib/module/renderer/components/maskFilters/Blur.js.map +1 -1
  95. package/lib/module/renderer/components/shapes/Box.js +102 -0
  96. package/lib/module/renderer/components/shapes/Box.js.map +1 -0
  97. package/lib/module/renderer/components/shapes/FitBox.js +5 -4
  98. package/lib/module/renderer/components/shapes/FitBox.js.map +1 -1
  99. package/lib/module/renderer/components/shapes/index.js +1 -0
  100. package/lib/module/renderer/components/shapes/index.js.map +1 -1
  101. package/lib/module/renderer/index.js +1 -0
  102. package/lib/module/renderer/index.js.map +1 -1
  103. package/lib/module/renderer/nodes/Declaration.js +4 -0
  104. package/lib/module/renderer/nodes/Declaration.js.map +1 -1
  105. package/lib/module/renderer/nodes/Drawing.js +8 -21
  106. package/lib/module/renderer/nodes/Drawing.js.map +1 -1
  107. package/lib/module/renderer/nodes/Node.js +9 -15
  108. package/lib/module/renderer/nodes/Node.js.map +1 -1
  109. package/lib/module/renderer/processors/Paint.js +22 -21
  110. package/lib/module/renderer/processors/Paint.js.map +1 -1
  111. package/lib/module/renderer/processors/math/Math.js +15 -0
  112. package/lib/module/renderer/processors/math/Math.js.map +1 -1
  113. package/lib/module/renderer/processors/math/Matrix3.js +6 -2
  114. package/lib/module/renderer/processors/math/Matrix3.js.map +1 -1
  115. package/lib/module/renderer/typeddash.js +21 -0
  116. package/lib/module/renderer/typeddash.js.map +1 -1
  117. package/lib/module/renderer/useContextBridge.js +21 -0
  118. package/lib/module/renderer/useContextBridge.js.map +1 -0
  119. package/lib/module/skia/Canvas.js.map +1 -1
  120. package/lib/module/skia/Picture/Picture.js +2 -0
  121. package/lib/module/skia/Picture/Picture.js.map +1 -0
  122. package/lib/module/skia/Picture/PictureFactory.js +2 -0
  123. package/lib/module/skia/Picture/PictureFactory.js.map +1 -0
  124. package/lib/module/skia/Picture/PictureRecorder.js +2 -0
  125. package/lib/module/skia/Picture/PictureRecorder.js.map +1 -0
  126. package/lib/module/skia/Picture/index.js +5 -0
  127. package/lib/module/skia/Picture/index.js.map +1 -0
  128. package/lib/module/skia/Picture/usePicture.js +19 -0
  129. package/lib/module/skia/Picture/usePicture.js.map +1 -0
  130. package/lib/module/skia/Skia.js +2 -0
  131. package/lib/module/skia/Skia.js.map +1 -1
  132. package/lib/module/skia/index.js +2 -0
  133. package/lib/module/skia/index.js.map +1 -1
  134. package/lib/module/values/hooks/useDerivedValue.js +1 -1
  135. package/lib/module/values/hooks/useDerivedValue.js.map +1 -1
  136. package/lib/module/views/SkiaView.js +6 -44
  137. package/lib/module/views/SkiaView.js.map +1 -1
  138. package/lib/module/views/types.js.map +1 -1
  139. package/lib/typescript/src/renderer/Canvas.d.ts +5 -10
  140. package/lib/typescript/src/renderer/components/Drawing.d.ts +7 -0
  141. package/lib/typescript/src/renderer/components/Picture.d.ts +6 -0
  142. package/lib/typescript/src/renderer/components/imageFilters/InnerShadow.d.ts +3 -12
  143. package/lib/typescript/src/renderer/components/imageFilters/{DropShadow.d.ts → Shadow.d.ts} +3 -2
  144. package/lib/typescript/src/renderer/components/imageFilters/index.d.ts +1 -2
  145. package/lib/typescript/src/renderer/components/index.d.ts +2 -1
  146. package/lib/typescript/src/renderer/components/shapes/Box.d.ts +23 -0
  147. package/lib/typescript/src/renderer/components/shapes/FitBox.d.ts +9 -0
  148. package/lib/typescript/src/renderer/components/shapes/index.d.ts +1 -0
  149. package/lib/typescript/src/renderer/index.d.ts +1 -0
  150. package/lib/typescript/src/renderer/nodes/Declaration.d.ts +3 -0
  151. package/lib/typescript/src/renderer/nodes/Drawing.d.ts +2 -2
  152. package/lib/typescript/src/renderer/nodes/Node.d.ts +2 -2
  153. package/lib/typescript/src/renderer/processors/Paint.d.ts +2 -2
  154. package/lib/typescript/src/renderer/processors/math/Math.d.ts +14 -0
  155. package/lib/typescript/src/renderer/processors/math/Matrix3.d.ts +1 -0
  156. package/lib/typescript/src/renderer/typeddash.d.ts +1 -0
  157. package/lib/typescript/src/renderer/useContextBridge.d.ts +5 -0
  158. package/lib/typescript/src/skia/Canvas.d.ts +6 -0
  159. package/lib/typescript/src/skia/Picture/Picture.d.ts +26 -0
  160. package/lib/typescript/src/skia/Picture/PictureFactory.d.ts +8 -0
  161. package/lib/typescript/src/skia/Picture/PictureRecorder.d.ts +15 -0
  162. package/lib/typescript/src/skia/Picture/index.d.ts +4 -0
  163. package/lib/typescript/src/skia/Picture/usePicture.d.ts +11 -0
  164. package/lib/typescript/src/skia/Skia.d.ts +5 -0
  165. package/lib/typescript/src/skia/index.d.ts +2 -0
  166. package/lib/typescript/src/views/SkiaView.d.ts +28 -13
  167. package/lib/typescript/src/views/types.d.ts +0 -23
  168. package/libs/ios/libskia.xcframework/ios-arm64_arm64e/libskia.a +0 -0
  169. package/libs/ios/libskia.xcframework/ios-arm64_arm64e_x86_64-simulator/libskia.a +0 -0
  170. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e/libskshaper.a +0 -0
  171. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e_x86_64-simulator/libskshaper.a +0 -0
  172. package/libs/ios/libsvg.xcframework/Info.plist +5 -5
  173. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e/libsvg.a +0 -0
  174. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsvg.a +0 -0
  175. package/package.json +7 -7
  176. package/src/renderer/Canvas.tsx +14 -30
  177. package/src/renderer/HostConfig.ts +2 -20
  178. package/src/renderer/components/Drawing.tsx +16 -0
  179. package/src/renderer/components/Group.tsx +44 -27
  180. package/src/renderer/components/Paint.tsx +5 -35
  181. package/src/renderer/components/Picture.tsx +17 -0
  182. package/src/renderer/components/imageFilters/InnerShadow.tsx +33 -47
  183. package/src/renderer/components/imageFilters/Shadow.tsx +39 -0
  184. package/src/renderer/components/imageFilters/index.ts +1 -2
  185. package/src/renderer/components/index.ts +2 -1
  186. package/src/renderer/components/maskFilters/Blur.tsx +1 -1
  187. package/src/renderer/components/shapes/Box.tsx +98 -0
  188. package/src/renderer/components/shapes/FitBox.tsx +6 -4
  189. package/src/renderer/components/shapes/index.ts +1 -0
  190. package/src/renderer/index.ts +1 -0
  191. package/src/renderer/nodes/Declaration.tsx +14 -0
  192. package/src/renderer/nodes/Drawing.tsx +16 -26
  193. package/src/renderer/nodes/Node.ts +9 -12
  194. package/src/renderer/processors/Paint.ts +34 -31
  195. package/src/renderer/processors/math/Math.ts +16 -0
  196. package/src/renderer/processors/math/Matrix3.ts +35 -31
  197. package/src/renderer/typeddash.ts +18 -0
  198. package/src/renderer/useContextBridge.tsx +21 -0
  199. package/src/skia/Canvas.ts +7 -0
  200. package/src/skia/Picture/Picture.ts +34 -0
  201. package/src/skia/Picture/PictureFactory.ts +9 -0
  202. package/src/skia/Picture/PictureRecorder.ts +18 -0
  203. package/src/skia/Picture/index.ts +4 -0
  204. package/src/skia/Picture/usePicture.ts +28 -0
  205. package/src/skia/Skia.ts +5 -0
  206. package/src/skia/index.ts +2 -0
  207. package/src/values/hooks/useDerivedValue.ts +1 -1
  208. package/src/views/SkiaView.tsx +31 -41
  209. package/src/views/types.ts +0 -24
  210. package/lib/commonjs/renderer/components/Defs.js.map +0 -1
  211. package/lib/commonjs/renderer/components/imageFilters/DropShadow.js.map +0 -1
  212. package/lib/module/renderer/components/Defs.js +0 -16
  213. package/lib/module/renderer/components/Defs.js.map +0 -1
  214. package/lib/module/renderer/components/imageFilters/DropShadow.js.map +0 -1
  215. package/lib/typescript/src/renderer/components/Defs.d.ts +0 -5
  216. package/src/renderer/components/Defs.tsx +0 -19
  217. package/src/renderer/components/imageFilters/DropShadow.tsx +0 -31
@@ -102,6 +102,9 @@ const multiply3 = (m1: Matrix3, m2: Matrix3) => {
102
102
  ] as const;
103
103
  };
104
104
 
105
+ export const matrixVecMul3 = (m: Matrix3, v: Vec3) =>
106
+ [dot3(m[0], v), dot3(m[1], v), dot3(m[2], v)] as const;
107
+
105
108
  const skiaMatrix3 = (m: Matrix3): SkMatrix => {
106
109
  return [
107
110
  m[0][0],
@@ -117,34 +120,35 @@ const skiaMatrix3 = (m: Matrix3): SkMatrix => {
117
120
  };
118
121
 
119
122
  export const processTransform2d = (transforms: Transforms2d) =>
120
- skiaMatrix3(
121
- transforms.reduce((acc, transform) => {
122
- const key = Object.keys(transform)[0] as Transform2dName;
123
- const value = (transform as Pick<Transformations, typeof key>)[key];
124
- if (key === "translateX") {
125
- return multiply3(acc, translateXMatrix(value));
126
- }
127
- if (key === "translateY") {
128
- return multiply3(acc, translateYMatrix(value));
129
- }
130
- if (key === "scale") {
131
- return multiply3(acc, scaleMatrix(value));
132
- }
133
- if (key === "scaleX") {
134
- return multiply3(acc, scaleXMatrix(value));
135
- }
136
- if (key === "scaleY") {
137
- return multiply3(acc, scaleYMatrix(value));
138
- }
139
- if (key === "skewX") {
140
- return multiply3(acc, skewXMatrix(value));
141
- }
142
- if (key === "skewY") {
143
- return multiply3(acc, skewYMatrix(value));
144
- }
145
- if (key === "rotate" || key === "rotateZ") {
146
- return multiply3(acc, rotateZMatrix(value));
147
- }
148
- return exhaustiveCheck(key);
149
- }, identityMatrix)
150
- );
123
+ skiaMatrix3(processTransform(transforms));
124
+
125
+ const processTransform = (transforms: Transforms2d) =>
126
+ transforms.reduce((acc, transform) => {
127
+ const key = Object.keys(transform)[0] as Transform2dName;
128
+ const value = (transform as Pick<Transformations, typeof key>)[key];
129
+ if (key === "translateX") {
130
+ return multiply3(acc, translateXMatrix(value));
131
+ }
132
+ if (key === "translateY") {
133
+ return multiply3(acc, translateYMatrix(value));
134
+ }
135
+ if (key === "scale") {
136
+ return multiply3(acc, scaleMatrix(value));
137
+ }
138
+ if (key === "scaleX") {
139
+ return multiply3(acc, scaleXMatrix(value));
140
+ }
141
+ if (key === "scaleY") {
142
+ return multiply3(acc, scaleYMatrix(value));
143
+ }
144
+ if (key === "skewX") {
145
+ return multiply3(acc, skewXMatrix(value));
146
+ }
147
+ if (key === "skewY") {
148
+ return multiply3(acc, skewYMatrix(value));
149
+ }
150
+ if (key === "rotate" || key === "rotateZ") {
151
+ return multiply3(acc, rotateZMatrix(value));
152
+ }
153
+ return exhaustiveCheck(key);
154
+ }, identityMatrix);
@@ -3,3 +3,21 @@ export const mapKeys = <T>(obj: T) => Object.keys(obj) as (keyof T)[];
3
3
  export const exhaustiveCheck = (a: never): never => {
4
4
  throw new Error(`Unexhaustive handling for ${a}`);
5
5
  };
6
+
7
+ // Shallow eq on props (without children)
8
+ export const shallowEq = <P>(p1: P, p2: P): boolean => {
9
+ const keys1 = mapKeys(p1);
10
+ const keys2 = mapKeys(p2);
11
+ if (keys1.length !== keys2.length) {
12
+ return false;
13
+ }
14
+ for (const key of keys1) {
15
+ if (key === "children") {
16
+ continue;
17
+ }
18
+ if (p1[key] !== p2[key]) {
19
+ return false;
20
+ }
21
+ }
22
+ return true;
23
+ };
@@ -0,0 +1,21 @@
1
+ import React, { useMemo, useContext } from "react";
2
+ import type { ReactNode, Context, ReactElement } from "react";
3
+
4
+ // useContextBridge() is taken from https://github.com/pmndrs/drei#usecontextbridge
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ export const useContextBridge = (...contexts: Context<any>[]) => {
7
+ const values =
8
+ // eslint-disable-next-line react-hooks/rules-of-hooks
9
+ contexts.map((context) => useContext(context));
10
+ return useMemo(
11
+ () =>
12
+ ({ children }: { children: ReactNode }) =>
13
+ contexts.reduceRight(
14
+ (acc, Context, i) => (
15
+ <Context.Provider value={values[i]} children={acc} />
16
+ ),
17
+ children
18
+ ) as ReactElement,
19
+ [contexts, values]
20
+ );
21
+ };
@@ -12,6 +12,7 @@ import type { SkMatrix } from "./Matrix";
12
12
  import type { SkImageFilter } from "./ImageFilter";
13
13
  import type { SkVertices } from "./Vertices";
14
14
  import type { SkTextBlob } from "./TextBlob";
15
+ import type { SkPicture } from "./Picture";
15
16
 
16
17
  export enum ClipOp {
17
18
  Difference,
@@ -485,4 +486,10 @@ export interface SkCanvas {
485
486
  * @param m
486
487
  */
487
488
  concat(m: SkMatrix): void;
489
+
490
+ /**
491
+ * Draws the given picture using the current clip, current matrix, and the provided paint.
492
+ * @param skp
493
+ */
494
+ drawPicture(skp: SkPicture): void;
488
495
  }
@@ -0,0 +1,34 @@
1
+ import type { InputColorMatrix } from "../ColorFilter";
2
+ import type { FilterMode } from "../Image";
3
+ import type { TileMode } from "../ImageFilter";
4
+ import type { SkRect } from "../Rect";
5
+ import type { IShader } from "../Shader";
6
+
7
+ export interface SkPicture {
8
+ /**
9
+ * Returns a new shader that will draw with this picture.
10
+ *
11
+ * @param tmx The tiling mode to use when sampling in the x-direction.
12
+ * @param tmy The tiling mode to use when sampling in the y-direction.
13
+ * @param mode How to filter the tiles
14
+ * @param localMatrix Optional matrix used when sampling
15
+ * @param tileRect The tile rectangle in picture coordinates: this represents the subset
16
+ * (or superset) of the picture used when building a tile. It is not
17
+ * affected by localMatrix and does not imply scaling (only translation
18
+ * and cropping). If null, the tile rect is considered equal to the picture
19
+ * bounds.
20
+ */
21
+ makeShader(
22
+ tmx: TileMode,
23
+ tmy: TileMode,
24
+ mode: FilterMode,
25
+ localMatrix?: InputColorMatrix,
26
+ tileRect?: SkRect
27
+ ): IShader;
28
+
29
+ /**
30
+ * Returns the serialized format of this SkPicture. The format may change at anytime and
31
+ * no promises are made for backwards or forward compatibility.
32
+ */
33
+ serialize(): Uint8Array | null;
34
+ }
@@ -0,0 +1,9 @@
1
+ import type { SkPicture } from "./Picture";
2
+
3
+ export interface PictureFactory {
4
+ /**
5
+ * Returns an SkPicture which has been serialized previously to the given bytes.
6
+ * @param bytes
7
+ */
8
+ MakePicture(bytes: Uint8Array | ArrayBuffer): SkPicture | null;
9
+ }
@@ -0,0 +1,18 @@
1
+ import type { SkCanvas } from "../Canvas";
2
+ import type { SkRect } from "../Rect";
3
+
4
+ import type { SkPicture } from "./Picture";
5
+
6
+ export interface SkPictureRecorder {
7
+ /**
8
+ * Returns a canvas on which to draw. When done drawing, call finishRecordingAsPicture()
9
+ *
10
+ * @param bounds - a rect to cull the results.
11
+ */
12
+ beginRecording(bounds: SkRect): SkCanvas;
13
+
14
+ /**
15
+ * Returns the captured draw commands as a picture and invalidates the canvas returned earlier.
16
+ */
17
+ finishRecordingAsPicture(): SkPicture;
18
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./Picture";
2
+ export * from "./PictureRecorder";
3
+ export * from "./usePicture";
4
+ export * from "./PictureFactory";
@@ -0,0 +1,28 @@
1
+ import type { DependencyList } from "react";
2
+ import { useMemo } from "react";
3
+
4
+ import type { SkCanvas } from "../Canvas";
5
+ import type { SkRect } from "../Rect";
6
+ import { Skia } from "../Skia";
7
+
8
+ import type { SkPicture } from "./Picture";
9
+
10
+ /**
11
+ * Memoizes and returns an SkPicture that can be drawn to another canvas.
12
+ * @param rect Picture bounds
13
+ * @param cb Callback for drawing to the canvas
14
+ * @returns SkPicture
15
+ */
16
+ export const usePicture = (
17
+ rect: SkRect,
18
+ cb: (canvas: SkCanvas) => void,
19
+ deps: DependencyList = []
20
+ ): SkPicture => {
21
+ return useMemo(() => {
22
+ const recorder = Skia.PictureRecorder();
23
+ const canvas = recorder.beginRecording(rect);
24
+ cb(canvas);
25
+ return recorder.finishRecordingAsPicture();
26
+ // eslint-disable-next-line react-hooks/exhaustive-deps
27
+ }, [cb, rect, deps]);
28
+ };
package/src/skia/Skia.ts CHANGED
@@ -26,6 +26,7 @@ import "./NativeSetup";
26
26
  import type { SkRSXform } from "./RSXform";
27
27
  import type { SkPath } from "./Path/Path";
28
28
  import type { SkContourMeasureIter } from "./ContourMeasure";
29
+ import type { PictureFactory, SkPictureRecorder } from "./Picture";
29
30
 
30
31
  /**
31
32
  * Declares the interface for the native Skia API
@@ -41,6 +42,8 @@ export interface Skia {
41
42
  resScale: number
42
43
  ) => SkContourMeasureIter;
43
44
  Paint: () => SkPaint;
45
+ PictureRecorder: () => SkPictureRecorder;
46
+ Picture: PictureFactory;
44
47
  Path: PathFactory;
45
48
  Matrix: () => SkMatrix;
46
49
  ColorFilter: ColorFilterFactory;
@@ -106,6 +109,8 @@ export const Skia = {
106
109
  XYWHRect: SkiaApi.XYWHRect,
107
110
  RRectXY: SkiaApi.RRectXY,
108
111
  Paint: SkiaApi.Paint,
112
+ PictureRecorder: SkiaApi.PictureRecorder,
113
+ Picture: SkiaApi.Picture,
109
114
  Path: SkiaApi.Path,
110
115
  ColorFilter: SkiaApi.ColorFilter,
111
116
  ContourMeasureIter: SkiaApi.ContourMeasureIter,
package/src/skia/index.ts CHANGED
@@ -20,3 +20,5 @@ export * from "./Surface";
20
20
  export * from "./FontMgr";
21
21
  export * from "./Vertices";
22
22
  export * from "./Color";
23
+ export * from "./Picture";
24
+ export * from "./Data";
@@ -1,7 +1,7 @@
1
1
  import { useMemo } from "react";
2
2
 
3
3
  import { ValueApi } from "../api";
4
- import { isValue } from "../../renderer";
4
+ import { isValue } from "../../renderer/processors";
5
5
 
6
6
  /**
7
7
  * Creates a new derived value - a value that will calculate its value depending
@@ -1,4 +1,5 @@
1
1
  import React from "react";
2
+ import type { ViewProps } from "react-native";
2
3
  import { requireNativeComponent } from "react-native";
3
4
 
4
5
  import type { SkImage, SkRect } from "../skia";
@@ -6,9 +7,8 @@ import type { SkiaReadonlyValue } from "../values";
6
7
 
7
8
  import type {
8
9
  DrawMode,
9
- NativeSkiaViewProps,
10
10
  RNSkiaDrawCallback,
11
- RNSkiaViewProps,
11
+ NativeSkiaViewProps,
12
12
  } from "./types";
13
13
 
14
14
  let SkiaViewNativeId = 1000;
@@ -35,8 +35,32 @@ declare global {
35
35
 
36
36
  const { SkiaViewApi } = global;
37
37
 
38
- export class SkiaView extends React.Component<RNSkiaViewProps> {
39
- constructor(props: RNSkiaViewProps) {
38
+ export interface SkiaViewProps extends ViewProps {
39
+ /**
40
+ * Sets the drawing mode for the skia view. There are two drawing
41
+ * modes, "continuous" and "default", where the continuous mode will
42
+ * continuously redraw the view, and the default mode will only
43
+ * redraw when any of the regular react properties are changed like
44
+ * sizes and margins.
45
+ */
46
+ mode?: DrawMode;
47
+ /**
48
+ * When set to true the view will display information about the
49
+ * average time it takes to render.
50
+ */
51
+ debug?: boolean;
52
+ /**
53
+ * Draw callback. Will be called whenever the view is invalidated and
54
+ * needs to redraw. This is either caused by a change in a react
55
+ * property, a touch event, or a call to redraw. If the view is in
56
+ * continuous mode the callback will be called 60 frames per second
57
+ * by the native view.
58
+ */
59
+ onDraw?: RNSkiaDrawCallback;
60
+ }
61
+
62
+ export class SkiaView extends React.Component<SkiaViewProps> {
63
+ constructor(props: SkiaViewProps) {
40
64
  super(props);
41
65
  this._nativeId = SkiaViewNativeId++;
42
66
  const { onDraw } = props;
@@ -47,13 +71,12 @@ export class SkiaView extends React.Component<RNSkiaViewProps> {
47
71
  }
48
72
 
49
73
  private _nativeId: number;
50
- private _animatingValues: Array<unknown> = [];
51
74
 
52
75
  public get nativeId() {
53
76
  return this._nativeId;
54
77
  }
55
78
 
56
- componentDidUpdate(prevProps: RNSkiaViewProps) {
79
+ componentDidUpdate(prevProps: SkiaViewProps) {
57
80
  const { onDraw } = this.props;
58
81
  if (onDraw !== prevProps.onDraw) {
59
82
  assertDrawCallbacksEnabled();
@@ -103,48 +126,15 @@ export class SkiaView extends React.Component<RNSkiaViewProps> {
103
126
  return SkiaViewApi.registerValuesInView(this._nativeId, values);
104
127
  }
105
128
 
106
- /**
107
- * Increases the number of animations active in the view.
108
- */
109
- public addAnimation(owner: unknown) {
110
- if (this._animatingValues.findIndex((p) => p === owner) === -1) {
111
- this._animatingValues.push(owner);
112
- }
113
-
114
- if (this._animatingValues.length === 1) {
115
- if (this.props.mode === "default" || this.props.mode === undefined) {
116
- //console.log("SkiaView addAnimation - mode changed to continuous");
117
- this.setDrawMode("continuous");
118
- }
119
- }
120
- }
121
-
122
- /**
123
- * Decreases the number of animations active in the view.
124
- */
125
- public removeAnimation(owner: unknown) {
126
- const indexOfOwner = this._animatingValues.indexOf(owner);
127
- if (indexOfOwner !== -1) {
128
- // Remove
129
- this._animatingValues = this._animatingValues.filter((p) => p !== owner);
130
- }
131
- if (this._animatingValues.length === 0) {
132
- if (this.props.mode === "default" || this.props.mode === undefined) {
133
- //console.log("SkiaView removeAnimation - mode changed to default");
134
- this.setDrawMode("default");
135
- }
136
- }
137
- }
138
-
139
129
  render() {
140
- const { style, mode, debug = false } = this.props;
130
+ const { mode, debug = false, ...viewProps } = this.props;
141
131
  return (
142
132
  <NativeSkiaView
143
- style={style}
144
133
  collapsable={false}
145
134
  nativeID={`${this._nativeId}`}
146
135
  mode={mode}
147
136
  debug={debug}
137
+ {...viewProps}
148
138
  />
149
139
  );
150
140
  }
@@ -47,30 +47,6 @@ export type TouchHandler = (touchInfo: Array<Array<TouchInfo>>) => void;
47
47
 
48
48
  export type RNSkiaDrawCallback = (canvas: SkCanvas, info: DrawingInfo) => void;
49
49
 
50
- export type RNSkiaViewProps = ViewProps & {
51
- /**
52
- * Sets the drawing mode for the skia view. There are two drawing
53
- * modes, "continuous" and "default", where the continuous mode will
54
- * continuously redraw the view, and the default mode will only
55
- * redraw when any of the regular react properties are changed like
56
- * sizes and margins.
57
- */
58
- mode?: DrawMode;
59
- /**
60
- * When set to true the view will display information about the
61
- * average time it takes to render.
62
- */
63
- debug?: boolean;
64
- /**
65
- * Draw callback. Will be called whenever the view is invalidated and
66
- * needs to redraw. This is either caused by a change in a react
67
- * property, a touch event, or a call to redraw. If the view is in
68
- * continuous mode the callback will be called 60 frames per second
69
- * by the native view.
70
- */
71
- onDraw?: RNSkiaDrawCallback;
72
- };
73
-
74
50
  /**
75
51
  * Listener interface for value changes
76
52
  */
@@ -1 +0,0 @@
1
- {"version":3,"sources":["Defs.tsx"],"names":["onDeclare","Defs","props"],"mappings":";;;;;;;AAAA;;AAGA;;;;;;AAMA,MAAMA,SAAS,GAAG,8BAAkB,MAAM;AACxC;AACA;AACA;AACA,SAAO,IAAP;AACD,CALiB,CAAlB;;AAOO,MAAMC,IAAI,GAAIC,KAAD,IAAsB;AACxC,sBAAO;AAAe,IAAA,SAAS,EAAEF;AAA1B,KAAyCE,KAAzC,EAAP;AACD,CAFM","sourcesContent":["import React from \"react\";\nimport type { ReactNode } from \"react\";\n\nimport { createDeclaration } from \"../nodes\";\n\nexport interface DefsProps {\n children: ReactNode | ReactNode[];\n}\n\nconst onDeclare = createDeclaration(() => {\n // Do nothing.\n // The goal of this component is to avoid declared paint from\n // being used in the drawing context automatically\n return null;\n});\n\nexport const Defs = (props: DefsProps) => {\n return <skDeclaration onDeclare={onDeclare} {...props} />;\n};\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["DropShadow.tsx"],"names":["onDeclare","children","dx","dy","blur","color","shadowOnly","opacity","input","factory","Skia","ImageFilter","MakeDropShadowOnly","MakeDropShadow","DropShadow","props"],"mappings":";;;;;;;AAAA;;AAEA;;AACA;;AAGA;;AAEA;;;;;;AAUA,MAAMA,SAAS,GAAG,oCAChB,OAAsCC,QAAtC,YAAgE;AAAA,MAA/D;AAAEC,IAAAA,EAAF;AAAMC,IAAAA,EAAN;AAAUC,IAAAA,IAAV;AAAgBC,IAAAA,KAAhB;AAAuBC,IAAAA;AAAvB,GAA+D;AAAA,MAAhB;AAAEC,IAAAA;AAAF,GAAgB;AAC9D,QAAMC,KAAK,GAAG,wBAASP,QAAT,CAAd;AACA,QAAMQ,OAAO,GAAGH,UAAU,GACtBI,WAAKC,WAAL,CAAiBC,kBADK,GAEtBF,WAAKC,WAAL,CAAiBE,cAFrB;AAGA,SAAOJ,OAAO,CAACP,EAAD,EAAKC,EAAL,EAASC,IAAT,EAAeA,IAAf,EAAqB,yBAAaC,KAAb,EAAoBE,OAApB,CAArB,EAAmDC,KAAnD,CAAd;AACD,CAPe,CAAlB;;AAUO,MAAMM,UAAU,GAAIC,KAAD,IAA2C;AACnE,sBAAO;AAAe,IAAA,SAAS,EAAEf;AAA1B,KAAyCe,KAAzC,EAAP;AACD,CAFM","sourcesContent":["import React from \"react\";\n\nimport { Skia } from \"../../../skia\";\nimport { createDeclaration } from \"../../nodes/Declaration\";\nimport type { AnimatedProps } from \"../../processors/Animations/Animations\";\nimport type { Color } from \"../../../skia/Color\";\nimport { processColor } from \"../../../skia/Color\";\n\nimport { getInput } from \"./getInput\";\n\nexport interface DropShadowProps {\n dx: number;\n dy: number;\n blur: number;\n color: Color;\n shadowOnly?: boolean;\n}\n\nconst onDeclare = createDeclaration<DropShadowProps>(\n ({ dx, dy, blur, color, shadowOnly }, children, { opacity }) => {\n const input = getInput(children);\n const factory = shadowOnly\n ? Skia.ImageFilter.MakeDropShadowOnly\n : Skia.ImageFilter.MakeDropShadow;\n return factory(dx, dy, blur, blur, processColor(color, opacity), input);\n }\n);\n\nexport const DropShadow = (props: AnimatedProps<DropShadowProps>) => {\n return <skDeclaration onDeclare={onDeclare} {...props} />;\n};\n"]}
@@ -1,16 +0,0 @@
1
- function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
-
3
- import React from "react";
4
- import { createDeclaration } from "../nodes";
5
- const onDeclare = createDeclaration(() => {
6
- // Do nothing.
7
- // The goal of this component is to avoid declared paint from
8
- // being used in the drawing context automatically
9
- return null;
10
- });
11
- export const Defs = props => {
12
- return /*#__PURE__*/React.createElement("skDeclaration", _extends({
13
- onDeclare: onDeclare
14
- }, props));
15
- };
16
- //# sourceMappingURL=Defs.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["Defs.tsx"],"names":["React","createDeclaration","onDeclare","Defs","props"],"mappings":";;AAAA,OAAOA,KAAP,MAAkB,OAAlB;AAGA,SAASC,iBAAT,QAAkC,UAAlC;AAMA,MAAMC,SAAS,GAAGD,iBAAiB,CAAC,MAAM;AACxC;AACA;AACA;AACA,SAAO,IAAP;AACD,CALkC,CAAnC;AAOA,OAAO,MAAME,IAAI,GAAIC,KAAD,IAAsB;AACxC,sBAAO;AAAe,IAAA,SAAS,EAAEF;AAA1B,KAAyCE,KAAzC,EAAP;AACD,CAFM","sourcesContent":["import React from \"react\";\nimport type { ReactNode } from \"react\";\n\nimport { createDeclaration } from \"../nodes\";\n\nexport interface DefsProps {\n children: ReactNode | ReactNode[];\n}\n\nconst onDeclare = createDeclaration(() => {\n // Do nothing.\n // The goal of this component is to avoid declared paint from\n // being used in the drawing context automatically\n return null;\n});\n\nexport const Defs = (props: DefsProps) => {\n return <skDeclaration onDeclare={onDeclare} {...props} />;\n};\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["DropShadow.tsx"],"names":["React","Skia","createDeclaration","processColor","getInput","onDeclare","children","dx","dy","blur","color","shadowOnly","opacity","input","factory","ImageFilter","MakeDropShadowOnly","MakeDropShadow","DropShadow","props"],"mappings":";;AAAA,OAAOA,KAAP,MAAkB,OAAlB;AAEA,SAASC,IAAT,QAAqB,eAArB;AACA,SAASC,iBAAT,QAAkC,yBAAlC;AAGA,SAASC,YAAT,QAA6B,qBAA7B;AAEA,SAASC,QAAT,QAAyB,YAAzB;AAUA,MAAMC,SAAS,GAAGH,iBAAiB,CACjC,OAAsCI,QAAtC,YAAgE;AAAA,MAA/D;AAAEC,IAAAA,EAAF;AAAMC,IAAAA,EAAN;AAAUC,IAAAA,IAAV;AAAgBC,IAAAA,KAAhB;AAAuBC,IAAAA;AAAvB,GAA+D;AAAA,MAAhB;AAAEC,IAAAA;AAAF,GAAgB;AAC9D,QAAMC,KAAK,GAAGT,QAAQ,CAACE,QAAD,CAAtB;AACA,QAAMQ,OAAO,GAAGH,UAAU,GACtBV,IAAI,CAACc,WAAL,CAAiBC,kBADK,GAEtBf,IAAI,CAACc,WAAL,CAAiBE,cAFrB;AAGA,SAAOH,OAAO,CAACP,EAAD,EAAKC,EAAL,EAASC,IAAT,EAAeA,IAAf,EAAqBN,YAAY,CAACO,KAAD,EAAQE,OAAR,CAAjC,EAAmDC,KAAnD,CAAd;AACD,CAPgC,CAAnC;AAUA,OAAO,MAAMK,UAAU,GAAIC,KAAD,IAA2C;AACnE,sBAAO;AAAe,IAAA,SAAS,EAAEd;AAA1B,KAAyCc,KAAzC,EAAP;AACD,CAFM","sourcesContent":["import React from \"react\";\n\nimport { Skia } from \"../../../skia\";\nimport { createDeclaration } from \"../../nodes/Declaration\";\nimport type { AnimatedProps } from \"../../processors/Animations/Animations\";\nimport type { Color } from \"../../../skia/Color\";\nimport { processColor } from \"../../../skia/Color\";\n\nimport { getInput } from \"./getInput\";\n\nexport interface DropShadowProps {\n dx: number;\n dy: number;\n blur: number;\n color: Color;\n shadowOnly?: boolean;\n}\n\nconst onDeclare = createDeclaration<DropShadowProps>(\n ({ dx, dy, blur, color, shadowOnly }, children, { opacity }) => {\n const input = getInput(children);\n const factory = shadowOnly\n ? Skia.ImageFilter.MakeDropShadowOnly\n : Skia.ImageFilter.MakeDropShadow;\n return factory(dx, dy, blur, blur, processColor(color, opacity), input);\n }\n);\n\nexport const DropShadow = (props: AnimatedProps<DropShadowProps>) => {\n return <skDeclaration onDeclare={onDeclare} {...props} />;\n};\n"]}
@@ -1,5 +0,0 @@
1
- import type { ReactNode } from "react";
2
- export interface DefsProps {
3
- children: ReactNode | ReactNode[];
4
- }
5
- export declare const Defs: (props: DefsProps) => JSX.Element;
@@ -1,19 +0,0 @@
1
- import React from "react";
2
- import type { ReactNode } from "react";
3
-
4
- import { createDeclaration } from "../nodes";
5
-
6
- export interface DefsProps {
7
- children: ReactNode | ReactNode[];
8
- }
9
-
10
- const onDeclare = createDeclaration(() => {
11
- // Do nothing.
12
- // The goal of this component is to avoid declared paint from
13
- // being used in the drawing context automatically
14
- return null;
15
- });
16
-
17
- export const Defs = (props: DefsProps) => {
18
- return <skDeclaration onDeclare={onDeclare} {...props} />;
19
- };
@@ -1,31 +0,0 @@
1
- import React from "react";
2
-
3
- import { Skia } from "../../../skia";
4
- import { createDeclaration } from "../../nodes/Declaration";
5
- import type { AnimatedProps } from "../../processors/Animations/Animations";
6
- import type { Color } from "../../../skia/Color";
7
- import { processColor } from "../../../skia/Color";
8
-
9
- import { getInput } from "./getInput";
10
-
11
- export interface DropShadowProps {
12
- dx: number;
13
- dy: number;
14
- blur: number;
15
- color: Color;
16
- shadowOnly?: boolean;
17
- }
18
-
19
- const onDeclare = createDeclaration<DropShadowProps>(
20
- ({ dx, dy, blur, color, shadowOnly }, children, { opacity }) => {
21
- const input = getInput(children);
22
- const factory = shadowOnly
23
- ? Skia.ImageFilter.MakeDropShadowOnly
24
- : Skia.ImageFilter.MakeDropShadow;
25
- return factory(dx, dy, blur, blur, processColor(color, opacity), input);
26
- }
27
- );
28
-
29
- export const DropShadow = (props: AnimatedProps<DropShadowProps>) => {
30
- return <skDeclaration onDeclare={onDeclare} {...props} />;
31
- };