ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

tsParticles @tsparticles/all 全功能 Bundle 详解:loadAll 工作机制与多框架接入指南

tsParticles @tsparticles/all 全功能 Bundle 详解:loadAll 工作机制与多框架接入指南 tsParticles tsparticles/all 全功能 Bundle 详解loadAll 工作机制与多框架接入指南【免费下载链接】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/tsparticlestsparticles/all是 tsParticles 生态中的“全量功能包”它通过一个loadAll(engine)调用把引擎tsparticles/engine之外的全部特效Effects、交互Interactions、运动路径Paths、插件Plugins、形状Shapes和更新器Updaters一次性注册到 tsParticles 实例中。本文基于仓库中该包的 README 与源码实现完整讲解它包含哪些能力、loadAll底层的注册流程以及在 CDN/原生 JS、React、Vue、Angular、Svelte 等环境下的具体接入方式帮助你在“不想逐个安装插件包”的场景下快速搭起功能完备的粒子系统。一、Bundle 定位一个依赖覆盖全部引擎扩展tsParticles 采用插件化架构核心运行时是 engine 目录对应的tsparticles/engine而每一种形状、路径、交互、特效都以独立的 npm 包形式提供各自暴露一个load*函数供注册。tsparticles/all的角色就是把这套“插件森林”聚合起来——官方文档将其描述为the tsParticles all bundle loads all the features to atsparticles/engineinstance见 bundles/all/README.md。从当前仓库版本来看该包版本为4.3.3见 bundles/all/package.json其description明确声明它聚合了 engine 包、插件、交互、预设、形状、更新器、特效、路径、发射器、音效与调色板。1.1 包含的包清单按类别README 的 Included Packages 部分列出了聚合范围这里按其源码加载顺序归类整理便于按需对照完整列表以 bundles/all/README.md 为准类别包含的包前缀tsparticles/源码入口目录基础全量包tsparticles即 full bundle及其全部依赖、enginebundles/full特效 Effectseffect-bubble、effect-filter、effect-particles、effect-shadow、effect-traileffects交互 Interactionsinteraction-external-particle、interaction-external-cannon、interaction-external-pop、interaction-light、interaction-particles-repulseinteractions路径 Pathspath-branches、path-brownian、path-curl-noise、path-curves、path-fractal-noise、path-grid、path-levy、path-perlin-noise、path-polygon、path-random、path-simplex-noise、path-spiral、path-svg、path-zig-zagpaths颜色插件plugin-hsv-color、plugin-hwb-color、plugin-lab-color、plugin-lch-color、plugin-named-color、plugin-oklab-color、plugin-oklch-colorplugins/colors缓动插件plugin-easing-back、-bounce、-circ、-cubic、-elastic、-expo、-gaussian、-linear、-quart、-quint、-sigmoid、-sine、-smoothstepplugins/easings掩膜插件plugin-background-mask、plugin-canvas-mask、plugin-polygon-maskplugins/backgroundMask、plugins/canvasMask、plugins/polygonMask发射器形状plugin-emitters-shape-canvas、plugin-emitters-shape-path、plugin-emitters-shape-polygonplugins/emittersShapes导出插件plugin-export-image、plugin-export-json、plugin-export-videoplugins/exports其他插件plugin-infection、plugin-manual-particles、plugin-motion、plugin-poisson-disc、plugin-responsive、plugin-sounds、plugin-themes、plugin-trail、plugin-zoomplugins形状 Shapesshape-arrow、shape-cards、shape-cog、shape-heart、shape-infinity、shape-matrix、shape-path、shape-rounded-polygon、shape-rounded-rect、shape-spiral、shape-squircleshapes更新器 Updatersupdater-gradient、updater-orbitupdaters一个值得注意的细节从源码结构看实际聚合范围比 README 列表更大。bundles/all/src/index.ts 中还加载了loadGifShapetsparticles/shape-gif对应 shapes/gif与loadRibbonShapetsparticles/shape-ribbon对应 shapes/ribbon且 bundles/all/package.json 的dependencies中同样列出了这两个包——如果你在配置shape: gif或 ribbon 相关选项它们已经包含在内无需单独安装。1.2 依赖关系图README 用一张 Mermaid 图表达了聚合层次tsparticles/all直接依赖 full bundletsparticles、engine以及 Effects、Interactions、Paths、Plugins、Shapes、Updaters 六大分组完整的逐包节点版本保留在 bundles/all/README.md 的 Dependency Graph 小节中。二、loadAll 的底层实现注册顺序与并行加载理解loadAll的实现就能明白“必须先调用它再load”这一约束的由来。2.1 注册流程核心实现位于 bundles/all/src/index.tsexport async function loadAll(engine: Engine): Promisevoid { engine.checkVersion(__VERSION__); await engine.pluginManager.register(async e { const loadInteractionsForAll async (e: Engine): Promisevoid { await loadFull(e); // 先注册 full bundle 的全部基础插件 await Promise.all([ loadExternalCannonInteraction(e), loadExternalParticleInteraction(e), loadExternalPopInteraction(e), loadLightInteraction(e), loadParticlesRepulseInteraction(e), // ... infection、emitters 形状、14 种 path ]); }; await Promise.all([ loadInteractionsForAll(e), loadHsvColorPlugin(e), loadEasingBackPlugin(e), // ... 其余颜色、缓动、掩膜、导出、特效、形状、更新器 ]); }); }从源码结构看它有三个关键设计点版本校验engine.checkVersion(__VERSION__)index.ts L92在注册前校验引擎与插件包的版本一致性__VERSION__是构建期由 rollup.config.js 注入的全局常量。延迟真正注册engine.pluginManager.register(...)注册的是一个回调真正的插件加载发生在引擎初始化阶段。这意味着loadAll(engine)本身只是“登记”这也是它必须出现在tsParticles.load(...)之前框架组件中则是init/particlesInit回调中的原因。两级并行所有load*调用通过Promise.all并发执行其中loadFull来自 bundles/full 的 full bundle被刻意放在loadInteractionsForAll内先行await因为它提供的是其他扩展交互、特效等所依赖的基础形状/更新器等能力——从调用链看full 是“地基”all 在其上叠加高级能力。2.2 普通版与 lazy 版package.json的exports字段暴露了两个子路径见 bundles/all/package.json.默认入口对应 src/index.ts静态 import 全部插件./lazy对应 src/index.lazy.ts注册回调内改用import(tsparticles/xxx/lazy)动态导入再统一执行加载如 index.lazy.ts L180-L193 的 shapes 动态导入段适用于希望按需拆分 chunk、减小首屏加载体积的打包场景。2.3 CDN 场景两个全局变量CDN/浏览器直连场景由另外两个入口文件处理src/browser.ts把loadAll挂到globalThis.loadAll并初始化globalThis.__tsParticlesInternalssrc/bundle.ts在 browser 基础上再export * from tsparticles/engine并把tsParticles实例也挂到globalThis。这正好对应 README 描述的 CDN 两种文件形态文件形态等价入口tsparticles.all.bundle.min.js单文件包含全部脚本含tsParticles实例 loadAllsrc/bundle.tstsparticles.all.min.js仅提供loadAll函数engine 及各依赖需自行在页面中引入src/browser.ts仓库内 demo/vanilla/views/bundle.pug 与 demo/vanilla/views/index.pug 分别引用了这两个文件可作为真实页面用法对照。三、Quick Checklist官方三步法README 给出的快速核对清单本质上与上面的实现机制一一对应安装tsparticles/engine或使用下文 CDN bundle 文件在tsParticles.load(...)之前调用包的 loader 函数即loadAll在tsParticles.load(...)的配置中应用所需的功能选项例如启用了paths或emitters相关选项时对应能力已由 bundle 注册完成。四、各环境接入方式4.1 CDN / Vanilla JS / jQueryBundle 版tsparticles.all.bundle.min.js的行为与 particles.js v1 的单文件体验一致可直接使用tsParticles全局实例官方推荐它作为从 v1 迁移的最简路径所有新增特性都以外部包形式存在该 bundle 只包含 v1 的部分特性。脚本加载完成后(async () { await loadAll(tsParticles); await tsParticles.load({ id: tsparticles, options: { /* options */ }, }); })();非 Bundle 版则需要你在页面中手动引入 README Included Packages 一节列出的全部依赖脚本工作量大一些但可按需组合。4.2 React.js / Preact / Inferno三者的组件语法相同react-particles组件 init回调README 给出类组件与函数组件两种写法类组件import React from react; import Particles from react-particles; import type { Engine } from tsparticles/engine; import { loadAll } from tsparticles/all; export class ParticlesContainer extends PureComponentunknown { // 自定义组件的 tsParticles 安装过程 async customInit(engine: Engine) { // 向 tsParticles 实例注册全量 bundle await loadAll(engine); } render() { const options { /* custom options */ }; return Particles options{options} init{this.customInit} /; } }Hooks / 函数组件import React, { useCallback } from react; import Particles from react-particles; import type { Engine } from tsparticles/engine; import { loadAll } from tsparticles/all; export function ParticlesContainer(props: unknown) { const customInit useCallback(async (engine: Engine) { await loadAll(engine); }); const options { /* custom options */ }; return Particles options{options} init{customInit} /; }框架层参考实现见 wrappers/reactReact 项目的完整示例工程在 demo/react。4.3 Vue2.x 与 3.x两代 Vue 语法一致通过:particlesInit绑定初始化回调Particles idtsparticles :particlesInitparticlesInit :optionsoptions /const options { /* custom options */ }; async function particlesInit(engine) { await loadAll(engine); }对应封装位于 wrappers/vue2 与 wrappers/vue3。4.4 Angularng-particles [id]id [options]options [particlesInit]particlesInit/ng-particlesconst options { /* custom options */ }; async function particlesInit(engine: Engine): void { await loadAll(engine); }封装实现见 wrappers/angular。4.5 SvelteParticles idtsparticles options{options} particlesInit{particlesInit} /let options { /* custom options */ }; let particlesInit async engine { await loadAll(engine); };封装实现见 wrappers/svelte。五、常见陷阱Common PitfallsREADME 的 Common pitfalls 一节值得在接入时逐条对照调用顺序不要在loadAll(...)之前调用tsParticles.load(...)。结合第二节的实现可知loadAll只是向pluginManager登记回调若load先执行回调尚未注册粒子系统会以缺少插件的状态启动peer 依赖核对启用高级选项前先确认所需的 peer 包或本 all bundle已就位——all bundle 已覆盖上表全部能力因此“功能缺失”通常应首先怀疑调用顺序而非缺失插件变更隔离一次只改一个 option 分组便于快速定位回归问题。六、版本与构建说明包版本4.3.3及完整的dependencies约 80 个workspace:*依赖见 bundles/all/package.json构建脚本为tsparticles-buildRollup 侧由共享插件 loadParticlesBundle 生成 ESM / CJS / browser / types 等产物main、module、browser、types与./lazy子路径均在exports中声明该包sideEffects声明为dist/browser/browser.js与dist/browser/index.js在 CDN 场景下浏览器构建的副作用挂载全局变量不会被 tree-shaking 掉这与第四节的 CDN 用法相互印证。七、延伸阅读各功能包的源码入口effects、interactions、paths、plugins、shapes、updaters其他聚合包可对照 bundles/basic、bundles/full、bundles/slimall 是其中能力最全的一个原生页面完整用法参考 demo/vanilla其中 bundle.pug 与 canvas.pug 分别演示了 bundle 与非 bundle 两种 CDN 文件。【免费下载链接】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),仅供参考
返回列表