@shopify/react-native-skia 0.1.134 → 0.1.138

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. package/README.md +4 -47
  2. package/android/build.gradle +1 -2
  3. package/cpp/api/JsiSkFont.h +25 -1
  4. package/cpp/api/JsiSkMatrix.h +12 -0
  5. package/cpp/api/JsiSkTypeface.h +1 -29
  6. package/jestSetup.js +5 -0
  7. package/lib/commonjs/animation/functions/interpolate.js +3 -2
  8. package/lib/commonjs/animation/functions/interpolate.js.map +1 -1
  9. package/lib/commonjs/animation/functions/interpolateColors.js +2 -2
  10. package/lib/commonjs/animation/functions/interpolateColors.js.map +1 -1
  11. package/lib/commonjs/animation/functions/interpolatePaths.js +45 -11
  12. package/lib/commonjs/animation/functions/interpolatePaths.js.map +1 -1
  13. package/lib/commonjs/mock/index.js +121 -177
  14. package/lib/commonjs/mock/index.js.map +1 -1
  15. package/lib/commonjs/renderer/components/Group.js +1 -1
  16. package/lib/commonjs/renderer/components/Group.js.map +1 -1
  17. package/lib/commonjs/renderer/processors/Transform.js +8 -15
  18. package/lib/commonjs/renderer/processors/Transform.js.map +1 -1
  19. package/lib/commonjs/skia/core/Data.js +7 -4
  20. package/lib/commonjs/skia/core/Data.js.map +1 -1
  21. package/lib/commonjs/skia/types/Font/Font.js.map +1 -1
  22. package/lib/commonjs/skia/types/Matrix.js +18 -3
  23. package/lib/commonjs/skia/types/Matrix.js.map +1 -1
  24. package/lib/commonjs/skia/web/JsiSkFont.js +6 -0
  25. package/lib/commonjs/skia/web/JsiSkFont.js.map +1 -1
  26. package/lib/commonjs/skia/web/JsiSkMatrix.js +4 -0
  27. package/lib/commonjs/skia/web/JsiSkMatrix.js.map +1 -1
  28. package/lib/commonjs/values/api.js +1 -7
  29. package/lib/commonjs/values/api.js.map +1 -1
  30. package/lib/commonjs/values/hooks/useComputedValue.js +2 -2
  31. package/lib/commonjs/values/hooks/useComputedValue.js.map +1 -1
  32. package/lib/commonjs/views/SkiaView.web.js +27 -37
  33. package/lib/commonjs/views/SkiaView.web.js.map +1 -1
  34. package/lib/commonjs/web/LoadSkiaWeb.js +29 -0
  35. package/lib/commonjs/web/LoadSkiaWeb.js.map +1 -0
  36. package/lib/commonjs/web/WithSkiaWeb.js +39 -0
  37. package/lib/commonjs/web/WithSkiaWeb.js.map +1 -0
  38. package/lib/commonjs/web/index.js +22 -12
  39. package/lib/commonjs/web/index.js.map +1 -1
  40. package/lib/module/animation/functions/interpolate.js +2 -2
  41. package/lib/module/animation/functions/interpolate.js.map +1 -1
  42. package/lib/module/animation/functions/interpolateColors.js +1 -1
  43. package/lib/module/animation/functions/interpolateColors.js.map +1 -1
  44. package/lib/module/animation/functions/interpolatePaths.js +44 -11
  45. package/lib/module/animation/functions/interpolatePaths.js.map +1 -1
  46. package/lib/module/mock/index.js +103 -147
  47. package/lib/module/mock/index.js.map +1 -1
  48. package/lib/module/renderer/components/Group.js +1 -1
  49. package/lib/module/renderer/components/Group.js.map +1 -1
  50. package/lib/module/renderer/processors/Transform.js +8 -15
  51. package/lib/module/renderer/processors/Transform.js.map +1 -1
  52. package/lib/module/skia/core/Data.js +7 -4
  53. package/lib/module/skia/core/Data.js.map +1 -1
  54. package/lib/module/skia/types/Font/Font.js.map +1 -1
  55. package/lib/module/skia/types/Matrix.js +12 -2
  56. package/lib/module/skia/types/Matrix.js.map +1 -1
  57. package/lib/module/skia/web/JsiSkFont.js +6 -0
  58. package/lib/module/skia/web/JsiSkFont.js.map +1 -1
  59. package/lib/module/skia/web/JsiSkMatrix.js +4 -0
  60. package/lib/module/skia/web/JsiSkMatrix.js.map +1 -1
  61. package/lib/module/values/api.js +0 -4
  62. package/lib/module/values/api.js.map +1 -1
  63. package/lib/module/values/hooks/useComputedValue.js +1 -1
  64. package/lib/module/values/hooks/useComputedValue.js.map +1 -1
  65. package/lib/module/views/SkiaView.web.js +27 -37
  66. package/lib/module/views/SkiaView.web.js.map +1 -1
  67. package/lib/module/web/LoadSkiaWeb.js +16 -0
  68. package/lib/module/web/LoadSkiaWeb.js.map +1 -0
  69. package/lib/module/web/WithSkiaWeb.js +23 -0
  70. package/lib/module/web/WithSkiaWeb.js.map +1 -0
  71. package/lib/module/web/index.js +2 -9
  72. package/lib/module/web/index.js.map +1 -1
  73. package/lib/typescript/jestSetup.d.ts +1 -0
  74. package/lib/typescript/src/animation/functions/interpolate.d.ts +6 -0
  75. package/lib/typescript/src/animation/functions/interpolatePaths.d.ts +3 -1
  76. package/lib/typescript/src/mock/index.d.ts +11 -42
  77. package/lib/typescript/src/renderer/Canvas.d.ts +1 -1
  78. package/lib/typescript/src/renderer/processors/Transform.d.ts +2 -2
  79. package/lib/typescript/src/skia/types/Font/Font.d.ts +6 -0
  80. package/lib/typescript/src/skia/types/Matrix.d.ts +6 -2
  81. package/lib/typescript/src/skia/types/Typeface/Typeface.d.ts +1 -4
  82. package/lib/typescript/src/skia/web/JsiSkFont.d.ts +1 -0
  83. package/lib/typescript/src/skia/web/JsiSkMatrix.d.ts +1 -0
  84. package/lib/typescript/src/values/api.d.ts +0 -1
  85. package/lib/typescript/src/views/SkiaView.web.d.ts +4 -6
  86. package/lib/typescript/src/web/LoadSkiaWeb.d.ts +6 -0
  87. package/lib/typescript/src/web/WithSkiaWeb.d.ts +12 -0
  88. package/lib/typescript/src/web/index.d.ts +2 -5
  89. package/package.json +7 -3
  90. package/scripts/setup-canvaskit.js +74 -0
  91. package/src/animation/functions/interpolate.ts +4 -2
  92. package/src/animation/functions/interpolateColors.ts +1 -1
  93. package/src/animation/functions/interpolatePaths.ts +59 -10
  94. package/src/mock/index.ts +103 -149
  95. package/src/renderer/components/Group.tsx +1 -1
  96. package/src/renderer/processors/Transform.ts +7 -10
  97. package/src/skia/core/Data.ts +7 -8
  98. package/src/skia/types/Font/Font.ts +7 -0
  99. package/src/skia/types/Matrix.ts +19 -3
  100. package/src/skia/types/Typeface/Typeface.ts +1 -4
  101. package/src/skia/web/JsiSkFont.ts +6 -0
  102. package/src/skia/web/JsiSkMatrix.ts +4 -0
  103. package/src/values/api.ts +0 -2
  104. package/src/values/hooks/useComputedValue.ts +1 -1
  105. package/src/views/SkiaView.web.tsx +36 -43
  106. package/src/web/LoadSkiaWeb.tsx +24 -0
  107. package/src/web/WithSkiaWeb.tsx +37 -0
  108. package/src/web/index.ts +2 -15
