Echarts with OffscreenCanvas
-
OffscreenCanvas: https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
-
Original issue: apache/echarts#9232
-
Supported browser: https://caniuse.com/#feat=offscreencanvas
- A canvas cannot be used directly in worker, but OffscreenCanvas can be used as a handle to control it.
- All rendering is done in worker.
Echartshas pretty well worker support, with a few exceptions. Patched below. - Mouse events are bind on the canvas in UI thread. When triggered, necessary event data is sent into worker using
postMessage, then dispatches the event to echarts instance using its internal API. See ecomfe/zrender#448 (comment) - All methods are dispatched into worker using
postMessage, and return a promise immediately. After the method is called in worker, send its return value back to UI thread usingpostMessage, and then resolve the promise. See https://github.com/CarterLi/ThreadPool.js - All Echarts events are bound in worker, and send event data back to UI thread when triggered. In UI thread,
DocumentFragmentis used as an event bus.
- Add
&& !env.workerto disable hoverLayerThreshold in worker. See apache/echarts#13164 - Modify
SaveAsImage.prototype.onclickto let it supportOffscreenCanvas - Optional: Remove UMD sh*t
- Optional: Modify
tooltip.initto let it accept a customizedTooltipContentimplementation
See patch.diff
See Github Page: https://carterli.github.io/echarts-with-offscreencanvas/
Support Chrome 80+ ( due to using of module scripts on worker ). Fallback version support Safari & Firefox 67 ( because older FireFox versions and Edge lack dynamic import support )