-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📋 CanvasKit Compatibility Layer (#495)
- Loading branch information
1 parent
7fae154
commit d55f839
Showing
22 changed files
with
1,033 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
diff --git a/node_modules/canvaskit-wasm/types/index.d.ts b/node_modules/canvaskit-wasm/types/index.d.ts | ||
index 3abbcc3..c578760 100644 | ||
--- a/node_modules/canvaskit-wasm/types/index.d.ts | ||
+++ b/node_modules/canvaskit-wasm/types/index.d.ts | ||
@@ -1,5 +1,5 @@ | ||
// Minimum TypeScript Version: 3.7 | ||
-export function CanvasKitInit(opts: CanvasKitInitOptions): Promise<CanvasKit>; | ||
+export default function CanvasKitInit(opts?: CanvasKitInitOptions): Promise<CanvasKit>; | ||
|
||
export interface CanvasKitInitOptions { | ||
/** | ||
@@ -1983,7 +1983,7 @@ export interface Paint extends EmbindObject<Paint> { | ||
* Sets the current color filter, replacing the existing one if there was one. | ||
* @param filter | ||
*/ | ||
- setColorFilter(filter: ColorFilter): void; | ||
+ setColorFilter(filter: ColorFilter | null): void; | ||
|
||
/** | ||
* Sets the color used when stroking and filling. The color values are interpreted as being in | ||
@@ -1997,25 +1997,25 @@ export interface Paint extends EmbindObject<Paint> { | ||
* Sets the current image filter, replacing the existing one if there was one. | ||
* @param filter | ||
*/ | ||
- setImageFilter(filter: ImageFilter): void; | ||
+ setImageFilter(filter: ImageFilter | null): void; | ||
|
||
/** | ||
* Sets the current mask filter, replacing the existing one if there was one. | ||
* @param filter | ||
*/ | ||
- setMaskFilter(filter: MaskFilter): void; | ||
+ setMaskFilter(filter: MaskFilter | null): void; | ||
|
||
/** | ||
* Sets the current path effect, replacing the existing one if there was one. | ||
* @param effect | ||
*/ | ||
- setPathEffect(effect: PathEffect): void; | ||
+ setPathEffect(effect: PathEffect | null): void; | ||
|
||
/** | ||
* Sets the current shader, replacing the existing one if there was one. | ||
* @param shader | ||
*/ | ||
- setShader(shader: Shader): void; | ||
+ setShader(shader: Shader | null): void; | ||
|
||
/** | ||
* Sets the geometry drawn at the beginning and end of strokes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
import CanvasKitInit from "canvaskit-wasm"; | ||
|
||
import { JsiSkApi } from "../web"; | ||
|
||
//import type { SkiaApi } from "../SkiaApi"; | ||
|
||
let Skia: ReturnType<typeof JsiSkApi>; | ||
|
||
beforeAll(async () => { | ||
const CanvasKit = await CanvasKitInit(); | ||
Skia = JsiSkApi(CanvasKit); | ||
}); | ||
|
||
describe("Draw a rectangle", () => { | ||
it("Check that CanvasKit and CanvasKit are loaded", async () => { | ||
expect(Skia).toBeDefined(); | ||
}); | ||
it("Draws a lightblue rectange", () => { | ||
const paint = Skia.Paint(); | ||
paint.setColor(Skia.Color("lightblue")); | ||
const rct = Skia.XYWHRect(64, 64, 128, 128); | ||
const surface = Skia.Surface.Make(256, 256); | ||
expect(surface).toBeDefined(); | ||
if (!surface) { | ||
return; | ||
} | ||
const canvas = surface.getCanvas(); | ||
canvas.drawRect(rct, paint); | ||
surface.ref.flush(); | ||
const image = surface.makeImageSnapshot(); | ||
const png = image.encodeToBytes(); | ||
const ref = fs.readFileSync( | ||
path.resolve(__dirname, "snapshots/lightblue-rect.png") | ||
); | ||
expect(ref.equals(png)).toBe(true); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* eslint-disable no-nested-ternary */ | ||
import type { CanvasKit, EmbindEnumEntity } from "canvaskit-wasm"; | ||
|
||
import type { SkJSIInstance } from "../../types"; | ||
|
||
export abstract class Host { | ||
readonly CanvasKit: CanvasKit; | ||
|
||
constructor(CanvasKit: CanvasKit) { | ||
this.CanvasKit = CanvasKit; | ||
} | ||
} | ||
|
||
export abstract class HostObject<T, N extends string> | ||
extends Host | ||
implements SkJSIInstance<N> | ||
{ | ||
readonly __typename__: N; | ||
readonly ref: T; | ||
|
||
constructor(CanvasKit: CanvasKit, ref: T, typename: N) { | ||
super(CanvasKit); | ||
this.ref = ref; | ||
this.__typename__ = typename; | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
export type NonNullish = {}; | ||
|
||
export const toOptionalValue = <T>( | ||
value: NonNullish | undefined | null | ||
): T | undefined | null => | ||
value === undefined ? undefined : value === null ? null : toValue(value); | ||
|
||
export const toUndefinedableValue = <T>( | ||
value: NonNullish | undefined | ||
): T | undefined => (value === undefined ? undefined : toValue(value)); | ||
|
||
export const toNullableValue = <T>(value: NonNullish | null): T | null => | ||
value === null ? null : toValue(value); | ||
|
||
export const toValue = <T>(value: NonNullish): T => | ||
(value as HostObject<T, string>).ref; | ||
|
||
export const ckEnum = (value: number): EmbindEnumEntity => ({ value }); | ||
export const optEnum = ( | ||
value: number | undefined | ||
): EmbindEnumEntity | undefined => | ||
value === undefined ? undefined : { value }; |
Oops, something went wrong.