Skip to content

Commit

Permalink
Add Freeze example
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon committed Apr 20, 2022
1 parent 3827f1b commit aed34ea
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 50 deletions.
36 changes: 35 additions & 1 deletion example/src/Examples/API/Freeze.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import type { ReactNode } from "react";
import React, { useEffect, useState } from "react";
import type { SkPaint, SkRect } from "@shopify/react-native-skia";
import {
FilterMode,
Canvas,
Group,
Rect,
Text,
useClockValue,
useDerivedValue,
Freeze,
rect,
createDrawing,
Skia,
TileMode,
} from "@shopify/react-native-skia";

const size = 200;
Expand Down Expand Up @@ -61,3 +66,32 @@ const Checkerboard = ({ color }: { color: string }) => {
</>
);
};

interface FreezeProps {
rect: SkRect;
children?: ReactNode | ReactNode[];
}

const onDraw = createDrawing<FreezeProps>(
(ctx, { rect: boundingRect }, node) => {
if (node.memoized === null) {
const recorder = Skia.PictureRecorder();
const canvas = recorder.beginRecording(boundingRect);
node.visit({
...ctx,
canvas,
});
const pic = recorder.finishRecordingAsPicture();
const shaderPaint = Skia.Paint();
shaderPaint.setShader(
pic.makeShader(TileMode.Decal, TileMode.Decal, FilterMode.Nearest)
);
node.memoized = shaderPaint;
}
ctx.canvas.drawRect(boundingRect, node.memoized as SkPaint);
}
);

export const Freeze = (props: FreezeProps) => {
return <skDrawing onDraw={onDraw} skipProcessing {...props} />;
};
48 changes: 0 additions & 48 deletions package/src/renderer/components/Freeze.tsx

This file was deleted.

1 change: 0 additions & 1 deletion package/src/renderer/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export * from "./pathEffects";
export * from "../processors";
export * from "./Picture";

export * from "./Freeze";
export * from "./Group";
export * from "./Mask";
export * from "./Paint";
Expand Down

0 comments on commit aed34ea

Please sign in to comment.