Jump to content

HTMLCanvasElement.<vendor>RequestPointerLock optional?


olrehm
 Share

Recommended Posts

Hi,

I am using BabylonJS from Typescript, and ran into the following problem:

      const canvas = $element.find('canvas')[0] as HTMLCanvasElement;
      const engine = new BABYLON.Engine(canvas, true);

Fails to compile with the message

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

My interpretation of this is that HTMLCanvasElement as defined in lib.d.ts does not have this property, and that I am casting to that definition of HTMLCanvasElement, while BABYLON.Engine expects the one defined in babylon.d.ts, which does have this property. The problem is that BabylonJS does not export its own mixin version of HTMLCanvasElement, so I cannot even cast to that to pass it into BABYLON.Engine. So I am left with casting to any:

      const canvas = $element.find('canvas')[0] as any;
      const engine = new BABYLON.Engine(canvas, true);

Which works but is somewhat ugly. I wonder if it would not be possible and better for babylons mixin to be defined like this:

interface HTMLCanvasElement {
    requestPointerLock(): void;
    msRequestPointerLock?(): void;
    mozRequestPointerLock?(): void;
    webkitRequestPointerLock?(): void;
}

Anyways the calling code seems to be trying through all vendor prefixes, so marking these optional sounds like it would actually more accurately describe the situation - and it would mean that the type is compatible with libxml.d.ts' version.

Any concerns? Should I send a pull request?

Cheers

Ole

Link to comment
Share on other sites

I know that you do - that is exactly the problem:

You have a mixin which is not compatible with lib.d.ts and which you do not expose but still use for parameters in your API. So callers cannot cast their HTML elements to your mixin - and then their calling code fails to compile.

That is why I suggest to either make your mixin compatible to lib.d.ts by making the things you add optional - I think this would be accurate in this case since you really do not know if these properties exist, and you do have code checking that before relying on it.

The other option is to export your mixin. But that is the worse solution in my opinion.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...