问题
I'm new to web development, so this might be a dumb question. Please let me know if it's already answered:)
According to MDN web docs' page for OffscreenCanvas, currently, the API only supports WebGL context only. (Maybe it just means the worker support for OffscreenCanvas' 2d context is not implemented, but we can still use it on the main thread?)
Note: This API is currently implemented for WebGL1 and WebGL2 contexts only. See bug 801176 for Canvas 2D API support from workers.
But when I read the HTML Specs for OffscreenCanvas, the 2d context is supported.
enum OffscreenRenderingContextType { "2d", "webgl" };
I suspect the second one is only a proposal for what OffscreenCanvas should support, and the first is what is actually supported. Is my understanding correct? If so, how could I know whether a specific browser supports the 2d context for OffscreenCanvas, like Chromium?
What makes me even more confused is people called the regular canvas an offscreenCanvas, and get a 2d context from it (example1, example2). But I think in this case, they are just referring a regular canvas which is not attached to DOM, other than the OffscreenCanvas I like to research on.
回答1:
I suspect the second one is only a proposal for what
OffscreenCanvasshould support, and the first is what is actually supported. Is my understanding correct?
Correct, the WHATWG spec is the standard that browsers should support. The reason Firefox's bug #801176 exists is because Firefox falls short of the requirements of the WHATWG specification in this area.
What makes me even more confused is people called the regular
canvasanoffscreenCanvas
These are simply variables called offscreenCanvas which hold a normal canvas. You can name your variables anything; these authors chose a descriptive name for a canvas that is not currently rendered on the screen.
回答2:
Update
It looks like OffscreenCanvas will ship in Chrome 69 or Chrome 70 with support for both 2D and WebGL. As for Images, you can use fetch and ImageBitmap inside a worker to download images.
You can see how to use OffscreenCanvas on MDN.
Previous answer
What's the state of OffscreenCanvas's support for 2d context?
The state is first off that OffscreenCanvas support has not shipped in Firefox or Chrome by default as of 2018/04/30 in Chrome 66 nor Firefox 59. It's not currently turned on in Chrome Canary 68 either nor Firefox nightly and therefore unlikely to be enabled in the next few months.
My own tests show that if you enable experimental support WebGL works in Chrome but not Firefox and neither supports 2D contexts what-so-ever. 2D contexts are more problematic since the spec has to change. For example ctx.drawImage(someImage, ...) makes no sense for an OffscreenCanvas since an OffscreenCanvas is really meant to be used in a worker but there are no Images in workers since those are DOM objects. WebGL partly gets around this issue because there are ways to update textures to WebGL that don't involve Images.
The entire question is really one of speculation. If you want to know when Firefox or Chrome or Safari or Edge is going to ship OffscreenCanvas and with 2d support the best you can do is dig through their bug trackers. I believe the spec may change as if you search the net you can find there are still discussions on how OffscreenCanvas should work and cover cases like WebVR and others that it doesn't currently cover well.
来源:https://stackoverflow.com/questions/46800673/whats-the-state-of-offscreencanvass-support-for-2d-context