or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

attributes-uniforms.mdbuffer-management.mdcontext-creation.mdextensions.mdframebuffer-operations.mdindex.mdshader-programs.mdtexture-operations.mdwebgl-rendering.md
tile.json

context-creation.mddocs/

Context Creation and Management

Context creation and management functionality for creating WebGL rendering contexts with configurable attributes and automatic environment detection.

Capabilities

Context Creation Function

Creates a WebGL rendering context with specified dimensions and optional attributes.

/**
 * Create a WebGL rendering context
 * @param width - Width of the drawing buffer (must be > 0)
 * @param height - Height of the drawing buffer (must be > 0)
 * @param options - Optional context attributes
 * @returns WebGLRenderingContext or null if creation fails
 */
function createContext(width, height, options);

Usage Examples:

const gl = require('gl');

// Basic context creation
const context = gl(800, 600);

// Context with specific attributes
const context = gl(800, 600, {
  alpha: false,           // No alpha channel
  depth: true,            // Enable depth buffer
  stencil: true,          // Enable stencil buffer
  preserveDrawingBuffer: true  // Keep buffer after present
});

// Check if context creation succeeded
if (!context) {
  console.error('Failed to create WebGL context');
  return;
}

Context Attributes

Configuration options for WebGL context creation.

interface ContextAttributes {
  /** Enable alpha channel in color buffer (default: true) */
  alpha?: boolean;
  /** Enable depth buffer (default: true) */
  depth?: boolean;
  /** Enable stencil buffer (default: false) */
  stencil?: boolean;
  /** Enable antialiasing (default: false, overridden to false) */
  antialias?: boolean;
  /** Enable premultiplied alpha (default: true) */
  premultipliedAlpha?: boolean;
  /** Preserve drawing buffer contents (default: false) */
  preserveDrawingBuffer?: boolean;
  /** Prefer low power GPU (default: false) */
  preferLowPowerToHighPerformance?: boolean;
  /** Fail if major performance caveat (default: false) */
  failIfMajorPerformanceCaveat?: boolean;
}

Context Properties

Properties available on created WebGL contexts.

interface WebGLRenderingContext {
  /** Current width of the drawing buffer */
  readonly drawingBufferWidth: number;
  /** Current height of the drawing buffer */
  readonly drawingBufferHeight: number;
}

Context State Management

Methods for managing context state and lifecycle.

/**
 * Get the current context attributes
 * @returns Context attributes object
 */
getContextAttributes(): ContextAttributes;

/**
 * Check if the context is lost (always returns false in headless-gl)
 * @returns false
 */
isContextLost(): boolean;

/**
 * Force execution of queued WebGL commands
 */
flush(): void;

/**
 * Wait for completion of all queued WebGL commands
 */
finish(): void;

Usage Examples:

// Check context attributes
const attrs = context.getContextAttributes();
console.log('Alpha enabled:', attrs.alpha);
console.log('Depth buffer:', attrs.depth);

// Get buffer dimensions
console.log('Buffer size:', context.drawingBufferWidth, 'x', context.drawingBufferHeight);

// Ensure commands are executed
context.flush();  // Force execution
context.finish(); // Wait for completion

Environment Detection

The module automatically detects the runtime environment and uses the appropriate WebGL implementation.

Node.js Environment:

  • Uses native OpenGL implementation via ANGLE
  • Full WebGL 1.0.3 conformance
  • Hardware accelerated when available

Browser Environment:

  • Falls back to standard WebGL context from HTML5 canvas
  • Maintains API compatibility
  • Adds custom extensions for compatibility
// Automatic environment detection - no configuration needed
const gl = require('gl');
const context = gl(800, 600);

// Same API works in both environments
context.clearColor(0, 0, 0, 1);
context.clear(context.COLOR_BUFFER_BIT);