Skip to content

Commit 88d964d

Browse files
committed
Expand getContext variants
1 parent c314fab commit 88d964d

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

src/CanvasAPI.res

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ type canvasFillRule =
112112
| @as("evenodd") Evenodd
113113
| @as("nonzero") Nonzero
114114

115+
type webGLPowerPreference =
116+
| @as("default") Default
117+
| @as("high-performance") HighPerformance
118+
| @as("low-power") LowPower
119+
115120
/**
116121
[See OffscreenCanvas on MDN](https://developer.mozilla.org/docs/Web/API/OffscreenCanvas)
117122
*/
@@ -425,3 +430,17 @@ type canvasRenderingContext2DSettings = {
425430
mutable colorSpace?: predefinedColorSpace,
426431
mutable willReadFrequently?: bool,
427432
}
433+
434+
type webGLContextAttributes = {
435+
mutable alpha?: bool,
436+
mutable depth?: bool,
437+
mutable stencil?: bool,
438+
mutable antialias?: bool,
439+
mutable premultipliedAlpha?: bool,
440+
mutable preserveDrawingBuffer?: bool,
441+
mutable powerPreference?: webGLPowerPreference,
442+
mutable failIfMajorPerformanceCaveat?: bool,
443+
mutable desynchronized?: bool,
444+
}
445+
446+
type imageBitmapRenderingContextSettings = {mutable alpha?: bool}

src/DOMAPI/HTMLCanvasElement.res

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -701,10 +701,10 @@ Creates a CanvasRenderingContext2D object representing a two-dimensional renderi
701701
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext#2d)
702702
*/
703703
@send
704-
external getContext2D: (
704+
external getContext_2D: (
705705
htmlCanvasElement,
706706
@as("2d") _,
707-
~options: canvasRenderingContext2D=?,
707+
~options: canvasRenderingContext2DSettings=?,
708708
) => canvasRenderingContext2D = "getContext"
709709

710710
/**
@@ -713,35 +713,35 @@ Returns an object that provides methods and properties for drawing and manipulat
713713
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLCanvasElement/getContext)
714714
*/
715715
@send
716-
external getContext2: (
716+
external getContext_WebGL: (
717717
htmlCanvasElement,
718-
~contextId: string,
719-
~options: JSON.t=?,
720-
) => imageBitmapRenderingContext = "getContext"
718+
@as("webgl") _,
719+
~options: webGLContextAttributes=?,
720+
) => webGLRenderingContext = "getContext"
721721

722722
/**
723723
Returns an object that provides methods and properties for drawing and manipulating images and graphics on a canvas element in a document. A context object includes information about colors, line widths, fonts, and other graphic parameters that can be drawn on a canvas.
724724
@param contextId The identifier (ID) of the type of canvas to create. Internet Explorer 9 and Internet Explorer 10 support only a 2-D context using canvas.getContext("2d"); IE11 Preview also supports 3-D or WebGL context using canvas.getContext("experimental-webgl");
725725
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLCanvasElement/getContext)
726726
*/
727727
@send
728-
external getContext3: (
728+
external getContext_WebGL2: (
729729
htmlCanvasElement,
730-
~contextId: string,
731-
~options: JSON.t=?,
732-
) => webGLRenderingContext = "getContext"
730+
@as("webgl2") _,
731+
~options: webGLContextAttributes=?,
732+
) => webGL2RenderingContext = "getContext"
733733

734734
/**
735735
Returns an object that provides methods and properties for drawing and manipulating images and graphics on a canvas element in a document. A context object includes information about colors, line widths, fonts, and other graphic parameters that can be drawn on a canvas.
736736
@param contextId The identifier (ID) of the type of canvas to create. Internet Explorer 9 and Internet Explorer 10 support only a 2-D context using canvas.getContext("2d"); IE11 Preview also supports 3-D or WebGL context using canvas.getContext("experimental-webgl");
737737
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLCanvasElement/getContext)
738738
*/
739739
@send
740-
external getContext4: (
740+
external getContext_BitmapRenderer: (
741741
htmlCanvasElement,
742-
~contextId: string,
743-
~options: JSON.t=?,
744-
) => webGL2RenderingContext = "getContext"
742+
@as("bitmaprenderer") _,
743+
~options: imageBitmapRenderingContextSettings=?,
744+
) => imageBitmapRenderingContext = "getContext"
745745

746746
/**
747747
Returns the content of the current canvas as an image that you can use as a source for another canvas or an HTML element.

tests/DOMAPI/HTMLCanvasInputElement__tes.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ open WebAPI.Global
22

33
let myCanvas: DOMAPI.htmlCanvasElement =
44
document->Document.getElementById("myCanvas")->Prelude.unsafeConversation
5-
let ctx = myCanvas->HTMLCanvasElement.getContext2D
5+
let ctx = myCanvas->HTMLCanvasElement.getContext_2D
66

77
ctx.fillStyle = Prelude.unsafeConversation("red")
88
ctx->CanvasRenderingContext2D.fillRect(~x=50., ~y=50., ~w=200., ~h=200.)

tools/TypeScript-DOM-lib-generator/src/build/emitter.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@ export async function emitRescriptBindings(webidl: Browser.WebIdl) {
17781778
});
17791779
}
17801780

1781-
const interfaceHierarchy: RescriptFile[] = [
1781+
let interfaceHierarchy: RescriptFile[] = [
17821782
{
17831783
name: "Prelude",
17841784
entries: [
@@ -2820,6 +2820,17 @@ export async function emitRescriptBindings(webidl: Browser.WebIdl) {
28202820
},
28212821
];
28222822

2823+
interfaceHierarchy = [
2824+
{
2825+
name: "Temp",
2826+
entries: [
2827+
enums(["WebGLPowerPreference"]),
2828+
dictionaries(["ImageBitmapRenderingContextSettings", "WebGLContextAttributes"]),
2829+
],
2830+
opens: [],
2831+
}
2832+
]
2833+
28232834
// Ensure the output folder exists.
28242835
await fs.mkdir(outputFolder, { recursive: true });
28252836

@@ -2911,7 +2922,7 @@ export async function emitRescriptBindings(webidl: Browser.WebIdl) {
29112922

29122923
await emitGlobalModule();
29132924

2914-
execSync("npx rescript format -all", { cwd: repoRoot, stdio: "inherit" });
2925+
// execSync("npx rescript format -all", { cwd: repoRoot, stdio: "inherit" });
29152926

29162927
// let remainers = allInterfaces.filter((i) => {
29172928
// return !interfaceHierarchy.some((h) => {

0 commit comments

Comments
 (0)