@@ -10,6 +10,12 @@ export interface FontMetrics {
10
10
  bounds?: SkRect;
11
11
  }
12
12
  export interface SkFont extends SkJSIInstance<"Font"> {
13
+ /**
14
+ * Retrieves the total width of the provided text
15
+ * @param text
16
+ * @param paint
17
+ */
18
+ getTextWidth(text: string, paint?: SkPaint): number;
13
19
  /**
14
20
  * Retrieves the advanceX measurements for each glyph.
15
21
  * If paint is not null, its stroking, PathEffect, and MaskFilter fields are respected.
@@ -1,4 +1,5 @@
1
1
  import type { SkJSIInstance } from "./JsiInstance";
2
+ import type { SkCanvas } from "./Canvas";
2
3
  export declare enum MatrixIndex {
3
4
  ScaleX = 0,
4
5
  SkewX = 1,
@@ -8,14 +9,16 @@ export declare enum MatrixIndex {
8
9
  TransY = 5,
9
10
  Persp0 = 6,
10
11
  Persp1 = 7,
11
- persp2 = 8
12
+ Persp2 = 8
12
13
  }
14
+ export declare const isMatrix: (obj: unknown) => obj is SkMatrix;
13
15
  export interface SkMatrix extends SkJSIInstance<"Matrix"> {
14
16
  concat: (matrix: SkMatrix) => void;
15
17
  translate: (x: number, y: number) => void;
16
18
  scale: (x: number, y?: number) => void;
17
19
  skew: (x: number, y: number) => void;
18
20
  rotate: (theta: number) => void;
21
+ identity: () => void;
19
22
  }
20
23
  declare type Transform2dName = "translateX" | "translateY" | "scale" | "skewX" | "skewY" | "scaleX" | "scaleY" | "rotateZ" | "rotate";
21
24
  declare type Transformations = {
@@ -25,5 +28,6 @@ export declare type Transforms2d = readonly (Pick<Transformations, "translateX">
25
28
  export interface TransformProp {
26
29
  transform?: Transforms2d;
27
30
  }
28
- export declare const processTransform: (m: SkMatrix, transforms: Transforms2d) => SkMatrix;
31
+ export declare const processTransform: <T extends SkMatrix | SkCanvas>(m: T, transforms: Transforms2d) => T;
32
+ export declare const toDegrees: (rad: number) => number;
29
33
  export {};
@@ -1,5 +1,2 @@
1
1
  import type { SkJSIInstance } from "../JsiInstance";
2
- export interface SkTypeface extends SkJSIInstance<"Typeface"> {
3
- readonly bold: boolean;
4
- readonly italic: boolean;
5
- }
2
+ export declare type SkTypeface = SkJSIInstance<"Typeface">;
@@ -6,6 +6,7 @@ import { JsiSkTypeface } from "./JsiSkTypeface";
6
6
  export declare class JsiSkFont extends HostObject<Font, "Font"> implements SkFont {
7
7
  constructor(CanvasKit: CanvasKit, ref: Font);
8
8
  measureText(_text: string, _paint?: SkPaint): SkRect;
9
+ getTextWidth(text: string, paint?: SkPaint | undefined): number;
9
10
  getMetrics(): {
10
11
  ascent: number;
11
12
  descent: number;
@@ -8,4 +8,5 @@ export declare class JsiSkMatrix extends HostObject<Matrix3x3, "Matrix"> impleme
8
8
  scale(x: number, y?: number): void;
9
9
  skew(x: number, y: number): void;
10
10
  rotate(value: number): void;
11
+ identity(): void;
11
12
  }
@@ -3,4 +3,3 @@ declare global {
3
3
  var SkiaValueApi: ISkiaValueApi;
4
4
  }
5
5
  export declare const ValueApi: ISkiaValueApi;
6
- export declare const createValue: <T>(initialValue: T) => import("./types").SkiaMutableValue<T>, createComputedValue: <R>(cb: () => R, values: import("./types").SkiaValue<unknown>[]) => import("./types").SkiaValue<R>;
@@ -3,6 +3,7 @@ import type { PointerEvent } from "react";
3
3
  import type { SkRect } from "../skia/types";
4
4
  import type { SkiaValue } from "../values";
5
5
  import type { DrawMode, SkiaViewProps } from "./types";
6
+ import { TouchType } from "./types";
6
7
  export declare class SkiaView extends React.Component<SkiaViewProps, {
7
8
  width: number;
8
9
  height: number;
@@ -29,8 +30,8 @@ export declare class SkiaView extends React.Component<SkiaViewProps, {
29
30
  /**
30
31
  * Sends a redraw request to the native SkiaView.
31
32
  */
32
- private redraw;
33
- requestRedraw(): void;
33
+ private tick;
34
+ redraw(): void;
34
35
  /**
35
36
  * Updates the drawing mode for the skia view. This is the same
36
37
  * as declaratively setting the mode property on the SkiaView.
@@ -48,9 +49,6 @@ export declare class SkiaView extends React.Component<SkiaViewProps, {
48
49
  */
49
50
  registerValues(_values: SkiaValue<unknown>[]): void;
50
51
  private handleTouchEvent;
51
- handleTouchStart(evt: PointerEvent): void;
52
- handleTouchMove(evt: PointerEvent): void;
53
- handleTouchEnd(evt: PointerEvent): void;
54
- handleTouchCancel(evt: PointerEvent): void;
52
+ createTouchHandler(touchType: TouchType): (evt: PointerEvent) => void;
55
53
  render(): JSX.Element;
56
54
  }
