New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation is unclear on how to "unmount" a canvas. #194
Comments
Thanks for the detailed description. This looks like a bug where the last frame is not stopped after |
So I looked into the source code for CanvasSpace and I think I might have zeroed in on the problem. Another problem that may happen is when we call Or... get this, I'm wrong and it's something else entirely :p Either way, let me know if you're open to contributions and I'd be happy to take a stab at this! |
The doc comment for
CanvasSpace#dispose
reads:However, I've found that simply calling
dispose()
is not enough to stop the CanvasSpace object's draw loop from being called.I'm using Pts.js in a Preact application, and for my use case I'd like to delete a CanvasSpace object as soon as my Preact component is unmounted. Of course, only the garbage collector can truly "delete" something, but I want to at least stop it from being referenced in places that causes the draw loop to be called. The code I wrote to do that was (simplified version):
However, I find that even after the clean-up function is run, and
space disposed
is logged on to the console, the space's draw loop continues to run. It throws an error (this._ctx is undefined
) since thecanvas
element no longer exists after the component has been unmounted.Either this is a possible bug, perhaps a stray callback to
requestAnimationFrame
somewhere in the source, or the documentation should be more clear about how to stop aCanvasSpace
from having any visible side effects after it has been disposed.Currently, my solution is to add a
space.pause()
before the call to dispose, so that the non-existentCanvasRenderingContext2D
is at least not referenced in a call to the space's draw loop.However, this just means that the space exists in memory, and its draw loop isn't being called.
Ideally, when a space has been disposed, the Garbage collector should be free to remove it from the heap.
The text was updated successfully, but these errors were encountered: