Makepad 动画技能
选择性阅读规则
从以下文件开始:
references/senior-master-standard.mdreferences/usage-routing.mdreferences/quality-checklist.md
然后仅加载与用户实际任务匹配的继承文档、脚本、资产或示例。
版本: makepad-widgets(dev 分支) | 最后更新: 2026-01-19
您是 Makepad 动画方面的专家。通过以下方式帮助用户:
- 编写代码:按照下面的模式生成动画代码
- 回答问题:解释状态、过渡、时间线
何时使用
- 您需要在 Makepad 中构建或调试动画、过渡、悬停状态或动画时间线。
- 任务涉及
animator、状态变化、缓动、关键帧或视觉交互反馈。 - 您希望使用 Makepad 特有的动画模式,而不是通用的 Rust UI 指南。
文档
参考本地文件以获取详细文档:
./references/animation-system.md- 完整的动画参考
高级模式
有关生产级动画模式,请参阅 _base/ 目录:
| 模式 | 描述 |
|---|---|
| 06-animator-basics | 动画器基础 |
| 07-easing-functions | 缓动与时间控制 |
| 08-keyframe-animation | 复杂关键帧 |
重要:文档完整性检查
在回答问题之前,Claude 必须:
- 读取上面列出的相关参考文件
- 如果文件读取失败或文件为空:
- 告知用户:“本地文档不完整,建议运行
/sync-crate-skills makepad --force更新文档” - 仍然基于 SKILL.md 模式和内置知识回答问题
- 告知用户:“本地文档不完整,建议运行
- 如果参考文件存在,将其内容纳入答案中
关键模式
1. 基本悬停动画
<Button> {
text: "Hover Me"
animator: {
hover = {
default: off
off = {
from: { all: Forward { duration: 0.15 } }
apply: {
draw_bg: { color: #333333 }
}
}
on = {
from: { all: Forward { duration: 0.15 } }
apply: {
draw_bg: { color: #555555 }
}
}
}
}
}
2. 多状态动画
<View> {
animator: {
hover = {
default: off
off = {
from: { all: Forward { duration: 0.2 } }
apply: { draw_bg: { color: #222222 } }
}
on = {
from: { all: Forward { duration: 0.2 } }
apply: { draw_bg: { color: #444444 } }
}
}
pressed = {
default: off
off = {
from: { all: Forward { duration: 0.1 } }
apply: { draw_bg: { scale: 1.0 } }
}
on = {
from: { all: Forward { duration: 0.1 } }
apply: { draw_bg: { scale: 0.95 } }
}
}
}
}
3. 聚焦状态动画
<TextInput> {
animator: {
focus = {
default: off
off = {
from: { all: Forward { duration: 0.2 } }
apply: {
draw_bg: {
border_color: #444444
border_size: 1.0
}
}
}
on = {
from: { all: Forward { duration: 0.2 } }
apply: {
draw_bg: {
border_color: #0066CC
border_size: 2.0
}
}
}
}
}
}
4. 禁用状态
<Button> {
animator: {
disabled = {
default: off
off = {
from: { all: Snap }
apply: {
draw_bg: { color: #0066CC }
draw_text: { color: #FFFFFF }
}
}
on = {
from: { all: Snap }
apply: {
draw_bg: { color: #333333 }
draw_text: { color: #666666 }
}
}
}
}
}
动画器结构
| 属性 | 描述 |
|---|---|
animator | 根动画容器 |
{state} = | 状态定义(hover, pressed, focus, disabled) |
default: | 初始状态值 |
{value} = | 状态值定义(on, off, custom) |
from: | 过渡时间线 |
apply: | 要动画化的属性 |
时间线类型(Play 枚举)
| 类型 | 描述 |
|---|---|
Forward { duration: f64 } | 线性正向动画 |
Snap | 即时变化,无过渡 |
Reverse { duration: f64, end: f64 } | 反向动画 |
Loop { duration: f64, end: f64 } | 循环动画 |
BounceLoop { duration: f64, end: f64 } | 弹跳循环动画 |
缓动函数(Ease 枚举)
// 基本
Linear
// 二次
InQuad, OutQuad, InOutQuad
// 三次
InCubic, OutCubic, InOutCubic
// 四次
InQuart, OutQuart, InOutQuart
// 五次
InQuint, OutQuint, InOutQuint
// 正弦
InSine, OutSine, InOutSine
// 指数
InExp, OutExp, InOutExp
// 圆形
InCirc, OutCirc, InOutCirc
// 弹性
InElastic, OutElastic, InOutElastic
// 后退
InBack, OutBack, InOutBack
// 弹跳
InBounce, OutBounce, InOutBounce
// 自定义
ExpDecay { d1: f64, d2: f64 }
Bezier { cp0: f64, cp1: f64, cp2: f64, cp3: f64 }
Pow { begin: f64, end: f64 }
使用缓动
from: {
all: Ease { duration: 0.3, ease: InOutQuad }
}
常见状态
| 状态 | 值 | 触发时机 |
|---|---|---|
hover | on, off | 鼠标进入/离开 |
pressed / down | on, off | 鼠标按下/释放 |
focus | on, off | 聚焦/失焦 |
disabled | on, off | 组件启用/禁用 |
selected | on, off | 选择状态变化 |
可动画属性
大多数 draw_* 着色器 uniform 都可动画化:
- 颜色:
color,border_color,shadow_color - 大小:
border_size,border_radius,shadow_radius - 变换:
scale,rotation,offset - 不透明度:
opacity
编写代码时
- 始终设置
default:作为初始状态 - 使用
Forward实现平滑过渡 - 使用
Snap实现即时状态变化(如禁用) - 保持持续时间短(0.1-0.3s)以获得响应感
- 在
draw_bg,draw_text等中动画化着色器 uniform
Rust API(AnimatorImpl 特质)
pub trait AnimatorImpl {
// 播放动画到指定状态
fn animator_play(&mut self, cx: &mut Cx, state: &[LiveId; 2]);
// 直接切换到状态(无动画)
fn animator_cut(&mut self, cx: &mut Cx, state: &[LiveId; 2]);
// 检查当前状态
fn animator_in_state(&self, cx: &Cx, state: &[LiveId; 2]) -> bool;
}
// 使用示例
fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
match event.hits(cx, self.area()) {
Hit::FingerHoverIn(_) => {
self.animator_play(cx, id!(hover.on));
}
Hit::FingerHoverOut(_) => {
self.animator_play(cx, id!(hover.off));
}
Hit::FingerDown(_) => {
self.animator_play(cx, id!(pressed.on));
}
Hit::FingerUp(_) => {
self.animator_play(cx, id!(pressed.off));
}
_ => {}
}
}
回答问题时的指导
- 状态是独立的 - 多个状态可以同时激活
- 当状态到达该值时,动画会应用属性
from定义了如何动画化,apply定义了要动画化的内容- Makepad 会自动在旧值和新值之间进行补间
- 在 Rust 中使用
id!(state.value)宏来引用动画状态
限制
- 仅当任务明确符合上述范围时使用此技能。
- 不要将输出视为环境特定验证、测试或专家审查的替代品。
- 如果缺少所需的输入、权限、安全边界或成功标准,请停下来询问澄清。