@@ -0,0 +1,6 @@
1
+ import type { CanvasKit as CanvasKitType, CanvasKitInitOptions } from "canvaskit-wasm";
2
+ declare global {
3
+ var CanvasKit: CanvasKitType;
4
+ }
5
+ export declare const LoadSkiaWeb: (opts?: CanvasKitInitOptions | undefined) => Promise<void>;
6
+ export declare const LoadSkia: (opts?: CanvasKitInitOptions | undefined) => Promise<void>;
@@ -0,0 +1,12 @@
1
+ import type { ComponentProps, ComponentType } from "react";
2
+ import { Suspense } from "react";
3
+ import { LoadSkiaWeb } from "./LoadSkiaWeb";
4
+ interface WithSkiaProps {
5
+ fallback?: ComponentProps<typeof Suspense>["fallback"];
6
+ getComponent: () => Promise<{
7
+ default: ComponentType;
8
+ }>;
9
+ opts?: Parameters<typeof LoadSkiaWeb>[0];
10
+ }
11
+ export declare const WithSkiaWeb: ({ getComponent, fallback, opts, }: WithSkiaProps) => JSX.Element;
12
+ export {};
@@ -1,5 +1,2 @@
1
- import type { CanvasKit as CanvasKitType } from "canvaskit-wasm";
2
- declare global {
3
- var CanvasKit: CanvasKitType;
4
- }
5
- export declare const LoadSkia: () => Promise<void>;
1
+ export * from "./LoadSkiaWeb";
2
+ export * from "./WithSkiaWeb";
package/package.json CHANGED
@@ -3,8 +3,11 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
+ "bin": {
7
+ "setup-skia-web": "./scripts/setup-canvaskit.js"
8
+ },
6
9
  "title": "React Native Skia",
