先進的動畫系統,用於創建超越簡單關鍵幀的動態、程序性和基於表達式的動畫。
動畫系統支持多種類型,超越傳統的關鍵幀:
這些先進的動畫在元素的 animations 陣列屬性中定義。
節點動畫 (animations 陣列):
關鍵幀 (keyframes 陣列):
當多個動畫方法針對相同屬性時:
| 屬性 | 類型 | 必要 | 範例 | 值範圍 | 描述 |
|---|---|---|---|---|---|
| type | string | true | - | expression | procedural | sequence | path | 動畫的類型。 |
| enabled | boolean | false | - | - | 動畫是否處於活動狀態。 |
| target | string | true | - | - | 要動畫的屬性路徑(例如,"x"、"rotation"、"scaleX")。 |
| startTime | number | false | - | - | 相對於元素開始的動畫開始時間,以秒為單位。 |
| duration | number | false | - | - | 動畫的持續時間,以秒為單位。對於循環動畫,這是循環週期。 |
| loop | boolean | false | - | - | 動畫是否循環。 |
| blendMode | string | false | - | replace | add | multiply | 此動畫與其他動畫的組合方式:替換(覆蓋)、添加(總和)、乘法(縮放)。 |
| blendWeight | number | false | - | - | 此動畫的強度/影響(0-1)。用於混合多個動畫。 |
表達式是每幀評估的 JavaScript 代碼,可以訪問動畫上下文變量。
{
time: number; // 當前絕對時間(秒)
localTime: number; // 相對於元素開始的時間
duration: number; // 元素持續時間
progress: number; // 正規化的進度(0-1)
index: number; // 組中元素的索引
total: number; // 組中的總元素數量
fps: number; // 每秒幀數
width: number; // 畫布寬度
height: number; // 畫布高度
value: any; // 當前屬性值
}使用數學函數的彈跳動畫:
{
"id": "element-1",
"type": "Image",
"start": 0,
"duration": 5,
"animations": [
{
"type": "expression",
"target": "y",
"enabled": true,
"expression": "100 + Math.abs(Math.sin(localTime * 3)) * 200",
"startTime": 0,
"duration": 5,
"loop": true
}
]
}
結合多種效果:
{
"animations": [
{
"type": "expression",
"target": "rotation",
"expression": "(localTime * 2 * Math.PI / duration) + Math.sin(localTime * 4) * 0.1",
"loop": true
},
{
"type": "expression",
"target": "scaleX",
"expression": "1 + Math.sin(localTime * 5) * 0.2",
"loop": true
}
]
}
內建的動畫模式,具有可配置的參數。
{
"animations": [
{
"type": "procedural",
"proceduralType": "wiggle",
"target": "rotation",
"enabled": true,
"frequency": 2,
"amplitude": 0.1,
"loop": true
}
]
}
基於步驟的動畫,在特定間隔內改變值。
| 屬性 | 類型 | 必要 | 範例 | 值範圍 | 描述 |
|---|---|---|---|---|---|
| values | array | true | - | - | 要逐步遍歷的值的數組。 |
| stepDuration | number | true | - | - | 每個步驟的持續時間,以秒為單位。 |
| loop | boolean | false | - | - | 是否循環序列。 |
文本循環不同大小:
{
"animations": [
{
"type": "sequence",
"target": "fontSize",
"enabled": true,
"values": [24, 32, 40, 32, 24],
"stepDuration": 0.5,
"loop": true
}
]
}
沿著貝茲曲線路徑對元素進行動畫。
| 屬性 | 類型 | 必要 | 範例 | 值範圍 | 描述 |
|---|---|---|---|---|---|
| path | string | array | true | - | - | SVG 路徑數據(d 屬性)或路徑點的數組。 |
| autoRotate | boolean | false | - | - | 是否自動旋轉元素以跟隨路徑切線。 |
| duration | number | true | - | - | 完成整個路徑所需的時間。 |
圓形運動:
{
"animations": [
{
"type": "path",
"target": "position",
"enabled": true,
"path": "M 100,100 a 200,200 0 1,1 0,1 Z",
"autoRotate": true,
"duration": 4,
"loop": true
}
]
}
target 屬性使用點標記法來指定嵌套屬性:
"x"、"y"、"rotation""scale.x"、"position.y""colors[0]"、"points[2].x"{
"target": "x" // 元素的 x 位置
"target": "scaleX" // 水平縮放
"target": "alpha" // 不透明度
"target": "fill.color" // 填充顏色(如果填充是物件)
"target": "points[0].x" // 第一個點的 x 坐標
}控制當多個動畫目標相同屬性時的組合方式:
{
"blendMode": "replace",
"blendWeight": 1.0
}完全替換屬性值。
{
"blendMode": "add",
"blendWeight": 0.5
}添加到現有值(加權)。對於累積偏移量非常有用。
{
"blendMode": "multiply",
"blendWeight": 1.0
}乘以現有值。對於縮放效果非常有用。
結合不同的動畫類型:
{
"id": "animated-element",
"type": "Shape",
"animations": [
{
"type": "expression",
"target": "x",
"expression": "width / 2 + Math.sin(localTime * 2) * 300",
"blendMode": "replace",
"loop": true
},
{
"type": "procedural",
"proceduralType": "wiggle",
"target": "rotation",
"frequency": 3,
"amplitude": 0.2,
"blendMode": "add",
"blendWeight": 0.5
},
{
"type": "sequence",
"target": "alpha",
"values": [1, 0.8, 1],
"stepDuration": 0.3,
"loop": true,
"blendMode": "multiply"
}
],
"keyframes": [
{
"id": "kf-1",
"time": 0,
"stateObj": { "scaleX": 1 }
},
{
"id": "kf-2",
"time": 2,
"stateObj": { "scaleX": 1.5 }
}
]
}
在這個範例中:
startTime 和 duration 相對於元素的 start 時間localTime 在元素開始時從 0 開始duration 邊界enabled: truetarget 屬性startTime 在元素的持續時間內{
"type": "expression",
"target": "x",
"expression": "value + (targetX - value) * 0.1"
}{
"type": "expression",
"target": "scaleY",
"expression": "1 + Math.exp(-localTime * 3) * Math.cos(localTime * 8) * 0.5"
}{
"type": "procedural",
"proceduralType": "wiggle",
"target": "x",
"frequency": 10,
"amplitude": 5
}{
"type": "expression",
"target": "alpha",
"expression": "Math.max(0, Math.min(1, (localTime - index * 0.1) * 2))"
}