KhronosGroup / KhronosGroup/WebGL
Introduce an asynchronous variant of canvas.getContext()
- Dominant language
- HTML
- Stars
- 2.9k
- Forks
- 703
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 4
Description
This was proposed on the WebGL call today, and seemed to have broad support. To be clear, any introduction of new API surface will need to happen in the [main HTML spec](https://html.spec.whatwg.org/multipage/canvas.html#htmlcanvaselement) but we would benefit from discussing the API shape we'd like to see here prior to making an official proposal for the main spec.
The primary intent of introducing an asynchronous version of `getContext()` is simply to allow the page to not block during context creation, which can be a lengthy process under the best of circumstances and may be made significantly longer if the context creation criteria requires the UA to create the context on a specific adapter, such as is the case with `powerPreference` or the `xrCompatible` flag introduced by WebXR.
It seems like the most straightforward variant of this (just make it return a `Promise`) should be sufficient, and as such I suspect the biggest point of discussion may simply be bikeshedding the name.
```webidl
partial interface HTMLCanvasElement {
Promise getContextAsync(DOMString contextId, optional any options = null);
}
```
`getContextAsync()` is about as simple as names come in this case, but if there's other JS APIs that have both a synchronous and asynchronous variant we should look at what the prior art is in terms of naming. The only one that I'm aware of off the top of my head is `XMLHttpRequest`, but in that case sync/async was actually an option passed to the API. (Also, uh... I don't know of anyone that would consider `XMLHttpRequest` an example of Web API naming done well. There's a reason its replacement was simply called `fetch`.)
Implementation wise, we'll need to be careful to ensure that the asynchronous nature of the API doesn't cause any weirdness when paired with the expectation that `getContext()` returns the same object every time you pass the previously created context ID type. Otherwise I can't see many stumbling blocks.
Contributor guide
Research direction
Start by reviewing the proposed WebIDL for HTMLCanvasElement and the linked canvas section of the main HTML spec. Compare asynchronous naming prior art such as XMLHttpRequest and fetch, then resolve the API shape and same-context behavior well enough to support an official proposal.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- html
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100