7
- "version": "0.1.134",
10
+ "version": "0.1.138",
8
11
  "description": "High-performance React Native Graphics using Skia",
9
12
  "main": "lib/module/index.js",
10
13
  "files": [
@@ -21,6 +24,7 @@
21
24
  "android/src/**",
22
25
  "libs/android/**",
23
26
  "index.js",
27
+ "jestSetup.js",
24
28
  "cpp/**/*.{h,cpp}",
25
29
  "ios",
26
30
  "libs/ios/libskia.xcframework",
@@ -28,6 +32,7 @@
28
32
  "libs/ios/libsvg.xcframework",
29
33
  "react-native-skia.podspec",
30
34
  "scripts/install-npm.js",
35
+ "scripts/setup-canvaskit.js",
31
36
  "dist/**"
32
37
  ],
33
38
  "scripts": {
@@ -72,7 +77,6 @@
72
77
  "eslint-config-react-native-wcandillon": "^3.7.2",
73
78
  "eslint-plugin-reanimated": "^1.2.6",
74
79
  "jest": "^27.4.3",
75
- "patch-package": "^6.4.7",
76
80
  "react": "17.0.2",
77
81
  "react-native": "0.66.2",
78
82
  "react-native-builder-bob": "^0.18.2",
@@ -80,7 +84,7 @@
80
84
  "typescript": "^4.6.4"
81
85
  },
82
86
  "dependencies": {
83
- "canvaskit-wasm": "^0.34.0",
87
+ "canvaskit-wasm": "^0.35.0",
84
88
  "react-reconciler": "^0.26.2"
85
89
  },
