Sitemap
Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Member-only story

Improve JavaScript Performance with OffscreenCanvas

--

Learn how to improve the performance of rendering in canvas by using OffscreenCanvas

Press enter or click to view image in full size
Photo by Snapwire from Pexels

The OffscreenCanvas interface allows canvas rendering contexts (2D and WebGL) to be used in Workers. It increases parallelism in the web, leading to improved performance on multi-core systems.

Using OffscreenCanvas we can render a canvas in the worker thread so that the main thread will be undisturbed so that our web app will work smoothly.

The support of OffscreenCanvas across the browser can be found here CanIUse.

There are two ways we can use OffscreenCanvas

  1. Creating an OffscreenCanvas from existing canvas
  2. Creating an OffscreenCanvas manually and render that to the existing canvas

1. Creating an OffscreenCanvas from existing canvas

We can call the transferControlToOffscreen method on the canvas element to get an OffscreenCanvas element which can be transferred to the worker. Operations applied to OffscreenCanvas will be rendered on the source canvas automatically.

Example

--

--