You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Paper.js获取路径相交节点及手柄位置?非闭合路径适用吗?

Paper.js提取路径相交后的节点及手柄坐标方案

核心操作步骤

  1. 获取路径交点集合:调用Path.getIntersections()方法,传入目标路径,即可得到两条路径的所有精确交点对象。
  2. 拆分路径为子段:以每个交点为拆分点,使用Path.split()方法将原路径拆分为独立的子路径段。
  3. 提取节点与手柄坐标:从拆分后的子路径的segments属性中,直接读取节点的绝对坐标;手柄坐标默认是相对节点的偏移值,与节点坐标相加即可得到绝对坐标。

代码示例

// 假设已定义两条开放路径pathA、pathB
const intersections = pathA.getIntersections(pathB);

// 如需保留原路径,建议先克隆再操作
const pathACopy = pathA.clone();
const pathBCopy = pathB.clone();

// 遍历所有交点处理路径
intersections.forEach(intersection => {
  // 拆分路径得到子路径
  const splitPathA = pathACopy.split(intersection);
  const splitPathB = pathBCopy.split(intersection);

  // 提取splitPathA的节点及手柄信息
  splitPathA.segments.forEach(seg => {
    const node = { x: seg.point.x, y: seg.point.y };
    const handleIn = { x: seg.handleIn.x + node.x, y: seg.handleIn.y + node.y };
    const handleOut = { x: seg.handleOut.x + node.x, y: seg.handleOut.y + node.y };

    // 此处可将数据存入自定义结构,供后续处理使用
    // 例如:result.push({ node, handleIn, handleOut });
  });

  // 同理提取splitPathB的节点信息
  splitPathB.segments.forEach(seg => {
    // 重复上述坐标提取逻辑
  });
});

非闭合路径兼容性说明

该方法完全适用于非闭合路径。Paper.js的getIntersections和split方法对开放路径、闭合路径均提供完整支持,拆分后的非闭合子路径会完整保留所有节点的位置及手柄数据,不会因路径未闭合丢失信息。

内容的提问来源于stack exchange,提问作者Philipp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 23:35:21