echarts-with-offscreencanvas
echarts-with-offscreencanvas copied to clipboard
Echarts with OffscreenCanvas
OffscreenEcharts
Echarts with OffscreenCanvas
-
Echarts: https://github.com/apache/incubator-echarts
-
OffscreenCanvas: https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
-
Original issue: https://github.com/apache/incubator-echarts/issues/9232
-
Supported browser: https://caniuse.com/#feat=offscreencanvas
-
Ref: https://segmentfault.com/a/1190000012563475
How does it work
- 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 https://github.com/ecomfe/zrender/pull/448#issuecomment-692451904 - 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.
Modify Echarts source code
- Add
&& !env.workerto disable hoverLayerThreshold in worker. See https://github.com/apache/incubator-echarts/issues/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
Known issues
Demo
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 )