news 2026/9/17 9:35:30

tsParticles 粒子描边(Stroke)配置迁移指南:从 particles.stroke 到 particles.paint.stroke

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
tsParticles 粒子描边(Stroke)配置迁移指南:从 particles.stroke 到 particles.paint.stroke

tsParticles 粒子描边(Stroke)配置迁移指南:从 particles.stroke 到 particles.paint.stroke

【免费下载链接】tsparticlestsParticles - Easily create highly customizable JavaScript particles effects, confetti explosions and fireworks animations and use them as animated backgrounds for your website. Ready to use components available for React.js, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact, Inferno, Solid, Riot and Web Components.项目地址: https://gitcode.com/GitHub_Trending/ts/tsparticles

导读

本指南围绕 tsParticles 中粒子描边(Stroke)配置的官方文档展开,说明描边选项为何从旧路径particles.stroke迁移至particles.paint.stroke,并结合引擎源码(engine/src)深入讲解paint分组下stroke的完整参数、默认值与渲染原理。读完本文,你将掌握在 JSON 配置或tsParticles.load()代码中正确书写粒子描边(宽度、颜色、透明度),并理解其与paint.fillpaint.color的协作关系。

背景:为什么 Stroke 选项被归入paint分组

在 Stroke.md 中明确指出,Stroke 选项已被归档到 Particles Paint 文档中,新的配置路径为particles.paint.stroke。这是 tsParticles 将粒子「填充」与「描边」两类绘制选项统一收拢到paint命名空间的设计调整:

  • 旧路径:particles.stroke
  • 新路径:particles.paint.stroke

从源码可以印证这一分组意图。IPaint.ts 注释写明paint是“grouping fill and stroke so variants can be selected together”,即让填充与描边可以作为同一套变体(variant)被整体选择;Paint.ts 类则通过懒加载方式持有fillstroke两个子选项:

loadLazyProperty(this, "fill", data.fill, () => new Fill()); loadLazyProperty(this, "stroke", data.stroke, () => new Stroke());

paint分组结构与颜色回退规则

Paint.md 给出的paint属性表如下:

KeyTypeNotes
colorIColor默认填充/描边颜色回退值(fallback)
fillIColorFill粒子内部颜色选项
strokeSingleOrMultiple描边(轮廓)选项,对应IStroke

关键回退规则:paint.color是 fill 和 stroke 的基准颜色。当paint.fill.colorpaint.stroke.color未设置时,引擎使用paint.color;一旦两者自身设置了color,则优先于paint.color

默认值在 ParticlesOptions.ts 中初始化,保持历史行为——粒子默认白色且填充:

this.paint.color = new AnimatableColor(); this.paint.color.value = "#fff"; this.paint.fill = new Fill(); this.paint.fill.enable = true;

即:

  • paint.color.value默认"#fff"
  • paint.fill.enable默认true

paint.stroke完整参数解析

描边选项对应接口 IStroke.ts,其实现类 Stroke.ts 包含三个字段:

