kopiro / siriwave

The Apple® Siri wave-form replicated in a JS library.
http://kopiro.github.io/siriwave/
MIT License
1.63k stars 168 forks source link

Idea: dispose() method #54

Closed semmel closed 3 years ago

semmel commented 3 years ago

In it's constructor SiriWave mutates the DOM by appending a <canvas> child to the target container DOM element. Would it not be nice to have some dispose() method (i.e. something like a destructor) which reverses the effects of the constructor?

Basically such a method just would have to

dispose() {
   this.stop();
   this.canvas.remove();
   // But also: what if this instance is (wrongly) called again by the user?
   // ... throw on next start() ?
}

My use case: I find myself destroying and creating new SiriWaves when my app's layout (i.e. the container element size) changes. So that width and height of the canvas are properly reset for the line animation. However unused canvas elements (owned by probably already garbage-collected SiriWaves) break my layout.

kopiro commented 3 years ago

Nice idea! I'll try to implement it tomorrow!

On Sat, 12 Dec 2020 at 23:47 Matthias Seemann notifications@github.com wrote:

In it's constructor SiriWave mutates the DOM by appending a child to the target container DOM element. Would it not be nice to have some dispose() method (i.e. something like a destructor) which reverses the effects of the constructor?

Basically such a method just would have to

dispose() { this.stop(); this.canvas.remove(); // But also: what if this instance is (wrongly) called again by the user? // ... throw on next start() ?}

My use case: I find myself destroying and creating new SiriWaves when my app's layout (i.e. the container element size) changes. So that width and height of the canvas are properly reset for the line animation. However unused canvas elements (owned by probably already garbage-collected SiriWaves) break my layout.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kopiro/siriwave/issues/54, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGNAFCJP2MGGTVE5QEPTQTSUPXIPANCNFSM4UYX35OA .

kopiro commented 3 years ago

@semmel what do you think?

ikupenov commented 3 years ago

Was just looking for this, thanks @kopiro !