Options
All
  • Public
  • Public/Protected
  • All
Menu

The rendering context, tied to a canvas element.

example

Most of the time, you won't need to manually create a context.

The defaultContext is, as the name suggests, the context that is used, unless another one is manually specified. It still needs to be initialized:

import { defaultContext, Sampler } from '@gdgt/webgl';


const canvas = document.createElement( 'canvas' );
document.body.appendChild( canvas );

defaultContext.initialize( canvas );

// All objects created without a specified context are tied to defaultContext:
const sampler = new Sampler();
// is equivalent to
const sampler = new Sampler({ context: defaultContext });

// You can also access the underlying WebGL2RenderingContext.
// This will resolve once defaultContext has been initialized.
const gl = await defaultContext.getGlContext();

If you'd rather manage your own context, you can do so by providing each object with your context upon instantiation:

import { Context, Sampler } from '@gdgt/webgl';


const canvas = document.createElement( 'canvas' );
document.body.appendChild( canvas );

const myContext = new Context();
myContext.initialize( canvas );

const sampler = new Sampler({ context: myContext });

Remember to do so on every object you create – using objects across multiple contexts is currently not supported.

Hierarchy

  • Context

Index

Constructors

Methods

  • Ties the context to a given canvas element. This can only be called once and needs to be called, before any rendering can take place.

    Parameters

    • canvas: HTMLCanvasElement

      The canvas element to render to

    • options: WebGLContextAttributes = {}

      Additional settings for context creation, passed to the canvas element. See WebGLContextAttributes. { alpha: true } is used unless explicitly overwritten. Note that { alpha: false } can lead to major performance issues on some platforms.

    Returns void

  • getAstcProfiles(): Promise<string[]>

Generated using TypeDoc