PPTX
动画效果
为形状添加进入、退出、强调和运动路径动画
为形状添加动画效果,控制它们在演示文稿中的出现和行为方式。
#基本用法
动画位于幻灯片级别,不在形状上设置 —— XSD 中 CT_Shape 没有子级 timing
元素。每张幻灯片提供 animations 数组,其每一项携带 shapeId(目标形状的
cNvPr id)以及内联的动画字段:
import { generatePresentation } from "@office-open/pptx";
{
"slides": [
{
"children": [
{
"shape": {
"id": 2,
"x": "1.3cm",
"y": "1.3cm",
"width": "15.9cm",
"height": "6.6cm",
"textBody": { "text": "动画形状" }
}
}
],
"animations": [{ "shapeId": 2, "type": "fade", "duration": 800 }]
}
]
}
import { generatePresentation } from "@office-open/pptx";
generatePresentation({
slides: [
{
children: [
{
shape: {
id: 2,
x: "1.3cm",
y: "1.3cm",
width: "15.9cm",
height: "6.6cm",
textBody: { text: "动画形状" },
},
},
],
animations: [{ shapeId: 2, type: "fade", duration: 800 }],
},
],
});
animations 的每一项把 shapeId 与动画预设字段配对。shapeId 与目标形状
NonVisualDrawingProperties(cNvPr id)上的 id 一致,由 shape.id
指定;其余字段即下面各小节记录的预设。
#按名称指定目标
新建文档可以改用 shapeName 引用目标——库在编译期把它解析为目标形状的
cNvPr id,作用域是同一张幻灯片上的形状:
{
"children": [
{
"shape": {
"name": "Hero",
"x": "1.3cm",
"y": "1.3cm",
"width": "15.9cm",
"height": "6.6cm",
"textBody": {
"text": "命名形状"
}
}
}
],
"animations": [
{
"shapeName": "Hero",
"type": "fade",
"duration": 800
}
]
}
shapeName 是创作通道:id 重新编号时名称保持稳定;名称未知或重复会在编译期
抛错,而不是发出悬空的 spTgt @spid。shapeId 是往返通道(解析来源保留源
id);两者同时给出时以 shapeId 为准。
#动画类型
#进入动画(class: "entrance",默认类别)
| 类型 | 描述 | 支持的方向 |
|---|---|---|
appear | 立即出现 | — |
fade | 淡入 | — |
fly | 从边缘飞入 | left、right、up、down |
wipe | 从边缘擦入 | left、right、up、down |
dissolve | 溶解出现 | — |
split | 分裂出现 | horizontal、vertical |
blinds | 百叶窗效果 | horizontal、vertical |
checker | 棋盘效果 | horizontal、vertical |
randomBars | 随机条纹 | horizontal、vertical |
wheel | 轮辐效果 | — |
zoom | 缩放出现 | — |
cover | 从边缘覆盖 | left、right、up、down |
push | 从边缘推入 | left、right、up、down |
strips | 从角落条状出现 | left、right、up、down |
#退出动画(class: "exit")
与进入动画相同的类型,添加 class: "exit":
[
{ "shapeName": "Title", "type": "fade", "class": "exit", "duration": 800 },
{ "shapeName": "Title", "type": "fly", "class": "exit", "direction": "right" }
]
#强调动画(class: "emphasis")
emphasisType | 描述 |
|---|---|
growShrink | 放大后缩小 |
spin | 完整旋转 |
colorChange | 变色(需指定 color) |
transparency | 半透明闪烁 |
boldFlash | 加粗闪烁 |
wave | 波浪效果 |
pulse | 脉冲效果 |
growWithTurn | 旋转放大 |
[
{ "shapeName": "Title", "class": "emphasis", "emphasisType": "growShrink", "duration": 800 },
{ "shapeName": "Title", "class": "emphasis", "emphasisType": "spin", "duration": 1000 },
{ "shapeName": "Title", "class": "emphasis", "emphasisType": "colorChange", "color": "FF0000" }
]
#运动路径动画
pathType | 描述 |
|---|---|
line | 直线 |
arc | 弧线 |
circle | 圆形路径 |
curve | S 形曲线 |
figureEight | 8 字形 |
bounce | 弹跳路径 |
loop | 环形路径 |
customPath | 自定义 SVG 路径(需指定 path) |
[
{ "shapeName": "Title", "pathType": "circle", "duration": 1500 },
{
"shapeName": "Title",
"pathType": "customPath",
"path": "M 0 0 L 100 0 L 100 100 L 0 100 Z",
"duration": 1200
}
]
#媒体播放(class: "mediaCall")
通过 mediaType 触发音视频播放(目标是 { video } / { audio } 子项的媒体
框),shapeId 必须指向该媒体框:
[
{
"shapeName": "Video 1",
"class": "mediaCall",
"mediaType": "playVideo",
"fullScreen": true,
"showWhenStopped": true
},
{
"shapeName": "Video 1",
"class": "mediaCall",
"mediaType": "playAudio",
"volume": 80,
"mute": false
}
]
| 属性 | 类型 | 说明 |
|---|---|---|
mediaType | "playAudio" | "playVideo" | "play" | 媒体动画类型 |
isNarration | boolean | 媒体为旁白 |
fullScreen | boolean | 全屏播放视频 |
volume | number | 音量 |
mute | boolean | 静音 |
showWhenStopped | boolean | 停止后保持媒体框可见 |
#动画选项
| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
type | AnimationType | "appear" | 动画预设类型 |
class | "entrance" | "exit" | "emphasis" | "mediaCall" | "entrance" | 动画类别 |
duration | number | 500 | 持续时间(毫秒) |
delay | number | 0 | 开始前延迟(毫秒) |
trigger | AnimationTrigger | "onClick" | 触发方式 |
direction | AnimationDirection | — | 动画方向 |
emphasisType | EmphasisType | — | 强调动画类型(class="emphasis") |
pathType | PathAnimationType | — | 运动路径类型 |
path | string | — | 自定义 SVG 路径字符串 |
speed | number | — | 速度倍率 |
repeatCount | number | — | 重复次数 |
autoReverse | boolean | false | 完成后自动反转 |
color | string | — | colorChange 的目标颜色 |
pathEditMode | "relative" | "fixed" | "none" | — | 运动路径编辑模式 |
rotationAngle | number | — | 运动路径旋转角(六万分之一度,p:animRot @rAng;原值透传) |
motionFrom | { x: string; y: string } | — | 运动路径起点 |
motionRotationCenter | { x: string; y: string } | — | 运动旋转中心 |
zoomContents | boolean | false | 缩放动画时缩放内容 |
#触发模式
[
{ "shapeName": "Title", "type": "fade", "trigger": "onClick" },
{ "shapeName": "Title", "type": "fade", "trigger": "withPrevious" },
{ "shapeName": "Title", "type": "fade", "trigger": "afterPrevious" }
]