Skip to content

glContext()

ts
function glContext<T>(
  canvas: T,
  params?: WebGLContextAttributes & {
    colorSpace?: "srgb" | "display-p3";
  },
): {
  canvas: T extends OffscreenCanvas ? OffscreenCanvas : HTMLCanvasElement;
  gl: WebGL2RenderingContext;
  setSize: (width: number, height: number) => void;
};

Initializes a WebGL2 rendering context for a given canvas.

Type Parameters

T

T extends string | HTMLCanvasElement | OffscreenCanvas

Parameters

canvas

T

The canvas element to use or a CSS selector to query it.

params?

WebGLContextAttributes & { colorSpace?: "srgb" | "display-p3"; }

WebGL2 context attributes and color space configuration.

Returns

ts
{
  canvas: T extends OffscreenCanvas ? OffscreenCanvas : HTMLCanvasElement;
  gl: WebGL2RenderingContext;
  setSize: (width: number, height: number) => void;
}
NameType
canvasT extends OffscreenCanvas ? OffscreenCanvas : HTMLCanvasElement
glWebGL2RenderingContext
setSize()(width: number, height: number) => void

Throws

Error if the canvas or WebGL2 context could not be created.

Released under the MIT License.