license const contexts = { } ; const WEBGL_ATTRIBUTES = { alpha : false , antialias : false , premultipliedAlpha : false , preserveDrawingBuffer : false , depth : false , stencil : false , failIfMajorPerformanceCaveat : true } ; export function clearWebGLContext ( webGLVersion ) { delete contexts [ webGLVersion ] ; } export function setWebGLContext ( webGLVersion , gl ) { contexts [ webGLVersion ] = gl ; } export function getWebGLContext ( webGLVersion , customCanvas ? ) { if ( ! ( webGLVersion in contexts ) ) { const newCtx = getWebGLRenderingContext ( webGLVersion , customCanvas ) ; if ( newCtx !== null ) { contexts [ webGLVersion ] = newCtx ; } else { console . log ( 'Could not get context for WebGL version' , webGLVersion ) ; return null ; } } const gl = contexts [ webGLVersion ] ; if ( gl == null || gl . isContextLost ( ) ) { delete contexts [ webGLVersion ] ; return getWebGLContext ( webGLVersion ) ; } gl . disable ( gl . DEPTH_TEST ) ; gl . disable ( gl . STENCIL_TEST ) ; gl . disable ( gl . BLEND ) ; gl . disable ( gl . DITHER ) ; gl . disable ( gl . POLYGON_OFFSET_FILL ) ; gl . disable ( gl . SAMPLE_COVERAGE ) ; gl . enable ( gl . SCISSOR_TEST ) ; gl . enable ( gl . CULL_FACE ) ; gl . cullFace ( gl . BACK ) ; return contexts [ webGLVersion ] ; } function createCanvas ( webGLVersion ) { if ( typeof OffscreenCanvas !== 'undefined' && webGLVersion === 2 ) { return new OffscreenCanvas ( 300 , 150 ) ; } else if ( typeof document !== 'undefined' ) { return document . createElement ( 'canvas' ) ; } else { throw new Error ( 'Cannot create a canvas in this context' ) ; } } function getWebGLRenderingContext ( webGLVersion , customCanvas ? ) { if ( webGLVersion !== 1 && webGLVersion !== 2 ) { throw new Error ( 'Cannot get WebGL rendering context, WebGL is disabled.' ) ; } const canvas = customCanvas == null ? createCanvas ( webGLVersion ) : customCanvas ; canvas . addEventListener ( 'webglcontextlost' , ( ev ) => { ev . preventDefault ( ) ; delete contexts [ webGLVersion ] ; } , false ) ; if ( webGLVersion === 1 ) { return ( canvas . getContext ( 'webgl' , WEBGL_ATTRIBUTES ) || canvas . getContext ( 'experimental-webgl' , WEBGL_ATTRIBUTES ) ) as WebGLRenderingContext ; } return canvas . getContext ( 'webgl2' , WEBGL_ATTRIBUTES ) as WebGLRenderingContext ; }