代码之家  ›  专栏  ›  技术社区  ›  unloco

缺少svg textpath字符

  •  0
  • unloco  · 技术社区  · 7 年前

    我试图显示一个用javascript生成的曲线文本 当我增加曲率时,一些字符会消失

    这里A JSFiddle 我创造了

    这是我的函数,它根据曲线百分比和文本大小生成路径数据

    public getPathData (width, height, curve): string {
      const positive_curve = (curve > 0);
      const perimeter = width / Math.abs(curve) * 100;
      const radius = perimeter / (2 * Math.PI) + (positive_curve ? 0 : height);
      const sign = positive_curve ? 1 : 1;
      const side = positive_curve ? 1 : 0;
      const diameter_parameter = sign * (2 * radius);
      return `m0,${radius} 
              a${radius},${radius} 0 0 ${side} ${diameter_parameter},0
              a${radius},${radius} 0 0 ${side} ${-diameter_parameter},0Z`;
      }
    

    这是浏览器问题吗?或者问题就在于道路本身?

    基于@paul lebeau answer更新的代码

    public getPathData (width, height, curve): string {
      const perimeter = width / Math.abs(curve) * 100;
      const radius = perimeter / (2 * Math.PI);
      const diameter = radius * 2;
      if (curve > 0) {
        return `m${radius},${diameter} 
                a${radius},${radius} 0 0 1 0 ${diameter}
                a${radius},${radius} 0 0 1 0 ${diameter}Z`;
      } else {
        return `m${radius},${diameter}  
                a${radius},${radius} 0 0 0 0 ${diameter}
                a${radius},${radius} 0 0 0 0 ${-diameter}Z`;
      }
    }
    

    它基本上基于曲线百分比将文本环绕在圆的内部或外部[-100%,100%]

    1 回复  |  直到 7 年前
        1
  •  2
  •   Paul LeBeau    7 年前

    <svg viewBox="-270 -270 1290 1280" width="257" height="256">
      <path id="curve" fill="white" stroke="red" stroke-width="1px" 
      d="m0,400 
        a400,400 0 0 1 800,0
        a400,400 0 0 1 -800,0Z">
      </path>
      <circle cx="0" cy="400" r="20"/>
      
      <text font-size="300" font-family="'Arial'" fill="#ff0000" x="0" y="0" text-anchor="middle">
        <textPath href="#curve" startOffset="25%">This is a new test</textPath>
      </text>
    </svg>

    <svg viewBox="-270 -270 1290 1280" width="257" height="256">
      <path id="curve" fill="white" stroke="red" stroke-width="1px" 
      d="m400,800 
        a400,400 0 0 1 0,-800
        a400,400 0 0 1 0,800Z">
      </path>
      <circle cx="400" cy="800" r="20"/>
      
      <text font-size="300" font-family="'Arial'" fill="#ff0000" x="0" y="0" text-anchor="middle">
        <textPath href="#curve" startOffset="50%">This is a new test</textPath>
      </text>
    </svg>