简体   繁体   English

销毁创建的未添加到 DOM 的 HTMLElement

[英]Destroy created HTMLElement which was not added to DOM

Currently I'm working on a game using pixi.js.目前我正在使用 pixi.js 开发游戏。 I implemented utility functions for eg wrapping lines or measure the text length of the longest line to use on a HTMLCanvasElement .我实现了实用功能,例如换行或测量要在HTMLCanvasElement上使用的最长行的文本长度。 Here's an example of the getLongestWidth :这是getLongestWidth的示例:

static getLongestWidth(lines: string[]): number {
  const canvas = document.createElement('canvas');
  const context = canvas.getContext('2d');
  const longestLine = lines.reduce((a, b) => a.length > b.length ? a : b, '');
  return context.measureText(longestLine).width;
}

I want to keep the 'core' canvas functions independet ( context.measureText in this example) so you can even use them without providing a CanvasRenderingContext2D everytime.我想保持“核心” canvas 函数独立(在本例中为context.measureText ),因此您甚至可以在不提供CanvasRenderingContext2D的情况下使用它们。

Since performance is a thing I wonder whether/how I should destroy the created canvas.由于性能是一个问题,我想知道是否/如何销毁创建的 canvas。 I'm guessing that after the function has finished at least the reference pointing to it will be destroyed and objects without reference will be garbage collected?我猜在 function 完成后,至少指向它的引用将被销毁,并且没有引用的对象将被垃圾收集? But the createElement part throws me off.但是 createElement 部分让我失望。 I tried document.removeChild(canvas) which of course fails since it was never actively added to the DOM but I'm not sure what's happening in the background.我尝试document.removeChild(canvas) ,这当然失败了,因为它从未主动添加到 DOM 中,但我不确定后台发生了什么。 Couldn't find anything in the docs or with Google and I'm kinda afraid of thousands orphan HTMLCanvasElements floating around in the background.在文档或谷歌中找不到任何东西,我有点害怕成千上万的孤HTMLCanvasElements在后台漂浮。

I'm sorry if this a super basic question:) Suggestions for improvements are welcome.如果这是一个超级基本的问题,我很抱歉:) 欢迎提出改进建议。

Recycle the same object?回收同样的object?

class test {
    static getLongestWidth(lines) {
      const context = this.ctx;
      const longestLine = lines.slice().sort((a,b)=>a.length - b.length)[0];
      return context.measureText(longestLine).width;
    }
}
test.ctx = document.createElement("canvas").getContext("2d");
test.getLongestWidth(["aa","b"]); //5.5615234375

There is no way to actively remove an object from memory.无法从 memory 中主动删除 object。 GC will collect it when there are no references left for that object.当没有 object 的引用时,GC 将收集它。

So you can dereference the object by setting, canvas = null;因此,您可以通过设置取消引用 object, canvas = null; . .

You can also use a static class object to keep track of the canvas and delete it when done.您还可以使用 static class object 来跟踪 ZFCC790C72A86190DE1B549D0DDC65 完成后将其删除。 It's technically the same thing as dereferencing.从技术上讲,这与取消引用相同。 ref 参考

class Test {
    private static test: any = 'test';
    public static testFunc() {
        console.log(`deleting: ${Test.test}`);
        delete Test.test;
        console.log(`done deleting: ${Test.test}`);
    }
}

Test.testFunc();

In all cases, the GC will collect it from memory in the next cycle.在所有情况下,GC 都会在下一个循环中从 memory 收集它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM