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

基于Paper.js实现不规则形状Metaball效果的技术求助

问题:使用Paper.js为不规则形状实现可导出矢量的Metaball效果

我正尝试用Paper.js给不规则形状做Metaball效果,但Paper.js官方示例只针对圆形,我数学能力有限,写不出计算形状间融合逻辑的函数,不确定这个效果是否可行。

我知道可以用SVG滤镜实现Metaball,但这会把效果光栅化,导出的SVG没法兼容Figma、Adobe Illustrator这类矢量软件,所以这个方案不能用。

目前我已经做了一个Demo:可以移动形状,当形状上的点距离达到阈值时,会用直线连接它们。核心逻辑在drawConnections函数里,Paper.js的Segment相关文档可参考官方说明。Demo的结构包含init()、onSvgLoad(item)等函数,其中drawConnections的代码如下:

function drawConnections(segments, threshold = 40) {
  for (const segment of segments) {
    for (const otherSegment of segments) {
      if (segment === otherSegment) continue;

      for (const seg of segment) {
        for (const otherSeg of otherSegment) {
          if (seg.point.getDistance(otherSeg.point) < threshold) {
            
            const path = drawLine([seg.point, otherSeg.point], 4);
            path.sendToBack();
            path.removeOn({ drag: true, up: true });
            
          }
        }
      }
    }
  }
}

另外我还做了两个额外Demo:

  • 限制每个segment只能建立一个连接的版本
  • 用SVG滤镜实现Metaball效果的版本

恳请各位提供帮助。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 17:23:13