@shopify/react-native-skia 0.1.157 → 0.1.158

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/android/build.gradle +10 -5
  2. package/cpp/api/JsiSkContourMeasure.h +7 -5
  3. package/lib/commonjs/dom/nodes/DrawingNode.js +1 -5
  4. package/lib/commonjs/dom/nodes/DrawingNode.js.map +1 -1
  5. package/lib/commonjs/dom/nodes/RenderNode.js +2 -8
  6. package/lib/commonjs/dom/nodes/RenderNode.js.map +1 -1
  7. package/lib/commonjs/dom/nodes/drawings/Text.js +4 -9
  8. package/lib/commonjs/dom/nodes/drawings/Text.js.map +1 -1
  9. package/lib/commonjs/dom/types/Common.js.map +1 -1
  10. package/lib/commonjs/dom/types/Drawings.js.map +1 -1
  11. package/lib/commonjs/mock/index.js +0 -1
  12. package/lib/commonjs/mock/index.js.map +1 -1
  13. package/lib/commonjs/renderer/Canvas.js +9 -1
  14. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  15. package/lib/commonjs/renderer/HostComponents.js.map +1 -1
  16. package/lib/commonjs/renderer/components/Mask.js +1 -3
  17. package/lib/commonjs/renderer/components/Mask.js.map +1 -1
  18. package/lib/commonjs/renderer/components/Paint.js +5 -18
  19. package/lib/commonjs/renderer/components/Paint.js.map +1 -1
  20. package/lib/commonjs/renderer/useCanvas.js +18 -2
  21. package/lib/commonjs/renderer/useCanvas.js.map +1 -1
  22. package/lib/commonjs/skia/core/Picture.js +1 -24
  23. package/lib/commonjs/skia/core/Picture.js.map +1 -1
  24. package/lib/commonjs/skia/core/Rect.js +1 -1
  25. package/lib/commonjs/skia/core/Rect.js.map +1 -1
  26. package/lib/commonjs/skia/types/ContourMeasure.js.map +1 -1
  27. package/lib/commonjs/skia/types/Size.js +2 -0
  28. package/lib/commonjs/skia/types/Size.js.map +1 -0
  29. package/lib/commonjs/skia/types/index.js +13 -0
  30. package/lib/commonjs/skia/types/index.js.map +1 -1
  31. package/lib/commonjs/skia/web/JsiSkColor.js +8 -0
  32. package/lib/commonjs/skia/web/JsiSkColor.js.map +1 -1
  33. package/lib/commonjs/skia/web/JsiSkContourMeasure.js +4 -7
  34. package/lib/commonjs/skia/web/JsiSkContourMeasure.js.map +1 -1
  35. package/lib/commonjs/values/web/RNSkReadonlyValue.js +4 -2
  36. package/lib/commonjs/values/web/RNSkReadonlyValue.js.map +1 -1
  37. package/lib/commonjs/views/SkiaBaseWebView.js +2 -2
  38. package/lib/commonjs/views/SkiaBaseWebView.js.map +1 -1
  39. package/lib/module/dom/nodes/DrawingNode.js +2 -6
  40. package/lib/module/dom/nodes/DrawingNode.js.map +1 -1
  41. package/lib/module/dom/nodes/RenderNode.js +1 -4
  42. package/lib/module/dom/nodes/RenderNode.js.map +1 -1
  43. package/lib/module/dom/nodes/drawings/Text.js +4 -9
  44. package/lib/module/dom/nodes/drawings/Text.js.map +1 -1
  45. package/lib/module/dom/types/Common.js.map +1 -1
  46. package/lib/module/dom/types/Drawings.js.map +1 -1
  47. package/lib/module/mock/index.js +0 -1
  48. package/lib/module/mock/index.js.map +1 -1
  49. package/lib/module/renderer/Canvas.js +9 -1
  50. package/lib/module/renderer/Canvas.js.map +1 -1
  51. package/lib/module/renderer/HostComponents.js.map +1 -1
  52. package/lib/module/renderer/components/Mask.js +2 -4
  53. package/lib/module/renderer/components/Mask.js.map +1 -1
  54. package/lib/module/renderer/components/Paint.js +3 -14
  55. package/lib/module/renderer/components/Paint.js.map +1 -1
  56. package/lib/module/renderer/useCanvas.js +13 -0
  57. package/lib/module/renderer/useCanvas.js.map +1 -1
  58. package/lib/module/skia/core/Picture.js +0 -18
  59. package/lib/module/skia/core/Picture.js.map +1 -1
  60. package/lib/module/skia/core/Rect.js +1 -1
  61. package/lib/module/skia/core/Rect.js.map +1 -1
  62. package/lib/module/skia/types/ContourMeasure.js.map +1 -1
  63. package/lib/module/skia/types/Size.js +2 -0
  64. package/lib/module/skia/types/Size.js.map +1 -0
  65. package/lib/module/skia/types/index.js +1 -0
  66. package/lib/module/skia/types/index.js.map +1 -1
  67. package/lib/module/skia/web/JsiSkColor.js +8 -0
  68. package/lib/module/skia/web/JsiSkColor.js.map +1 -1
  69. package/lib/module/skia/web/JsiSkContourMeasure.js +3 -7
  70. package/lib/module/skia/web/JsiSkContourMeasure.js.map +1 -1
  71. package/lib/module/values/web/RNSkReadonlyValue.js +4 -2
  72. package/lib/module/values/web/RNSkReadonlyValue.js.map +1 -1
  73. package/lib/module/views/SkiaBaseWebView.js +2 -2
  74. package/lib/module/views/SkiaBaseWebView.js.map +1 -1
  75. package/lib/typescript/src/dom/nodes/RenderNode.d.ts +1 -3
  76. package/lib/typescript/src/dom/types/Common.d.ts +2 -3
  77. package/lib/typescript/src/dom/types/Drawings.d.ts +1 -3
  78. package/lib/typescript/src/renderer/Canvas.d.ts +4 -1
  79. package/lib/typescript/src/renderer/HostComponents.d.ts +1 -5
  80. package/lib/typescript/src/renderer/components/Paint.d.ts +3 -4
  81. package/lib/typescript/src/renderer/useCanvas.d.ts +1 -0
  82. package/lib/typescript/src/skia/core/Picture.d.ts +0 -8
  83. package/lib/typescript/src/skia/types/ContourMeasure.d.ts +3 -2
  84. package/lib/typescript/src/skia/types/Size.d.ts +4 -0
  85. package/lib/typescript/src/skia/types/index.d.ts +1 -0
  86. package/lib/typescript/src/skia/web/JsiSkContourMeasure.d.ts +2 -6
  87. package/package.json +2 -2
  88. package/src/dom/nodes/DrawingNode.ts +2 -4
  89. package/src/dom/nodes/RenderNode.ts +1 -13
  90. package/src/dom/nodes/drawings/Text.ts +4 -4
  91. package/src/dom/types/Common.ts +2 -4
  92. package/src/dom/types/Drawings.ts +1 -4
  93. package/src/mock/index.ts +0 -1
  94. package/src/renderer/Canvas.tsx +7 -2
  95. package/src/renderer/HostComponents.ts +1 -4
  96. package/src/renderer/components/Mask.tsx +2 -2
  97. package/src/renderer/components/Paint.tsx +3 -14
  98. package/src/renderer/useCanvas.ts +14 -0
  99. package/src/skia/core/Picture.ts +0 -24
  100. package/src/skia/core/Rect.ts +1 -1
  101. package/src/skia/types/ContourMeasure.tsx +3 -2
  102. package/src/skia/types/Size.ts +4 -0
  103. package/src/skia/types/index.ts +1 -0
  104. package/src/skia/web/JsiSkColor.ts +11 -0
  105. package/src/skia/web/JsiSkContourMeasure.ts +7 -3
  106. package/src/values/web/RNSkReadonlyValue.ts +4 -2
  107. package/src/views/SkiaBaseWebView.tsx +2 -2
