• 关于js计算非等宽字体宽度的方法


    准备一个容器

    首先在body外插入一个absolute的容器避免重绘:

    
    const svgWidthTestContainer = document.createElement('svg');
    svgWidthTestContainer.setAttribute('id', 'svgWidthTest');
    
    svgWidthTestContainer.style.cssText = `
      position: absolute;
       500px;
      height: 500px;
      left: -1000px;
      top: -1000px;
      visibility: 'hidden';
    `;
    document.body.appendChild(svgWidthTestContainer);
    

    计算方法

    总结出了两种方法,这里由于我使用的是svg,其他元素同理。下面先说性能最好的一个方法,先创建所有的text元素,然后统一append到准备好的容器里。
    代码如下:

    
    export function getSvgsWidth(texts) {
      // 这里使用div不用fragment主要是不方便删除
      const textsFragment = document.createElement('g');
      const textElements = texts.map((text) => {
        const textElement = document.createElement('text');
        textElement.textContent = text;
        textsFragment.appendChild(textElement);
        return textElement;
      });
      svgWidthTestContainer.appendChild(textsFragment);
      const textElementsWidth = textElements.map(element => element.getBoundingClientRect().width);
      svgWidthTestContainer.removeChild(textsFragment);
      return textElementsWidth;
    }
    // 得到1-1000000数字在屏幕上的宽度
    console.log(getSvgsWidth([...Array(100000).keys()]));
    

    还有一个方法(不推荐)就是事先准备好一个text,然后每次替换里面的textContent返回宽度,代码如下:

    
    // 准备好text
    const textElementTest = document.createElement('text');
    
    svgWidthTestContainer.appendChild(textElementTest);
    
    export function getSvgsWidthWithOneText(texts) {
      const textElementsWidth = texts.map((text) => {
        textElementTest.textContent = text;
        return textElementTest.getBoundingClientRect().width;
      });
      return textElementsWidth;
    }
    
    
    
    // 可以做一个性能测试,我这边算出来他俩一直保持着5倍左右的差距
    const dateStart = new Date().getTime();
    console.log(getSvgsWidth([...Array(100000).keys()]));
    console.log(getSvgsWidthWithOneText([...Array(100000).keys()]));
    
    console.log(new Date().getTime() - dateStart);
    

    来源:https://segmentfault.com/a/1190000017551345

  • 相关阅读:
    FW : Unit of Measure related settings in SAP
    SAP PCA: 转移价格的确定
    SAP 关于贸易伙伴(Trading Partner)区分关联方/非关联方/子公司/第三方
    FSV tables in S/4 HANA OB58 , S_E38_98000088
    SAP Profit Center Table Data
    服务器搭建网站完整教程(宝塔面板+wordpress)
    运营
    undefined reference to `std::cout'等错误
    [c++][chromium]C++做与不做 C++ Dos and Don'ts
    [git]快速迁移git仓库
  • 原文地址:https://www.cnblogs.com/datiangou/p/10192593.html
Copyright © 2020-2023  润新知