字段类型默认值说明
colorIAnimatableColor \| IRangeColor继承paint.color描边颜色,支持颜色动画(IAnimatableColor)或随机范围色(IRangeColor
opacityRangeValue未设置(可选)描边透明度,可为静态数值或{ min, max }范围对象
widthRangeValue0描边线宽,同样支持范围值,用于在粒子之间随机取宽

其中widthopacity都经由loadRangeProperty加载(见 Stroke.ts),因此二者既可以写死为单个数字,也可以写成{ min, max }让每个粒子在范围内随机取值——这与 tsParticles 一贯的RangeValue约定一致。color则通过AnimatableColor.create创建,说明描边颜色天然支持valueanimation(如enablespeedsync)的动画配置。

需要注意width的默认值是0,这意味着若不配置描边,粒子不会有可见轮廓;描边只在显式设置width > 0时生效。这一点在渲染端也有印证:RenderManager.ts 中只有当strokeWidth大于最小线宽时才设置strokeStyle,并在绘制阶段对带描边的粒子调用context.stroke()(RenderManager.ts)。

配置示例:从迁移前后到完整用法

迁移前(旧路径,已废弃)

{ "particles": { "stroke": { "width": 2, "color": { "value": "#ffffff" } } } }

迁移后(新路径)

Paint.md 中的官方描边示例:

{ "paint": { "color": { "value": "#14b8a6" }, "stroke": { "width": 2, "color": { "value": "#ffffff" } } } }

进阶:范围宽度、透明度与描边颜色动画

结合IStroke的字段能力,可以写出更丰富的描边配置:

{ "particles": { "paint": { "color": { "value": "#14b8a6" }, "stroke": { "width": { "min": 1, "max": 4 }, "opacity": 0.8, "color": { "value": "#ffffff", "animation": { "enable": true, "speed": 1, "sync": false } } } } } }

在 JS/TS 中通过tsParticles.load()使用同样结构:

await tsParticles.load({ id: "tsparticles", options: { particles: { paint: { color: { value: "#14b8a6" }, stroke: { width: 2, color: { value: "#ffffff" }, }, }, }, }, });

paint.fill的协作:只描边不填充

描边与填充是互补关系:fill.enabletrue时粒子内部被填充,为false时仅保留描边轮廓(见 Fill.md)。若想绘制“空心”粒子,可组合配置:

{ "particles": { "paint": { "color": { "value": "#14b8a6" }, "fill": { "enable": false }, "stroke": { "width": 2, "color": { "value": "#ffffff" } } } } }

渲染端对此的分工清晰:填充色与描边色都来自paint体系,RenderManager.ts 通过 updater 的getColorStyles同时取得fillstroke样式;粒子绘制时先fill()再按线宽stroke(),二者共用同一套颜色回退逻辑。

相关文档索引

  • 描边主文档:Paint.md
  • 填充选项:Fill.md
  • 颜色模型(IColorIAnimatableColorIRangeColor):Color.md
  • 粒子选项根文档:Particles.md
  • 选项总览:Options.md

【免费下载链接】tsparticlestsParticles - Easily create highly customizable JavaScript particles effects, confetti explosions and fireworks animations and use them as animated backgrounds for your website. Ready to use components available for React.js, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact, Inferno, Solid, Riot and Web Components.项目地址: https://gitcode.com/GitHub_Trending/ts/tsparticles

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/17 9:33:10

把Scratch改造成游戏引擎:三个月实战改造指南

“Scratch变成游戏引擎?说实话,三个月前我自己也不信。”这个想法起源于一次暑期班的课后复盘——孩子们用积木搭出来的小游戏,每次重开都要手动复位角色、重置变量、重新播放背景音乐,玩起来就像没有导演的舞台剧。我当时随口说了…

作者头像 李华
网站建设 2026/9/17 9:31:11

Spring Boot文件上传下载实战与优化策略

1. 文件传输在现代Web应用中的核心地位文件上传与下载功能看似基础,实则是现代Web应用中最高频使用的功能模块之一。从社交媒体平台的图片分享到企业OA系统的文档流转,从在线教育平台的课件分发到医疗系统的影像传输,文件交互能力直接影响着用…

作者头像 李华
网站建设 2026/9/17 9:31:07

Ubuntu / WSL 安装pipx uv 管理项目

一、pipx管理工具 1.1 安装 pipx 在 Ubuntu / WSL 上可以用两种方式安装,推荐第二种(官方脚本)或第三种(pip 安装最新版本并自动配置 PATH)。 1.1.1、apt 安装(最快,但版本往往偏旧&#xff…

作者头像 李华
网站建设 2026/9/17 9:30:21

把Scratch逼成游戏引擎?三个月实战复盘:性能优化与架构设计

经常有人问我,都2025年了,为什么还有人要把Scratch逼成游戏引擎?不瞒你说,我一度也回答不上来。但当我真正动手,把一款正经的空战射击游戏塞进这个“教小孩拖积木”的工具里,还稳定跑在接近60帧时&#xff…

作者头像 李华