SuperAPI AI 工具集

svg-stroke-reveal

通用与其它

查看项目

更新于 10-16 01:13

技能介绍

用途:让 SVG 中带描边的 <path> 逐渐“画”出来。两种模式:once(进入视口时播放一次,适合 logo、签名、图标)和 scroll(随滚动进度连续绘制,适合折线图、路线图、行程示意)。触发词包括“滚动绘制 SVG”“svg stroke draw”“logo 渐显”“path animation reveal”“stroke-dashoffset”或 /svg-stroke-reveal。

主要能力:对每个 [data-svgr] 内的 <path> 调用 getTotalLength(),初始化时设置 strokeDasharray 与 strokeDashoffset 等于路径长度以隐藏线条。once 模式用 IntersectionObserver(阈值 0.3)触发一次,通过 CSS transition 将 dashoffset 归零,触发后立即 unobserve;scroll 模式由 IntersectionObserver 只切换“在视口内”标志,再用一个 rAF 节流的滚动监听按 progress=(innerHeight-rect.top)/(innerHeight+rect.height) 逐帧写入 dashoffset,不使用 CSS transition。多路径可用 transition-delay: calc(var(--svgr-i) * 160ms) 按 DOM 顺序错开。

使用方式:把 SVG 加上 [data-svgr] 并选择 once 或 scroll;默认复制作者原始 CodePen 文件(+ _assets/)到项目并保留署名注释,仅改内容、图片、颜色;若项目不允许外部库,则改用技能内 assets/ 的 vanilla 精简版并适配。本地需通过 HTTP 运行,不要用 file://。用 playwright-verify 验证:scroll 模式截取滚动前/中/后确认 dashoffset 递减,once 模式确认反复滚动只触发一次。

限制:<text> 无法描边绘制,必须先转成 outline <path>(Illustrator Create Outlines、Figma Flatten 等);不要对承载 path 的元素做非等比 transform: scale(sx, sy),否则绘制速度会失真,应通过 viewBox 与 width/height 控制尺寸;once 与 scroll 各用独立 IntersectionObserver,全页 scroll 模式共用一个 rAF 节流监听;prefers-reduced-motion: reduce 时直接设 dashoffset=0,不做动画。该技能不适用于通用滚动效果(透明度/位移显现、文字错落、导航收缩、scrollspy、视差等)。