86
90
  "eslintIgnore": [
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable max-len */
3
+
4
+ /**
5
+ * A script to automate the setup of `@shopify/react-native-skia` for web.
6
+ * The only requirement is that your project supports a 'static' folder (often named '/public').
7
+ * In `@expo/webpack-config` this is `./web` (default for now).
8
+ *
9
+ * This script does the following:
10
+ * 1. Resolve the public path relative to wherever the script is being run.
11
+ * 2. Log out some useful info about the web setup, just in case anything goes wrong.
12
+ * 3. Resolve the installed wasm file `canvaskit-wasm/bin/full/canvaskit.wasm`
13
+ * from `@shopify/react-native-skia -> canvaskit`.
14
+ * 4. Recursively ensure the path exists and copy the file into the desired location.
15
+ *
16
+ *
17
+ * Usage:
18
+ * $ `npx <script> web`
19
+ *
20
+ * -> Copies the file to `<project>/web/static/js/canvaskit.wasm`
21
+ *
22
+ * Tooling that uses `/public`:
23
+ * $ `npx <script> public`
24
+ *
25
+ * -> Copies the file to `<project>/public/static/js/canvaskit.wasm`
26
+ */
27
+ const fs = require("fs");
28
+ const path = require("path");
29
+
30
+ const args = process.argv.slice(2);
31
+
32
+ const gray = (text) => `\x1b[90m${text}\x1b[0m`;
33
+ const lime = (text) => `\x1b[32m${text}\x1b[0m`;
34
+
35
+ function getWasmFilePath() {
36
+ try {
37
+ return require.resolve("canvaskit-wasm/bin/full/canvaskit.wasm");
38
+ } catch (error) {
39
+ console.error(
40
+ `Could not find 'canvaskit-wasm'. Please install '@shopify/react-native-skia' and ensure it can be resolved from your project: ${process.cwd()}`
41
+ );
42
+ process.exit(1);
43
+ }
44
+ }
45
+
46
+ function getOutputFilePath() {
47
+ // Default to using `web` public path.
48
+ const publicFolder = path.resolve(args[0] || "web");
49
+ const publicLocation = "./static/js/canvaskit.wasm";
50
+ const output = path.join(publicFolder, publicLocation);
51
+
52
+ console.log(
53
+ `› Copying 'canvaskit.wasm' file to static folder:\n ${gray(output)}\n`
54
+ );
55
+ return output;
56
+ }
57
+
58
+ function copyFile(from, to) {
59
+ const data = fs.readFileSync(from);
60
+ fs.mkdirSync(path.dirname(to), { recursive: true });
61
+ fs.writeFileSync(to, data);
62
+ }
63
+
64
+ // Copy the WASM file to `<static>/static/js/canvaskit.wasm`
65
+ (() => {
66
+ copyFile(getWasmFilePath(), getOutputFilePath());
67
+
68
+ console.log(lime("› Success! You are almost there:"));
69
+ console.log(
70
+ gray(
71
+ "› To load React Native Skia Web, follow these instructions : https://shopify.github.io/react-native-skia/docs/getting-started/web"
72
+ )
73
+ );
74
+ })();
@@ -60,7 +60,9 @@ function isExtrapolate(value: string): value is Extrapolate {
60
60
 
61
61
  // validates extrapolations type
62
62
  // if type is correct, converts it to ExtrapolationConfig
63
- function validateType(type: ExtrapolationType): RequiredExtrapolationConfig {
63
+ export function validateInterpolationOptions(
64
+ type: ExtrapolationType
65
+ ): RequiredExtrapolationConfig {
64
66
  // initialize extrapolationConfig with default extrapolation
65
67
  const extrapolationConfig: RequiredExtrapolationConfig = {
66
68
  extrapolateLeft: Extrapolate.EXTEND,
@@ -151,7 +153,7 @@ export function interpolate(
151
153
  );
152
154
  }
153
155
 
154
- const extrapolationConfig = validateType(type);
156
+ const extrapolationConfig = validateInterpolationOptions(type);
155
157
  const { length } = input;
156
158
  const narrowedInput: InterpolationNarrowedInput = {
157
159
  leftEdgeInput: input[0],
@@ -1,4 +1,4 @@
1
- import { mix } from "../../renderer";
1
+ import { mix } from "../../renderer/processors/math";
2
2
  import type { Color, SkColor } from "../../skia";
3
3
  import { Skia } from "../../skia";
4
4
 
@@ -1,10 +1,26 @@
1
1
  import type { SkPath } from "../../skia/types";
2
+ import { exhaustiveCheck } from "../../renderer/typeddash";
3
+
4
+ import type { ExtrapolationType } from "./interpolate";
5
+ import { validateInterpolationOptions, Extrapolate } from "./interpolate";
6
+
7
+ const lerp = (
8
+ value: number,
9
+ from: number,
10
+ to: number,
11
+ p1: SkPath,
12
+ p2: SkPath
13
+ ) => {
14
+ const t = (value - from) / (to - from);
15
+ return p2.interpolate(p1, t)!;
16
+ };
2
17
 
3
18
  /**
4
19
  * Maps an input value within a range to an output path within a path range.
5
20
  * @param value - The input value.
6
21
  * @param inputRange - The range of the input value.
7
22
  * @param outputRange - The range of the output path.
23
+ * @param options - Extrapolation options
8
24
  * @returns The output path.
9
25
  * @example <caption>Map a value between 0 and 1 to a path between two paths.</caption>
10
26
  * const path1 = new Path();
@@ -18,21 +34,54 @@ import type { SkPath } from "../../skia/types";
18
34
  export const interpolatePaths = (
19
35
  value: number,
20
36
  input: number[],
21
- outputRange: SkPath[]
37
+ outputRange: SkPath[],
38
+ options?: ExtrapolationType
22
39
  ) => {
40
+ const extrapolation = validateInterpolationOptions(options);
41
+ if (value < input[0]) {
42
+ switch (extrapolation.extrapolateLeft) {
43
+ case Extrapolate.CLAMP:
44
+ return outputRange[0];
45
+ case Extrapolate.EXTEND:
46
+ return lerp(value, input[0], input[1], outputRange[0], outputRange[1]);
47
+ case Extrapolate.IDENTITY:
48
+ throw new Error(
49
+ "Identity is not a supported extrapolation type for interpolatePaths()"
50
+ );
51
+ default:
52
+ exhaustiveCheck(extrapolation.extrapolateLeft);
53
+ }
54
+ } else if (value > input[input.length - 1]) {
55
+ switch (extrapolation.extrapolateRight) {
56
+ case Extrapolate.CLAMP:
57
+ return outputRange[outputRange.length - 1];
58
+ case Extrapolate.EXTEND:
59
+ return lerp(
60
+ value,
61
+ input[input.length - 2],
62
+ input[input.length - 1],
63
+ outputRange[input.length - 2],
64
+ outputRange[input.length - 1]
65
+ );
66
+ case Extrapolate.IDENTITY:
67
+ throw new Error(
68
+ "Identity is not a supported extrapolation type for interpolatePaths()"
69
+ );
70
+ default:
71
+ exhaustiveCheck(extrapolation.extrapolateRight);
72
+ }
73
+ }
23
74
  let i = 0;
24
75
  for (; i <= input.length - 1; i++) {
25
76
  if (value >= input[i] && value <= input[i + 1]) {
26
77
  break;
27
78
  }
28
- if (i === input.length - 1) {
29
- if (value < input[0]) {
30
- return outputRange[0];
31
- } else {
32
- return outputRange[i];
33
- }
34
- }
35
79
  }
36
- const t = (value - input[i]) / (input[i + 1] - input[i]);
37
- return outputRange[i + 1].interpolate(outputRange[i], t)!;
80
+ return lerp(
81
+ value,
82
+ input[i],
83
+ input[i + 1],
84
+ outputRange[i],
85
+ outputRange[i + 1]
86
+ );
38
87
  };
package/src/mock/index.ts CHANGED
@@ -1,156 +1,110 @@
1
- /**
2
- * Mock implementation for test runners.
3
- *
4
- * Example:
5
- *
6
- * ```js
7
- * jest.mock('@shopify/react-native-skia', () => require('@shopify/react-native-skia/lib/commonjs/mock'));
8
- * ```
9
- */
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
10
2
 
11
- import type { Skia as SkiaApi, SkRect } from "../skia/types";
3
+ import type { Color, Skia as SkiaApi, SkRect, Vector } from "../skia/types";
4
+ import * as Values from "../values/web";
5
+ import * as ValuesHooks from "../values/hooks";
6
+ import * as BaseSkia from "../skia/types";
7
+ import type * as SkiaExports from "../skia";
8
+ import type * as ExternalExports from "../external";
9
+ import type * as ValueExports from "../values";
10
+ import type * as AnimationExports from "../animation";
11
+ import { useSharedValueEffect } from "../external/reanimated/useSharedValueEffect";
12
+ import * as timingFunctions from "../animation/timing";
13
+ import * as springFunctions from "../animation/spring";
14
+ import * as decayFunctions from "../animation/decay";
15
+ import * as interpolateFn from "../animation/functions/interpolate";
16
+ import * as interpolatePathFn from "../animation/functions/interpolatePaths";
17
+ import * as interpolateVectorFn from "../animation/functions/interpolateVector";
18
+ import { ShaderLib } from "../renderer/components/shaders/ShaderLib";
12
19
 
13
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- const MockJSIInstance: any = {};
15
- const Noop = () => MockJSIInstance;
20
+ class Stub {
21
+ constructor() {
22
+ return new Proxy(() => {}, {
23
+ get: () => new Stub(),
24
+ apply: () => new Stub(),
25
+ set: () => true,
26
+ });
27
+ }
28
+ }
16
29
 
17
- export const Skia: SkiaApi = {
18
- Point: Noop,
19
- XYWHRect: Noop,
20
- RuntimeShaderBuilder: Noop,
21
- RRectXY: Noop,
22
- RSXform: Noop,
23
- Color: Noop,
24
- ContourMeasureIter: Noop,
25
- Paint: Noop,
26
- PictureRecorder: Noop,
27
- Picture: {
28
- MakePicture: Noop,
29
- },
30
- Path: {
31
- Make: Noop,
32
- MakeFromSVGString: Noop,
33
- MakeFromOp: Noop,
34
- MakeFromCmds: Noop,
35
- MakeFromText: Noop,
36
- },
37
- Matrix: Noop,
38
- ColorFilter: {
39
- MakeMatrix: Noop,
40
- MakeBlend: Noop,
41
- MakeCompose: Noop,
42
- MakeLerp: Noop,
43
- MakeLinearToSRGBGamma: Noop,
44
- MakeSRGBToLinearGamma: Noop,
45
- MakeLumaColorFilter: Noop,
46
- },
47
- Font: Noop,
48
- Typeface: {
49
- MakeFreeTypeFaceFromData: Noop,
50
- },
51
- MaskFilter: {
52
- MakeBlur: Noop,
53
- },
54
- RuntimeEffect: {
55
- Make: Noop,
56
- },
57
- ImageFilter: {
58
- MakeOffset: Noop,
59
- MakeDisplacementMap: Noop,
60
- MakeShader: Noop,
61
- MakeBlur: Noop,
62
- MakeColorFilter: Noop,
63
- MakeCompose: Noop,
64
- MakeDropShadow: Noop,
65
- MakeDropShadowOnly: Noop,
66
- MakeErode: Noop,
67
- MakeDilate: Noop,
68
- MakeBlend: Noop,
69
- MakeRuntimeShader: Noop,
70
- },
71
- Shader: {
72
- MakeLinearGradient: Noop,
73
- MakeRadialGradient: Noop,
74
- MakeTwoPointConicalGradient: Noop,
75
- MakeSweepGradient: Noop,
76
- MakeTurbulence: Noop,
77
- MakeFractalNoise: Noop,
78
- MakeBlend: Noop,
79
- MakeColor: Noop,
80
- },
81
- PathEffect: {
82
- MakeCorner: Noop,
83
- MakeDash: Noop,
84
- MakeDiscrete: Noop,
85
- MakeCompose: Noop,
86
- MakeSum: Noop,
87
- MakeLine2D: Noop,
88
- MakePath1D: Noop,
89
- MakePath2D: Noop,
90
- },
91
- MakeVertices: Noop,
92
- Data: {
93
- fromURI: Noop,
94
- fromBytes: Noop,
95
- fromBase64: Noop,
96
- },
97
- Image: {
98
- MakeImageFromEncoded: Noop,
99
- MakeImage: Noop,
100
- },
101
- SVG: {
102
- MakeFromData: Noop,
103
- MakeFromString: Noop,
104
- },
105
- FontMgr: {
106
- RefDefault: Noop,
107
- },
108
- TextBlob: {
109
- MakeFromText: Noop,
110
- MakeFromGlyphs: Noop,
111
- MakeFromRSXform: Noop,
112
- MakeFromRSXformGlyphs: Noop,
113
- },
114
- Surface: {
115
- Make: Noop,
116
- },
117
- };
118
-
119
- export const vec = (x: number, y: number) => ({ x, y });
30
+ const Noop: () => any = () => {};
120
31
 
121
- export const rect = (x: number, y: number, width: number, height: number) => ({
122
- x,
123
- y,
124
- width,
125
- height,
126
- });
32
+ export const Skia: SkiaApi = new Stub() as any;
127
33
 
128
- export const rrect = (r: SkRect, rx: number, ry: number) => ({
129
- rect: r,
130
- rx,
131
- ry,
132
- });
34
+ export const vec = (x?: number, y?: number) => ({ x: x ?? 0, y: y ?? x ?? 0 });
133
35
 
134
- export const useTouchHandler = Noop;
135
- export const useComputedValue = Noop;
136
- export const useValue = Noop;
137
- export const useClockValue = Noop;
138
- export const useValueEffect = Noop;
139
- export const useTiming = Noop;
140
- export const runTiming = Noop;
141
- export const timing = Noop;
142
- export const useSpring = Noop;
143
- export const runSpring = Noop;
144
- export const spring = Noop;
145
- export const runDecay = Noop;
146
- export const decay = Noop;
36
+ export const Mock: typeof SkiaExports &
37
+ typeof ExternalExports &
38
+ typeof ValueExports &
39
+ typeof AnimationExports & {
40
+ createDrawing: () => any;
41
+ createDeclaration: () => any;
42
+ ShaderLib: typeof ShaderLib;
43
+ } = {
44
+ // SkiaExports
45
+ // 1. Skia API. BaseSkia contains the enums, and functions like isPaint etc
46
+ Skia,
47
+ ...BaseSkia,
48
+ // 2. Hooks
49
+ useDataCollection: Noop,
50
+ useRawData: Noop,
51
+ useData: Noop,
52
+ useFont: Noop,
53
+ useTypeface: Noop,
54
+ useImage: Noop,
55
+ usePath: Noop,
56
+ useSVG: Noop,
57
+ useTextPath: Noop,
58
+ usePaint: Noop,
59
+ usePicture: Noop,
60
+ useSvgPath: Noop,
61
+ // 3. Point/Rect/Transform utilities
62
+ vec,
63
+ rect: (x: number, y: number, width: number, height: number) => ({
64
+ x,
65
+ y,
66
+ width,
67
+ height,
68
+ }),
69
+ rrect: (r: SkRect, rx: number, ry: number) => ({
70
+ rect: r,
71
+ rx,
72
+ ry,
73
+ }),
74
+ point: vec,
75
+ add: (a: Vector, b: Vector) => vec(a.x + b.x, a.y + b.y),
76
+ sub: (a: Vector, b: Vector) => vec(a.x - b.x, a.y - b.y),
77
+ neg: (a: Vector) => vec(-a.x, -a.y),
78
+ dist: (a: Vector, b: Vector) => Math.hypot(a.x - b.x, a.y - b.y),
79
+ translate: ({ x, y }: Vector) =>
80
+ [{ translateX: x }, { translateY: y }] as const,
147
81
 
148
- export const useSharedValueEffect = Noop;
149
-
150
- export const useData = Noop;
151
- export const useDataCollection = Noop;
152
- export const useFont = Noop;
153
- export const useImage = Noop;
154
- export const usePicture = Noop;
155
- export const useSVG = Noop;
156
- export const useTypeface = Noop;
82
+ bounds: Noop,
83
+ topLeft: Noop,
84
+ topRight: Noop,
85
+ bottomLeft: Noop,
86
+ bottomRight: Noop,
87
+ center: Noop,
88
+ processTransform2d: Noop,
89
+ // ExternalExports
90
+ useSharedValueEffect,
91
+ // ValueExports
92
+ ...Values,
93
+ ...ValuesHooks,
94
+ // Animations
95
+ ...timingFunctions,
96
+ ...springFunctions,
97
+ ...decayFunctions,
98
+ ...interpolateFn,
99
+ ...interpolatePathFn,
100
+ ...interpolateVectorFn,
101
+ interpolateColors: (
102
+ _value: number,
103
+ _inputRange: number[],
104
+ _outputRange: Color[]
105
+ ) => Float32Array.of(0, 0, 0, 0),
106
+ mixColors: (_v: number, _x: Color, _y: Color) => Float32Array.of(0, 0, 0, 0),
107
+ ShaderLib,
108
+ createDrawing: Noop,
109
+ createDeclaration: Noop,
110
+ };
@@ -58,7 +58,7 @@ const onDraw = createDrawing<GroupProps>(
58
58
  } else {
59
59
  canvas.save();
60
60
  }
61
- processCanvasTransform(ctx, groupProps);
61
+ processCanvasTransform(canvas, groupProps);
62
62
  if (clip) {
63
63
  const op = invertClip ? ClipOp.Difference : ClipOp.Intersect;
64
64
  processClip(Skia, canvas, clip, op);