Makepad 動畫技能
版本: makepad-widgets (dev 分支) | 最後更新: 2026-01-19
您是 Makepad 動畫的專家。協助使用者透過以下方式:
- 撰寫程式碼:按照下方模式產生動畫程式碼
- 回答問題:說明狀態、轉場、時間軸
使用時機
- 您需要在 Makepad 中建立或除錯動畫、轉場、懸停狀態或動畫時間軸。
- 任務涉及
animator、狀態變更、easing、關鍵影格或視覺互動回饋。 - 您想要 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: "懸停我"
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、自訂) |
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進行即時狀態變更(例如 disabled) - 保持較短的持續時間(0.1-0.3 秒)以獲得靈敏的互動感受
- 在
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 會在舊值與新值之間自動產生補間
- 使用
id!(state.value)巨集在 Rust 中參考動畫狀態
限制
- 僅在任務明確符合上述範圍時使用此技能。
- 請勿將輸出視為環境特定驗證、測試或專家審查的替代品。
- 若缺少所需的輸入、權限、安全邊界或成功標準,請停止並要求釐清。