How to add support for browser features like WebGL in TypeScript?

var canvas = <HTMLCanvasElement>document.getElementById('canvas1'); var gl = canvas.getContext('webgl'); canvas.width = 400; canvas.height = 400; gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); 

The "HTMLCanvasElement" seems to offer only 2d canvas methods. This will not allow me to compile gl.viewport (), etc. How to add this support for WebGL?

+6
source share
1 answer

You need to define WebGL methods before using them. You can find text written in WebGL written by the community here . Add WebGL.d.ts to your project and name it like this:

 ///<reference path="WebGL.d.ts" /> var canvas = <any>document.getElementById('canvas1'); var gl = <WebGLRenderingContext> canvas.getContext('webgl'); canvas.width = 400; canvas.height = 400; gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); 
+10
source

Source: https://habr.com/ru/post/927161/


All Articles