瀏覽代碼

Make private HTMLCanvasElement mixin compatible with the one in lib.d.ts.

Babylon.js has a mixin for HTMLCanvasElement to add vendor-prefixed requestPointerLock methods. The problem is that it does not expose this mixin, so it is not possible from Typescript to provide this except by either using any, or by created the same mixin one self and keeping the definitions in sync. This Typescript code:

    const canvas = document.getElementById("main-canvas") as HTMLCanvasElement;
    const engine = new BABYLON.Engine(canvas, true);

Will currently fail with:

    error TS2345: Argument of type 'HTMLCanvasElement' is not assignable to parameter of type 'HTMLCanvasElement'.
    Property 'msRequestPointerLock' is missing in type 'HTMLCanvasElement'.

This is because the HTMLCanvasElement in the first line is the one provided by lib.d.ts which does not have the vendor-prefixed methods, but the one expected in the second line is the private mixin BABYLON uses.

The solution implemented in this commit is to mark the vendor-prefixed methods optional, which is appropriate because indeed it will depend on the browser implementation if they are there, and indeed Babylon.js already checks if they are set: https://github.com/BabylonJS/Babylon.js/search?utf8=%E2%9C%93&q=msRequestPointerLock

This has the pleasant side-effect that it makes the private HTMLCanvasElement mixin compatible with the one from lib.d.ts, and as a result, the code above compiles.
Ole Rehmsen 9 年之前
父節點
當前提交
58291ef5a8
共有 1 個文件被更改,包括 3 次插入3 次删除
  1. 3 3
      src/babylon.mixins.ts

+ 3 - 3
src/babylon.mixins.ts

@@ -48,9 +48,9 @@ interface Document {
 
 
 interface HTMLCanvasElement {
 interface HTMLCanvasElement {
     requestPointerLock(): void;
     requestPointerLock(): void;
-    msRequestPointerLock(): void;
-    mozRequestPointerLock(): void;
-    webkitRequestPointerLock(): void;
+    msRequestPointerLock?(): void;
+    mozRequestPointerLock?(): void;
+    webkitRequestPointerLock?(): void;
 }
 }
 
 
 interface CanvasRenderingContext2D {
 interface CanvasRenderingContext2D {