LottieFiles / dotlottie-web

Official LottieFiles player for rendering Lottie and dotLottie animations in the web. Supports React, Vue, Svelte, SolidJS and Web Components.
https://developers.lottiefiles.com/docs/dotlottie-player/
MIT License
191 stars 11 forks source link

Resizing in DotLottieWorker leads to JS error - Cannot resize canvas after call to transferControlToOffscreen() #321

Closed lkaisers closed 3 months ago

lkaisers commented 3 months ago

Overview

I experience an issue when using the resize method with the DotLottieWorker instead of DotLottie. Uncaught (in promise) DOMException: Failed to set the 'width' property on 'HTMLCanvasElement': Cannot resize canvas after call to transferControlToOffscreen().

Consuming repo

https://www.npmjs.com/package/@lottiefiles/dotlottie-web/v/0.30.2

Labels

theashraf commented 3 months ago

Hi @lkaisers, It would be really helpful if you could provide a CodePen or CodeSandbox example demonstrating the issue.

lkaisers commented 3 months ago

Hi @theashraf, of course, here you go: https://codepen.io/lkaisers-the-typescripter/pen/vYqdJpo

You may have to open the browser console to see the logged issue. But you can also see that resizing doesn't work if you load the page in a narrow window and increase the width so it should be resized, but the canvas appears blurry because the canvas isn't updated.

I also used ChatGPT to find out what's going wrong and its answer makes sense for me. https://chatgpt.com/share/e5ee62fd-a3c8-4c18-b056-0473c0bf3325 Following this answer, the issue arises "because the main thread is trying to resize a canvas that has been transferred to a worker thread". This would be a bug in DotLottieWorker because the resize method needs to work a bit differently to make the worker do the job, but I'm not that experienced with web workers to clearly say what's going wrong here.

theashraf commented 3 months ago

@lkaisers I've released a fix for this issue. Please test out dotlottie-web@v0.31.0. Thanks!!

lkaisers commented 3 months ago

@theashraf I just tested everything and the resizing using DotLottieWorker works as expected! Thank you very much, big improvement for our site performance! 😄