jonobr1 / two.js

A renderer agnostic two-dimensional drawing api for the web.
https://two.js.org
MIT License
8.3k stars 455 forks source link

[Question] How to completely destroy instances of TwoJS ? #734

Open duping123456 opened 2 months ago

duping123456 commented 2 months ago

I have a Vue project that references this JS. In one of the pages, four graphics drawn by TwoJS were rendered. When I re rendered the page, the four TwoJs were not destroyed and were regenerated. This caused the memory to constantly increase every time I switched data and re rendered the component, until the webpage crashed

jonobr1 commented 2 months ago

Ah yes, on unmount you'll want to do this:

const index = Two.Instances.indexOf(two);
const parent = two.renderer.domElement.parentElement;
if (index >= 0) Two.Instances.splice(index, 1);
if (parent) parent.removeChild(two.renderer.domElement);
two.release(two.scene);

Assuming your instance of Two.js is called two.