UNPKG

@tolokoban/tgd

Version:

ToloGameDev library for WebGL2

130 lines 4.08 kB
import { TgdCamera } from "../camera"; import { TgdInputs } from "../input"; import { TgdPainterGroup } from "../painter/group"; import { TgdAnimation } from "../types/animation"; import { TgdEvent } from "../event"; /** * You can pass all the attributes of the [WebGL2ContextAttributes](https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext) * object. * @see {@link TgdContext} */ export type TgdContextOptions = WebGLContextAttributes & { /** * You can override the behaviour for when a resize event occurs, * by providing a callback `onResize(...)`. * * By default, this is what will happen: * ``` * gl.canvas.width = width * gl.canvas.height = height * gl.viewport(0, 0, width, height) * ``` */ onResize?(this: void, context: TgdContext, width: number, height: number): void; name?: string; camera?: TgdCamera; enableTextureFloatStorage?: boolean; /** * Size of a pixel. If under 1, you will start to have pixelated render. * Defaut to 1. */ resolution?: number; }; /** * This class gives you a [WebGL2RenderingContext](https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext) for a given canvas, * through its public readonly attribute `gl`. * * It also takes care of the canvas resizing and the animation frames. * * @example * ``` * import { TgdContext, TgdPainterClear } from "@tgd" * * export function paint(canvas: HTMLCanvasElement) { * const ctx = new TgdContext(canvas) * const clear = new TgdPainterClear(ctx, { color: [1, 0.667, 0, 1] }) * ctx.add(clear) * ctx.paint() * } * ``` */ export declare class TgdContext extends TgdPainterGroup { readonly canvas: HTMLCanvasElement; private readonly options; private static incrementalId; readonly name: string; readonly gl: WebGL2RenderingContext; readonly inputs: TgdInputs; readonly implementationColorReadFormat: number; readonly implementationColorReadType: number; readonly eventPaint: TgdEvent<TgdContext>; resolution: number; private _camera; private _fps; private _aspectRatio; private _aspectRatioInverse; private readonly observer; private paintingIsOngoing; private paintingIsQueued; private isPlaying; private requestAnimationFrame; private lastTimeInSec; private timeShift; private pauseTime; private pauseAccumulation; private readonly animationManager; /** * @param canvas The canvas to which attach a WebGL2 context. * @see {@link TgdContextOptions} */ constructor(canvas: HTMLCanvasElement, options?: TgdContextOptions); get fps(): number; get time(): number; get camera(): TgdCamera; set camera(camera: TgdCamera); /** * Check if the last WebGL command has returned an error. */ checkError(caption: string, action?: () => void): void; animSchedule(...animations: TgdAnimation[]): TgdAnimation[]; animCancel(animation: TgdAnimation): void; get width(): number; get height(): number; get aspectRatio(): number; get aspectRatioInverse(): number; /** * Is the animation playing? */ get playing(): boolean; /** * If `playing` is true, the method `paint()` will be called * for every animation frame. * @see paint() */ set playing(value: boolean); /** * Start the animation. * You can achieve the same result with `context.playing = true`. * * @see playing */ play(): void; /** * Pause the animation. * You can achieve the same result with `context.playing = false`. * * @see playing */ pause(): void; takeSnapshotToCanvas(target: HTMLCanvasElement): void; lookupWebglConstant(value: number): string; /** * Trigger the painters to render the scene. */ readonly paint: () => void; private now; private readonly actualPaint; delete(): void; stateReset(): WebGL2RenderingContext; } //# sourceMappingURL=context.d.ts.map