@krsmt0113/letters-effect
v0.1.1
Published
Reusable handwritten letters animation effect
Downloads
111
Maintainers
Readme
@krsmt0113/letters-effect
把「手写描边文字动画」封装成可复用包,适合在任意前端项目中直接使用。
字形素材来自你复制站点中的同一套小写字母路径数据(a-z)。
安装
npm install @krsmt0113/letters-effect如果你当前是本仓库本地开发,可直接从
packages/letters-effect引用源码。
快速开始
1) HTML
<div id="letters" style="width:320px;height:160px"></div>2) JavaScript
import { createLettersEffect } from "@krsmt0113/letters-effect";
const effect = createLettersEffect("#letters", {
text: "hello",
stroke: "#ffffff",
strokeWidth: 2,
duration: 3200,
loop: true,
autoplay: true,
});API
createLettersEffect(target, options)
target:HTMLElement | string,容器元素或选择器options:text文本内容(默认hello,仅支持a-z和空格,其他字符会被过滤)stroke描边颜色(默认#ffffff)strokeWidth描边粗细(默认2)duration单次动画时长 ms(默认3200)loop是否循环(默认false)autoplay是否自动播放(默认true)svgClassName给内部svg附加 classsvgDefs注入<defs>内容(可自定义渐变)ariaLabel无障碍标签paddingSVG 内边距(默认4)letterSpacing字母间距(默认1.8)wordSpacing空格宽度(默认6)
返回 LettersEffect 实例。
实例方法
play()开始/继续播放pause()暂停播放restart()从头播放setText(text)更新文字并重绘setProgress(value)手动设置进度(0 ~ 1)setOptions(options)批量更新参数并重绘destroy()销毁实例并移除 SVG
渐变描边示例
const defs = `
<linearGradient id="rainbow" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="120" y2="0">
<stop offset="0%" stop-color="#007AFF" />
<stop offset="20%" stop-color="#AF52DE" />
<stop offset="40%" stop-color="#FF3B30" />
<stop offset="60%" stop-color="#FF9500" />
<stop offset="80%" stop-color="#FFCC00" />
<stop offset="100%" stop-color="#34C759" />
</linearGradient>
`;
createLettersEffect("#letters", {
text: "apple",
stroke: "url(#rainbow)",
svgDefs: defs,
});