react-three-fiber icon indicating copy to clipboard operation
react-three-fiber copied to clipboard

Draft: Canvas type improvements

Open kmannislands opened this issue 3 years ago • 3 comments
trafficstars

Improve typing on createRoot by declaring that it handles HTMLCanvasElement | OffscreenCanvas. This is more expressive than accepting any subclass of Element and allows some generic type params to be removed.

Two reasons this is a draft:

  • Though removing the generic type param from createRoot seems like a clear DX improvement, the function is part of the publicly documented API of r3f so this could be considered a small breaking change
  • To discuss approaches on how to source the OffscreenCanvas typing. Currently, I import it from the three definitely typed definitions but the def there seems non-ideal. I was a bit surprised that the type isn't defined in dom.lib.d.ts. Poking around, it seems that it is, however, defined in lib.webworker.d.ts meaning that it's possible to modify the tsconfig to include WebWorker. I wouldn't recommend this approach since generally the code in this repo is intended for the main thread so making worker-side types universally available is probably a bad idea. Open to suggestions here.

kmannislands avatar Aug 04 '22 20:08 kmannislands

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 056492fe47681dbf992db29a630e510bc3c7a5f6:

Sandbox Source
example Configuration

codesandbox-ci[bot] avatar Aug 04 '22 20:08 codesandbox-ci[bot]

Keen to hear your thoughts on typing OffscreenCanvas @joshuaellis (thanks for taking over the three.js typings btw)

kmannislands avatar Aug 04 '22 20:08 kmannislands

I'm looking to pick this PR into v9 (#2331), although the only withstanding issue would be with the question of how to source OffscreenCanvas. I'm very wary of relying on types' dependencies as it undermines our ability to follow semver, and we've already been bitten by this with WebXR which necessitates the major bump in v9.

CodyJasonBennett avatar Sep 15 '22 10:09 CodyJasonBennett

I coincidentally mirrored this in #2770 and #2774, so merging as complete. I'll relegate removing the redundant generics from RenderProps etc. to v9.

CodyJasonBennett avatar Feb 28 '23 05:02 CodyJasonBennett