跳到主要内容
Office Open

PPTX

效果与填充

为形状应用阴影、发光、3D 效果和各种填充类型

通过视觉效果(阴影、发光、3D)和填充(纯色、渐变、图片、透明)增强形状。

#纯色填充

ts
import { generatePresentation } from "@office-open/pptx";
JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "10.6cm",
            "height": "6.6cm",
            "properties": {
              "fill": {
                "type": "solid",
                "color": "2E74B5"
              }
            }
          }
        }
      ]
    }
  ]
}

简写形式 -- 直接传入颜色字符串:

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "10.6cm",
            "height": "6.6cm",
            "properties": {
              "fill": "2E74B5"
            }
          }
        }
      ]
    }
  ]
}

#渐变填充

#线性渐变(带角度)

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "15.9cm",
            "height": "6.6cm",
            "properties": {
              "fill": {
                "type": "gradient",
                "stops": [
                  {
                    "position": 0,
                    "color": "0D47A1"
                  },
                  {
                    "position": 100,
                    "color": "42A5F5"
                  }
                ],
                "angle": 45
              }
            }
          }
        }
      ]
    }
  ]
}

#径向渐变(带路径)

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "10.6cm",
            "height": "10.6cm",
            "properties": {
              "geometry": "ellipse",
              "fill": {
                "type": "gradient",
                "stops": [
                  {
                    "position": 0,
                    "color": "FFFFFF"
                  },
                  {
                    "position": 100,
                    "color": "1565C0"
                  }
                ],
                "path": "circle"
              }
            }
          }
        }
      ]
    }
  ]
}

#多色标渐变

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "15.9cm",
            "height": "6.6cm",
            "properties": {
              "fill": {
                "type": "gradient",
                "stops": [
                  {
                    "position": 0,
                    "color": "E53935"
                  },
                  {
                    "position": 50,
                    "color": "FF9800"
                  },
                  {
                    "position": 100,
                    "color": "FFEB3B"
                  }
                ],
                "angle": 90
              }
            }
          }
        }
      ]
    }
  ]
}

#图片填充(Blip Fill)

data 字段接受原始字节(Uint8Array、ArrayBuffer、Buffer),也支持 base64 data URL(如 data:image/png;base64,...)。

ts
import * as fs from "node:fs";
JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "15.9cm",
            "height": "9.3cm",
            "properties": {
              "fill": {
                "type": "blip",
                "data": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
                "imageType": "png"
              }
            }
          }
        }
      ]
    }
  ]
}

#无填充(透明)

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "13.2cm",
            "height": "6.6cm",
            "properties": {
              "fill": {
                "type": "none"
              },
              "outline": {
                "color": "2E74B5",
                "width": 2
              }
            }
          }
        }
      ]
    }
  ]
}

#轮廓

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "13.2cm",
            "height": "6.6cm",
            "properties": {
              "fill": {
                "type": "solid",
                "color": "E3F2FD"
              },
              "outline": {
                "color": "1565C0",
                "width": "0.1cm",
                "dash": "solid"
              }
            }
          }
        }
      ]
    }
  ]
}

#效果

使用 effects 属性控制阴影、发光、倒影、柔化边缘和 3D 效果:

#外部阴影

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "5.3cm",
            "height": "3.2cm",
            "properties": {
              "fill": "ED7D31",
              "effects": {
                "outerShadow": {
                  "blurRadius": 50800,
                  "distance": 38100,
                  "direction": 90,
                  "color": {
                    "value": "000000",
                    "transforms": {
                      "alpha": 50
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  ]
}

#内部阴影

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "5.3cm",
            "height": "3.2cm",
            "properties": {
              "fill": "5B9BD5",
              "effects": {
                "innerShadow": {
                  "blurRadius": 40000,
                  "distance": 30000,
                  "direction": 90,
                  "color": {
                    "value": "000000",
                    "transforms": {
                      "alpha": 40
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  ]
}

#发光

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "5.3cm",
            "height": "3.2cm",
            "properties": {
              "fill": "70AD47",
              "effects": {
                "glow": {
                  "radius": 152400,
                  "color": {
                    "value": "92D050",
                    "transforms": {
                      "alpha": 60
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  ]
}

#倒影

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "5.3cm",
            "height": "3.2cm",
            "properties": {
              "fill": "FFC000",
              "effects": {
                "reflection": {
                  "blurRadius": 6350,
                  "distance": 38100,
                  "direction": 90,
                  "startAlpha": 90,
                  "endAlpha": 0
                }
              }
            }
          }
        }
      ]
    }
  ]
}

#柔化边缘

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "5.3cm",
            "height": "3.2cm",
            "properties": {
              "fill": "BF8F00",
              "effects": {
                "softEdge": 50800
              }
            }
          }
        }
      ]
    }
  ]
}

#3D 旋转和斜面

3D 拆成两个形状属性 —— 不在 effects 下:

  • scene3d(a:scene3d)—— 3D 场景:camera 与 lightRig(可选 backdrop)。相机旋转是球坐标 { lat, lon, rev },单位为度;lightRig 携带 rig 与 direction。
  • shape3d(a:sp3d)—— 3D 形状本体:bevelT/bevelB(顶/底斜面)、 extrusionH、contourW、z 与 prstMaterial。
JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "5.3cm",
            "height": "4.0cm",
            "properties": {
              "fill": "FFC000",
              "scene3d": {
                "camera": {
                  "preset": "perspectiveFront",
                  "fov": 30,
                  "rotation": {
                    "lat": 0,
                    "lon": 0,
                    "rev": 10
                  }
                },
                "lightRig": {
                  "rig": "threePt",
                  "direction": "top"
                }
              },
              "shape3d": {
                "bevelT": {
                  "w": 76200,
                  "h": 76200,
                  "prst": "circle"
                },
                "extrusionH": 50000,
                "prstMaterial": "plastic"
              }
            }
          }
        }
      ]
    }
  ]
}

#组合效果

可以同时应用多种效果:

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "5.3cm",
            "height": "3.2cm",
            "properties": {
              "fill": "7030A0",
              "effects": {
                "outerShadow": {
                  "blurRadius": 40000,
                  "distance": 30000,
                  "direction": 45,
                  "color": {
                    "value": "000000",
                    "transforms": {
                      "alpha": 40
                    }
                  }
                },
                "glow": {
                  "radius": 101600,
                  "color": {
                    "value": "B381E7",
                    "transforms": {
                      "alpha": 35
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  ]
}

#Effects 选项

effects 只承载扁平的 2D 效果(EffectListOptions)。3D 直接位于形状上,分属 scene3d 与 shape3d。

属性类型说明
effects.blurBlurEffectOptions模糊效果
effects.glowGlowEffectOptions发光效果(radius + color)
effects.innerShadowInnerShadowEffectOptions内部阴影(blurRadius、distance、direction 单位为度、color)
effects.outerShadowOuterShadowEffectOptions外部阴影(blurRadius、distance、direction 单位为度、color)
effects.reflectionReflectionEffectOptions | true倒影(blurRadius、distance、direction 单位为度、startAlpha、endAlpha)
effects.softEdgenumber | UniversalMeasure柔化边缘半径 —— 标量(EMU 或 UniversalMeasure)
scene3dScene3DOptions3D 场景 —— camera 与 lightRig(角度单位为度)
shape3dShape3DOptions3D 形状本体 —— bevelT/bevelB、extrusionH、contourW、z、prstMaterial

阴影与发光的 color 是 SolidFillOptions({ value, transforms });不透明度 通过 color.transforms.alpha(整数百分比)传入,而非顶层 alpha。