Skip to content

Instantly share code, notes, and snippets.

@Jagathishrex
Last active January 2, 2021 15:32
Show Gist options
  • Save Jagathishrex/78f73dd9a725b3edf65c187afba89c03 to your computer and use it in GitHub Desktop.
Save Jagathishrex/78f73dd9a725b3edf65c187afba89c03 to your computer and use it in GitHub Desktop.
let canvas;
let ctx;
function animate() {
// do animations here ...
const bitmap = canvas.transferToImageBitmap();
self.postMessage({msg: 'render', bitmap}); // this will be handled in main thread , where we will update the content
self.requestAnimationFrame(animate);
}
self.onmessage = function(ev) {
if(ev.data.msg === 'offscreen') {
canvas = ev.data.canvas;
ctx = canvas.getContext('2d');
animate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment