如何居中打印()'d拉斐尔文本在一个空间

how to center print()'d raphael text in a space

本文关键字:文本 空间 一个 打印 何居中      更新时间:2023-09-26

我很好奇如何正确地为print()'d文本提供所需的渲染空间,或者如何让Raphael从中心向外渲染文本

var an = Raphael("about-navlink", 69, 22), fonts = [0, an.getFont("arialbold")], about_navlink = an.print(0, 10, "about.", fonts[1], 22).attr({fill: "#000000"});

使文本从左侧开始。我想让这段代码的部分与PHP动态或选择一个静态的宽度,其中的中心和放弃计算。我已经看了raphael的文档,我没有在print()参考中找到任何讨论文本对齐的内容。任何帮助都将非常感激。谢谢!

在Raphael渲染了对应于文本的路径后,我们设为printed:

  • 使用Raphael.pathBBox(printed.attr('path')) (doc),计算负偏移量(-width/2和-height/2)

  • var m = Raphael.matrix()创建一个矩阵,您可以用您的偏移量(m.translate(left, top))进行翻译,或者直接在矩阵构造函数参数中放置偏移值(左和上分别为第5和第6个参数)

  • 那么你有两种方法应用它:

    1. 使用Raphael.mapPath明确应用矩阵(它计算一个路径字符串,你必须设置:

      printed.attr(
          'path',
          Raphael.mapPath(printed.attr('path'), m)
      );
      
    2. 或者像使用一个简单的变换:

      printed.transform(
          m.toTransformString()
      );
      

      如果在这个元素上有其他的转换,使用

      '...' + m.toTransformString()
      

      让你的转换发生在已有的转换之前,

      m.toTransformString() + '...'