微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过PaperJS消除无用点来加入连接的子路径?

如何解决如何通过PaperJS消除无用点来加入连接的子路径?

我有一条路径绘制了一个圆,其原点在“西”侧,然后通过移除顶部和底部进行拆分。然后我得到三个子路径:

  1. 左上角 1/4 圈
  2. 右半圆
  3. 左下角 1/4 圈

但即使在视觉上 1 和 3 看起来像一个翻转的 2,1 和 3 实际上是两个子路径。我如何优化这个?我试过smooth()、flatten() 和simple() 都不起作用。

这是sketch

解决方法

根据您的简化案例,您只需构建一个由所有子路径段组成的新路径。 为了稍微优化生成的路径,您可以跳过路径 B 的第一段,只保留其句柄,因为它与路径 A 的最后一段相同。 根据您的用例,您还可以使用相同的逻辑跳过路径 B 的最后一段,因为它与路径 A 的第一段相同,并确保结果路径设置为 closed

这是一个演示可能实现的 sketch

const compoundPath = project.importJSON(
    ['CompoundPath',{ 'applyMatrix': true,'children': [['Path','segments': [[50,700],[0,600],[50,600]] }],['Path',[100,700]] }]] }]
);
compoundPath.strokeColor = 'black';
project.activeLayer.addChild(compoundPath);

const subPaths = [];
compoundPath.children.forEach((child,i) => {
    subPaths.push(
        child
            .clone()
            .translate(0,150)
            .addTo(project.activeLayer)
    );
});

const assembledPath = assembleSubPaths(subPaths);
assembledPath.strokeColor = 'black';

function assembleSubPaths(subPaths) {
    const path = new Path();
    subPaths.forEach((subPath) => {
        subPath.segments.forEach((segment,segmentIndex) => {
            const isFirstSegment = segmentIndex === 0;
            if (path.segments.length === 0 || !isFirstSegment) {
                path.add(segment);
            } else {
                path.lastSegment.handleOut = segment.handleOut;
            }
        });
        subPath.remove();
    });
    return path;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。