---
url: /api/global/glContext/functions/glContext.md
---
# glContext()

```ts
function glContext<
  T extends string | HTMLCanvasElement | OffscreenCanvas,
>(params: {
  canvas: T;
  colorSpace?: "srgb" | "display-p3";
  alpha?: boolean;
  antialias?: boolean;
  depth?: boolean;
  desynchronized?: boolean;
  failIfMajorPerformanceCaveat?: boolean;
  powerPreference?: "default" | "high-performance" | "low-power";
  premultipliedAlpha?: boolean;
  preserveDrawingBuffer?: boolean;
  stencil?: boolean;
}): {
  gl: WebGL2RenderingContext;
  canvas: T extends OffscreenCanvas ? OffscreenCanvas : HTMLCanvasElement;
  setSize: (width: number, height: number) => void;
};
```

Initializes a WebGL2 rendering context for a given canvas.

## Type Parameters

### T

`T` *extends* `string` | `HTMLCanvasElement` | `OffscreenCanvas`

## Parameters

### params

Canvas and WebGL2 context configuration.

#### canvas

`T`

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

#### colorSpace?

`"srgb"` | `"display-p3"`

The color space to use for the drawing buffer.

#### alpha?

`boolean`

#### antialias?

`boolean`

#### depth?

`boolean`

#### desynchronized?

`boolean`

#### failIfMajorPerformanceCaveat?

`boolean`

#### powerPreference?

`"default"` | `"high-performance"` | `"low-power"`

#### premultipliedAlpha?

`boolean`

#### preserveDrawingBuffer?

`boolean`

#### stencil?

`boolean`

## Returns

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

| Name        | Type                                                                      |
| ----------- | ------------------------------------------------------------------------- |
| `gl`        | `WebGL2RenderingContext`                                                  |
| `canvas`    | `T` *extends* `OffscreenCanvas` ? `OffscreenCanvas` : `HTMLCanvasElement` |
| `setSize()` | (`width`: `number`, `height`: `number`) => `void`                         |

## Throws

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