@shopify/react-native-skia
Version:
High-performance React Native Graphics using Skia
143 lines (133 loc) • 3.23 kB
text/typescript
import type {
BlurMaskFilterProps,
CircleProps,
CTMProps,
ImageProps,
PointsProps,
PathProps,
RectProps,
RoundedRectProps,
OvalProps,
LineProps,
PatchProps,
VerticesProps,
DiffRectProps,
TextProps,
TextPathProps,
TextBlobProps,
GlyphsProps,
PictureProps,
ImageSVGProps,
ParagraphProps,
AtlasProps,
DrawingNodeProps,
SkottieProps,
} from "../../dom/types";
export enum CommandType {
// Context
Group,
SavePaint,
RestorePaint,
SaveCTM,
RestoreCTM,
PushColorFilter,
PushBlurMaskFilter,
PushImageFilter,
PushPathEffect,
PushShader,
ComposeColorFilter,
ComposeImageFilter,
ComposePathEffect,
MaterializePaint,
SaveBackdropFilter,
SaveLayer,
RestorePaintDeclaration,
// Drawing
DrawBox,
DrawImage,
DrawCircle,
DrawPaint,
DrawPoints,
DrawPath,
DrawRect,
DrawRRect,
DrawOval,
DrawLine,
DrawPatch,
DrawVertices,
DrawDiffRect,
DrawText,
DrawTextPath,
DrawTextBlob,
DrawGlyphs,
DrawPicture,
DrawImageSVG,
DrawParagraph,
DrawAtlas,
DrawSkottie,
}
export type Command<T extends CommandType = CommandType> = {
type: T;
[]: unknown;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const materializeCommand = (command: any) => {
"worklet";
const newProps = { ...command.props };
if (command.animatedProps) {
for (const key in command.animatedProps) {
newProps[key] = command.animatedProps[key].value;
}
}
return { ...command, props: newProps };
};
export const isCommand = <T extends CommandType>(
command: Command,
type: T
): command is Command<T> => {
"worklet";
return command.type === type;
};
export interface GroupCommand extends Command<CommandType.Group> {
props?: Pick<DrawingNodeProps, "zIndex">;
children: Command[];
}
export const isGroup = (command: Command): command is GroupCommand => {
"worklet";
return command.type === CommandType.Group;
};
interface Props {
[]: ImageProps;
[]: CircleProps;
[]: CTMProps;
[]: DrawingNodeProps;
[]: BlurMaskFilterProps;
[]: PointsProps;
[]: PathProps;
[]: RectProps;
[]: RoundedRectProps;
[]: OvalProps;
[]: LineProps;
[]: PatchProps;
[]: VerticesProps;
[]: DiffRectProps;
[]: TextProps;
[]: TextPathProps;
[]: TextBlobProps;
[]: GlyphsProps;
[]: PictureProps;
[]: ImageSVGProps;
[]: ParagraphProps;
[]: AtlasProps;
[]: SkottieProps;
}
interface DrawCommand<T extends CommandType> extends Command<T> {
props: T extends keyof Props ? Props[T] : never;
}
export const isDrawCommand = <T extends keyof Props>(
command: Command,
type: T
): command is DrawCommand<T> => {
"worklet";
return command.type === type;
};