Skip to content

使用脚本制作“横”笔画组件

这个示例教程介绍如何使用脚本制作一个简单的笔画“横”,并设置长度与字重两个基础可调参数。

新建字形

将右下角Tab切换到“字形”一栏,点击新建按钮,输入“横”,新建字形。点击“横”字形,进入编辑面板。

添加参数

点击工具栏黑色控制台按钮,打开参数与脚本页面,在左侧参数栏切换至“字形参数”Tab,新建两个参数。分别将参数名设置为“长度”和“字重”。将“长度”设置为500,“字重”设置为40。

添加脚本

在右侧脚本界面的脚本函数中,输入如下代码:

js
const length = glyph.getParam('长度')
const weight = glyph.getParam('字重')
const ox = 500
const oy = 500
const start = new FP.Joint(
  'start',
  {
    x: ox - length / 2,
    y: oy,
  },
)
const end = new FP.Joint(
  'end',
  {
    x: ox + length / 2,
    y: oy,
  },
)
const refline = (p1, p2) => {
  return {
    name: `${p1.name}-${p2.name}`,
    start: p1.name,
    end: p2.name,
  }
}
glyph.addJoint(start)
glyph.addJoint(end)
glyph.addRefLine(refline(start, end))

const rect = new FP.RectangleComponent(start.x, start.y - weight / 2, length, weight)
glyph.addComponent(rect)

预览效果

点击运行按钮,回到字形编辑面板,可以看到一个简单的“横”被绘制出来,可以调整参数以预览效果。