@@ -57,10 +57,10 @@ export class TextPathNode extends JsiDrawingNode<TextPathProps, SkTextBlob> {
57
57
  }
58
58
  // Gives us the (x, y) coordinates as well as the cos/sin of the tangent
59
59
  // line at that position.
60
- const { px, py, tx, ty } = cont.getPosTan(dist);
61
- const adjustedX = px - (width / 2) * tx;
62
- const adjustedY = py - (width / 2) * ty;
63
- rsx.push(this.Skia.RSXform(tx, ty, adjustedX, adjustedY));
60
+ const [p, t] = cont.getPosTan(dist);
61
+ const adjustedX = p.x - (width / 2) * t.x;
62
+ const adjustedY = p.y - (width / 2) * t.y;
63
+ rsx.push(this.Skia.RSXform(t.x, t.y, adjustedX, adjustedY));
64
64
  dist += width / 2;
65
65
  }
66
66
  return this.Skia.TextBlob.MakeFromRSXform(text, rsx, font);
@@ -1,4 +1,4 @@
1
- import type { ReactNode, RefObject } from "react";
1
+ import type { ReactNode } from "react";
2
2
 
3
3
  import type {
4
4
  BlendMode,
@@ -15,8 +15,6 @@ import type {
15
15
  Vector,
16
16
  } from "../../skia/types";
17
17
 
18
- import type { DeclarationNode } from "./Node";
19
-
20
18
  export type SkEnum<T> = Uncapitalize<keyof T extends string ? keyof T : never>;
21
19
 
22
20
  export type PathDef = string | SkPath;
@@ -86,5 +84,5 @@ export interface PaintProps extends ChildrenProps {
86
84
  export interface GroupProps extends PaintProps, TransformProps {
87
85
  clip?: ClipDef;
88
86
  invertClip?: boolean;
89
- layer?: RefObject<DeclarationNode<unknown, SkPaint>> | SkPaint | boolean;
87
+ layer?: SkPaint | boolean;
90
88
  }
@@ -1,5 +1,3 @@
1
- import type { RefObject } from "react";
2
-
3
1
  import type {
4
2
  FillType,
5
3
  SkImage,
@@ -29,10 +27,9 @@ import type {
29
27
  SkEnum,
30
28
  } from "./Common";
31
29
  import type { DrawingContext } from "./DrawingContext";
32
- import type { DeclarationNode } from "./Node";
33
30
 
34
31
  export interface DrawingNodeProps extends GroupProps {
35
- paint?: SkPaint | RefObject<DeclarationNode<unknown, SkPaint>>;
32
+ paint?: SkPaint;
36
33
  }
37
34
 
38
35
  export type ImageProps = DrawingNodeProps &
package/src/mock/index.ts CHANGED
@@ -53,7 +53,6 @@ export const Mock: typeof SkiaExports &
53
53
  useTypeface: Noop,
54
54
  useImage: Noop,
55
55
  useSVG: Noop,
56
- usePicture: Noop,
57
56
  createPicture: Noop,
58
57
  // 3. Point/Rect/Transform utilities
59
58
  vec,
@@ -20,7 +20,8 @@ import { SkiaView, useDrawCallback } from "../views";
20
20
  import type { TouchHandler } from "../views";
21
21
  import { useValue } from "../values/hooks/useValue";
22
22
  import { Skia } from "../skia/Skia";
23
- import type { SkiaValue } from "../values";
23
+ import type { SkiaValue, SkiaMutableValue } from "../values";
24
+ import type { SkSize } from "../skia/types";
24
25
 
25
26
  import { debug as hostDebug, skHostConfig } from "./HostConfig";
26
27
  // import { debugTree } from "./nodes";
@@ -49,10 +50,11 @@ export interface CanvasProps extends ComponentProps<typeof SkiaView> {
49
50
  ref?: RefObject<SkiaView>;
50
51
  children: ReactNode;
51
52
  onTouch?: TouchHandler;
53
+ onSize?: SkiaMutableValue<SkSize>;
52
54
  }
53
55
 
54
56
  export const Canvas = forwardRef<SkiaView, CanvasProps>(
55
- ({ children, style, debug, mode, onTouch }, forwardedRef) => {
57
+ ({ children, style, debug, mode, onTouch, onSize }, forwardedRef) => {
56
58
  const size = useValue({ width: 0, height: 0 });
57
59
  const canvasCtx = useMemo(() => ({ Skia, size }), [size]);
58
60
  const innerRef = useCanvasRef();
@@ -114,6 +116,9 @@ export const Canvas = forwardRef<SkiaView, CanvasProps>(
114
116
  height !== canvasCtx.size.current.height
115
117
  ) {
116
118
  canvasCtx.size.current = { width, height };
119
+ if (onSize) {
120
+ onSize.current = { width, height };
121
+ }
117
122
  }
118
123
  paint.reset();
119
124
  const ctx = {
@@ -1,5 +1,3 @@
1
- import type { ForwardedRef } from "react";
2
-
3
1
  import { NodeType } from "../dom/types";
4
2
  import type {
5
3
  CircleProps,
@@ -55,7 +53,6 @@ import type {
55
53
  BlendProps,
56
54
  MorphologyImageFilterProps,
57
55
  } from "../dom/types/ImageFilters";
58
- import type { PaintNode } from "../dom/nodes/PaintNode";
59
56
 
60
57
  import type { Container } from "./Container";
61
58
  import { exhaustiveCheck } from "./typeddash";
@@ -67,7 +64,7 @@ declare global {
67
64
  interface IntrinsicElements {
68
65
  skGroup: SkiaProps<GroupProps>;
69
66
  skLayer: SkiaProps<ChildrenProps>;
70
- skPaint: SkiaProps<PaintProps> & { ref: ForwardedRef<PaintNode> };
67
+ skPaint: SkiaProps<PaintProps>;
71
68
 
72
69
  // Drawings
73
70
  skFill: SkiaProps<DrawingNodeProps>;
@@ -2,7 +2,7 @@ import type { ReactNode } from "react";
2
2
  import React, { useMemo } from "react";
3
3
 
4
4
  import { BlendMode } from "../../skia/types";
5
- import { useCanvas } from "../useCanvas";
5
+ import { useSkiaPrivate } from "../useCanvas";
6
6
 
7
7
  import { Group } from "./Group";
8
8
 
@@ -14,7 +14,7 @@ interface MaskProps {
14
14
  }
15
15
 
16
16
  export const Mask = ({ children, mask, mode, clip }: MaskProps) => {
17
- const { Skia } = useCanvas();
17
+ const Skia = useSkiaPrivate();
18
18
  const maskPaint = useMemo(() => {
19
19
  const paint = Skia.Paint();
20
20
  paint.setBlendMode(BlendMode.Src);
@@ -1,19 +1,8 @@
1
- /* eslint-disable max-len */
2
- import React, { useRef, forwardRef } from "react";
1
+ import React from "react";
3
2
 
4
3
  import type { SkiaProps } from "../processors";
5
4
  import type { DrawingNodeProps } from "../../dom/types";
6
- import type { PaintNode } from "../../dom/nodes/PaintNode";
7
5
 
8
- export const usePaintRef = () => {
9
- console.log(`usePaintRef() is now deprecated.
10
- If you are using the layer property, simply pass the component directly: https://shopify.github.io/react-native-skia/docs/group#layer-effects.
11
- If you are using the paint property, please the following paint properties directly: https://shopify.github.io/react-native-skia/docs/paint/overview`);
12
- return useRef<PaintNode>(null);
6
+ export const Paint = (props: SkiaProps<DrawingNodeProps>) => {
7
+ return <skPaint {...props} />;
13
8
  };
14
-
15
- export const Paint = forwardRef<PaintNode, SkiaProps<DrawingNodeProps>>(
16
- (props, ref) => {
17
- return <skPaint ref={ref} {...props} />;
18
- }
19
- );
@@ -14,8 +14,22 @@ export const CanvasProvider = CanvasContext.Provider;
14
14
 
15
15
  export const useCanvas = () => {
16
16
  const ctx = useContext(CanvasContext);
17
+ console.warn(
18
+ // eslint-disable-next-line max-len
19
+ "useCanvas is deprecated. use the onSize property instead: https://shopify.github.io/react-native-skia/docs/canvas/overview"
20
+ );
17
21
  if (!ctx) {
18
22
  throw new Error("Canvas context is not available");
19
23
  }
20
24
  return ctx;
21
25
  };
26
+
27
+ // This private function will be removed once we remove the useCanvas hook and
28
+ // implement the Mask component as a node (will be faster too)
29
+ export const useSkiaPrivate = () => {
30
+ const ctx = useContext(CanvasContext);
31
+ if (!ctx) {
32
+ throw new Error("Canvas context is not available");
33
+ }
34
+ return ctx.Skia;
35
+ };
@@ -1,30 +1,6 @@
1
- import type { DependencyList } from "react";
2
- import { useMemo } from "react";
3
-
4
1
  import { Skia } from "../Skia";
5
2
  import type { SkCanvas, SkRect } from "../types";
6
3
 
7
- /**
8
- * Memoizes and returns an SkPicture that can be drawn to another canvas.
9
- * @param rect Picture bounds
10
- * @param cb Callback for drawing to the canvas
11
- * @returns SkPicture
12
- */
13
- export const usePicture = (
14
- rect: SkRect,
15
- cb: (canvas: SkCanvas) => void,
16
- deps: DependencyList = []
17
- ) => {
18
- console.warn("usePicture() is deprecated. Use createPicture() instead.");
19
- return useMemo(() => {
20
- const recorder = Skia.PictureRecorder();
21
- const canvas = recorder.beginRecording(rect);
22
- cb(canvas);
23
- return recorder.finishRecordingAsPicture();
24
- // eslint-disable-next-line react-hooks/exhaustive-deps
25
- }, deps);
26
- };
27
-
28
4
  /**
29
5
  * Memoizes and returns an SkPicture that can be drawn to another canvas.
30
6
  * @param rect Picture bounds
@@ -12,7 +12,7 @@ export const bounds = (rects: SkRect[]) => {
12
12
  const y = Math.min(...rects.map((r) => r.y));
13
13
  const width = Math.max(...rects.map((r) => r.x + r.width));
14
14
  const height = Math.max(...rects.map((r) => r.y + r.height));
15
- return rect(x, y, width, height);
15
+ return rect(x, y, width - x, height - y);
16
16
  };
17
17
 
18
18
  export const topLeft = (r: SkRect | SkRRect) =>
@@ -1,5 +1,6 @@
1
1
  import type { SkJSIInstance } from "./JsiInstance";
2
2
  import type { SkPath } from "./Path/Path";
3
+ import type { SkPoint } from "./Point";
3
4
 
4
5
  export interface PosTan {
5
6
  px: number;
@@ -11,10 +12,10 @@ export interface PosTan {
11
12
  export interface SkContourMeasure extends SkJSIInstance<"ContourMeasure"> {
12
13
  /**
13
14
  * Returns the given position and tangent line for the distance on the given contour.
14
- * The return value is 4 floats in this order: posX, posY, vecX, vecY.
15
+ * The return value an array of 2 vectors: [position, tangent]
15
16
  * @param distance - will be pinned between 0 and length().
16
17
  */
17
- getPosTan(distance: number): PosTan;
18
+ getPosTan(distance: number): [position: SkPoint, tangent: SkPoint];
18
19
 
19
20
  /**
20
21
  * Returns an Path representing the segment of this contour.
@@ -0,0 +1,4 @@
1
+ export interface SkSize {
2
+ width: number;
3
+ height: number;
4
+ }
@@ -25,3 +25,4 @@ export * from "./RSXform";
25
25
  export * from "./JsiInstance";
26
26
  export * from "./Skia";
27
27
  export * from "./TextBlob";
28
+ export * from "./Size";
@@ -241,6 +241,17 @@ const parseCSSColor = (cssStr: string) => {
241
241
  return null;
242
242
  } // Covers NaN.
243
243
  return [(iv & 0xff0000) >> 16, (iv & 0xff00) >> 8, iv & 0xff, 1];
244
+ } else if (str.length === 9) {
245
+ var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
246
+ if (!(iv >= 0 && iv <= 0xffffffff)) {
247
+ return null; // Covers NaN.
248
+ }
249
+ return [
250
+ ((iv & 0xff000000) >> 24) & 0xff,
251
+ (iv & 0xff0000) >> 16,
252
+ (iv & 0xff00) >> 8,
253
+ (iv & 0xff) / 255,
254
+ ];
244
255
  }
245
256
 
246
257
  return null;
@@ -4,6 +4,7 @@ import type { SkContourMeasure } from "../types";
4
4
 
5
5
  import { HostObject } from "./Host";
6
6
  import { JsiSkPath } from "./JsiSkPath";
7
+ import { JsiSkPoint } from "./JsiSkPoint";
7
8
 
8
9
  export class JsiSkContourMeasure
9
10
  extends HostObject<ContourMeasure, "ContourMeasure">
@@ -13,9 +14,12 @@ export class JsiSkContourMeasure
13
14
  super(CanvasKit, ref, "ContourMeasure");
14
15
  }
15
16
 
16
- getPosTan(distance: number) {
17
- const [px, py, tx, ty] = this.ref.getPosTan(distance);
18
- return { px, py, tx, ty };
17
+ getPosTan(distance: number): [position: JsiSkPoint, tangent: JsiSkPoint] {
18
+ const posTan = this.ref.getPosTan(distance);
19
+ return [
20
+ new JsiSkPoint(this.CanvasKit, posTan.slice(0, 2)),
21
+ new JsiSkPoint(this.CanvasKit, posTan.slice(2)),
22
+ ];
19
23
  }
20
24
 
21
25
  getSegment(startD: number, stopD: number, startWithMoveTo: boolean) {
@@ -13,8 +13,10 @@ export class RNSkReadonlyValue<T> implements SkiaValue<T> {
13
13
  }
14
14
 
15
15
  protected update(nextValue: T): void {
16
- this._current = nextValue;
17
- this.notifyListeners();
16
+ if (this._current !== nextValue) {
17
+ this._current = nextValue;
18
+ this.notifyListeners();
19
+ }
18
20
  }
19
21
 
20
22
  public readonly __typename__ = "RNSkValue";
@@ -46,8 +46,8 @@ export abstract class SkiaBaseWebView<
46
46
  // Reset canvas / surface on layout change
47
47
  if (this._canvasRef.current) {
48
48
  const canvas = this._canvasRef.current;
49
- canvas.width = canvas.clientWidth * pd;
50
- canvas.height = canvas.clientHeight * pd;
49
+ canvas.width = width * pd;
50
+ canvas.height = height * pd;
51
51
  const surface = CanvasKit.MakeWebGLCanvasSurface(this._canvasRef.current);
52
52
  if (!surface) {
53
53
  throw new Error("Could not create surface");