Skip to content

Instantly share code, notes, and snippets.

Created March 12, 2022 11:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
declare module 'glslCanvas' {
export type DefaultUniforms = 'u_mouse' | 'u_resolution' | 'u_time' | 'u_tex'
export interface TestResult {
wasValid: boolean
frag?: string
vert?: string
timeElapsedMs: number
export type TextureData = object
export type UniformTypes = string | boolean | number | number[]
export interface Uniforms {
[name: string]: UniformTypes | Uniforms
export interface Mouse {
x: number
y: number
export interface ConstructorOptions {
fragmentString: string
vertexString?: string
backgroundColor: 'rgba(1,1,1,0)' | string
[key: string]: unknown
export type LoadTextureOptions = object
export type UniformMethods = `${1 | 2 | 3 | 4}${'f' | 'i'}${'v' | ''}` | `${2 | 3 | 4}fv`
export type UniformGlslTypes =
| 'float'
| 'int'
| 'bool'
| 'vec2'
| 'vec3'
| 'vec4'
| 'ivec2'
| 'ivec3'
| 'ivec4'
| 'bvec2'
| 'bvec3'
| 'bvec4'
| 'mat2'
| 'mat3'
| 'mat4'
| 'sampler2D'
| string
export type FragmentBuffers<T extends string> = Record<`u_buffer${number}`, {fragment: `${string}${T}`}>
class GlslCanvas {
width: number
height: number
canvas: HTMLCanvasElement
gl: WebGLRenderingContext
program?: unknown
textures: object
buffers: unknown
uniforms: Uniforms
vbo: object
isValid: boolean
vertexString: string
timeLoad: number
timeDelta: number
forceRender: boolean
paused: boolean
realToCSSPixels: number
constructor(canvas: HTMLCanvasElement, contextOptions?: Partial<ConstructorOptions>, options?: object)
setUniform(name: string, ...value: UniformTypes[]): void
load(fragmentShader: string, vertexShader?: string): void
destroy(): void
test(callback: (result: TestResult) => void, fragString?: string, vertString?: string): void
name: string,
urlElementOrData: string | TextureData,
options?: Partial<LoadTextureOptions>,
): void
refreshUniforms(): void
setUniforms(uniforms: Uniforms): void
setMouse(mouse: Mouse): void
uniform(method: UniformMethods, type: UniformGlslTypes, name: string, ...value: UniformTypes[]): void
uniformTexture(name: string, texture: unknown, options?: object): void
resize(): void
render(): void
play(): void
pause(): void
renderPrograms(): void
getBuffers<T extends string>(fragString: T): FragmentBuffers<T>
loadPrograms(buffers: FragmentBuffers<string>): void
createSwappableBuffer(W: GLsizei, H: GLsizei, program: unknown): object
W: GLsizei,
H: GLsizei,
): {
index: number
texture: unknown
buffer: unknown
W: GLsizei
H: GLsizei
resize: (W: GLsizei, H: GLsizei) => void
resizeSwappableBuffers(): void
version(): `${number}.${number}.${number}`
export default GlslCanvas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment