@shopify/react-native-skia
Version:
High-performance React Native Graphics using Skia
42 lines (36 loc) • 1.29 kB
text/typescript
import type { CanvasKit, Surface } from "canvaskit-wasm";
import type { SurfaceFactory } from "../types";
import { Host } from "./Host";
import { JsiSkSurface } from "./JsiSkSurface";
export class JsiSkSurfaceFactory extends Host implements SurfaceFactory {
constructor(CanvasKit: CanvasKit) {
super(CanvasKit);
}
Make(width: number, height: number) {
return new JsiSkSurface(
this.CanvasKit,
this.CanvasKit.MakeSurface(width, height)!
);
}
MakeOffscreen(width: number, height: number) {
// OffscreenCanvas may be unvailable in some environments.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const OC = (globalThis as any).OffscreenCanvas;
let surface: Surface | null;
if (OC === undefined) {
return this.Make(width, height);
} else {
const offscreen = new OC(width, height);
const webglContext = this.CanvasKit.GetWebGLContext(offscreen);
const grContext = this.CanvasKit.MakeWebGLContext(webglContext);
if (!grContext) {
throw new Error("Could not make a graphics context");
}
surface = this.CanvasKit.MakeRenderTarget(grContext, width, height);
}
if (!surface) {
return null;
}
return new JsiSkSurface(this.CanvasKit, surface);
}
}