共用類型

查看 View 屬性、資料類型及程式碼範例。

ViewContent

可繪製的 View、基本值或巢狀陣列;null/undefined 不產生內容。

type ViewContent =
  View | string | number | boolean | null | undefined | ViewContent[];

範例

const example: ViewContent = ['Speed', 78, null];

TextContent

文字統一呼叫 toString();null/undefined 留空,圖示以其他 View 組合。

type TextContent =
  | string
  | number
  | bigint
  | boolean
  | symbol
  | { toString(): string }
  | null
  | undefined;

範例

const example: TextContent = {
  toString() {
    return '78 km/h';
  },
};

HDRColor

可用於顏色和漸層的 HDR 顏色資料。

type HDRColor = { readonly $type: 'CapDrive.HDR'; readonly color: string };

範例

const example: HDRColor = HDR('white');

RGBColor

r/g/b 與透明度 a 範圍 0–1;space 可用 srgb 或 displayP3。

type RGBColor = {
  space?: 'srgb' | 'displayP3';
  r: number;
  g: number;
  b: number;
  a?: number;
};

範例

const example: RGBColor = {
  space: 'displayP3',
  r: 0.2,
  g: 0.7,
  b: 0.6,
  a: 0.8,
};

Color

顏色名稱、十六進位字串、RGBColor 或 HDR() 顏色。

type Color = string | RGBColor | HDRColor;

範例

const example: Color = '#30B3A2';

ViewPoint

以螢幕點為單位的局部 x/y 座標,並非經緯度。

type ViewPoint = { x: number; y: number };

範例

const example: ViewPoint = { x: 20, y: 40 };

ChartDataPoint

value.x/y 決定點的位置,label.x/y 決定標籤;y 為 null 時斷線。

type ChartDataPoint = {
  value: { x: number; y: number | null };
  label: { x: string; y: string };
};

範例

const example: ChartDataPoint = {
  value: { x: 12, y: null },
  label: { x: '12s', y: '' },
};

ChartRange

數值範圍 [min, max],並非索引或標籤。

type ChartRange = readonly [min: number, max: number];

範例

const example: ChartRange = [-20, 100];

MapCoordinate

地理緯度 latitude、經度 longitude,單位為度。

type MapCoordinate = { latitude: number; longitude: number };

範例

const example: MapCoordinate = { latitude: 31.2304, longitude: 121.4737 };

MapPathStyle

原生軌跡樣式:Color 顏色、線寬、端點、連接、虛線、外描邊及可選起終點標記。不支援漸層。

type MapPathStyle = {
  stroke?: Color;
  lineWidth?: number;
  lineCap?: 'round' | 'butt' | 'square';
  lineJoin?: 'round' | 'miter' | 'bevel';
  dash?: readonly number[];
  dashPhase?: number;
  outlineStroke?: Color;
  outlineWidth?: number;
  showEndpoints?: boolean;
};

範例

const example: MapPathStyle = {
  stroke: '#30B3A2',
  lineWidth: 4,
  outlineStroke: 'white',
  outlineWidth: 1,
  showEndpoints: true,
};

ChartAxisStyle

軸線填色、寬度、線端及虛線;clear 隱藏軸線,標籤另有開關。

type ChartAxisStyle = {
  stroke?: Paint;
  lineWidth?: number;
  lineCap?: 'round' | 'butt' | 'square';
  dash?: readonly number[];
  dashPhase?: number;
};

範例

const example: ChartAxisStyle = {
  stroke: '#888888',
  lineWidth: 1,
  dash: [4, 3],
};

BarChartDataPoint

一根長條的 value 與可選 label;按陣列順序排列。

type BarChartDataPoint = { value: number; label?: string };

範例

const example: BarChartDataPoint = { value: -25, label: 'B' };

PathCommand

move、line、quad、cubic、arc、close 路徑指令;座標和半徑用點,角度用度。

type PathCommand =
  | { op: 'move'; x: number; y: number }
  | { op: 'line'; x: number; y: number }
  | { op: 'quad'; x: number; y: number; cx: number; cy: number }
  | {
      op: 'cubic';
      x: number;
      y: number;
      c1x: number;
      c1y: number;
      c2x: number;
      c2y: number;
    }
  | {
      op: 'arc';
      x: number;
      y: number;
      radius: number;
      startAngle: number;
      sweepAngle: number;
    }
  | { op: 'close' };

範例

const example: PathCommand = {
  op: 'cubic',
  x: 100,
  y: 60,
  c1x: 20,
  c1y: 0,
  c2x: 80,
  c2y: 100,
};

GradientStop

顏色或含 location 的顏色物件;location 為 0–1 漸層位置。

type GradientStop = Color | { color: Color; location: number };

範例

const example: GradientStop = { color: '#30B3A2', location: 0.7 };

GradientColors

依序排列的漸層顏色或色標;純顏色等距分布。

type GradientColors = ReadonlyArray<GradientStop>;

範例

const example: GradientColors = [
  { color: 'red', location: 0 },
  { color: 'blue', location: 1 },
];

LinearGradientOptions

線性漸層 angle 用度,center 為正規化座標;(0.5, 0.5) 為中心。

type LinearGradientOptions = { angle?: number; center?: ViewPoint };

範例

const example: LinearGradientOptions = {
  angle: 90,
  center: { x: 0.5, y: 0.5 },
};

RadialGradientOptions

徑向漸層 startRadius/endRadius 用點,center 為正規化座標。

type RadialGradientOptions = {
  startRadius?: number;
  endRadius?: number;
  center?: ViewPoint;
};

範例

const example: RadialGradientOptions = {
  startRadius: 0,
  endRadius: 60,
  center: { x: 0.5, y: 0.5 },
};

AngularGradientOptions

角向漸層 angle 用度,center 為正規化座標。

type AngularGradientOptions = { angle?: number; center?: ViewPoint };

範例

const example: AngularGradientOptions = {
  angle: -90,
  center: { x: 0.5, y: 0.5 },
};

Gradient

由 LinearGradient、RadialGradient 或 AngularGradient 建立;可直接呼叫或使用 new。

type Gradient = {
  readonly type: 'linear' | 'radial' | 'angular';
  readonly angle?: number;
  readonly startRadius?: number;
  readonly endRadius?: number;
  readonly center?: ViewPoint;
  readonly colors: GradientColors;
};

範例

const example: Gradient = LinearGradient(['red', 'blue'], { angle: 90 });

Paint

繪製填色,可以是 Color 或 Gradient。

type Paint = Color | Gradient;

範例

const example: Paint = RadialGradient(['white', '#30B3A2'], {
  endRadius: 60,
});

DrawingEffect

特效的顏色、不透明度、模糊半徑及 x/y 位移;距離用點。

type DrawingEffect = {
  color?: Color;
  opacity?: number;
  radius?: number;
  x?: number;
  y?: number;
};

範例

const example: DrawingEffect = {
  color: 'cyan',
  opacity: 0.6,
  radius: 8,
  x: 0,
  y: 2,
};

ViewTransform

整體視覺變換不改變版面。平移用點,旋轉及傾斜用度,縮放 1 不變,錨點用 0–1。

type ViewTransform = {
  translateX?: number;
  translateY?: number;
  scaleX?: number;
  scaleY?: number;
  rotation?: number;
  skewX?: number;
  skewY?: number;
  anchorX?: number;
  anchorY?: number;
};

範例

const example: ViewTransform = {
  rotation: 30,
  scaleX: 1.2,
  scaleY: 1.2,
  anchorX: 0.5,
  anchorY: 0.5,
};

RepeatAxisValue

數字作用於啟用的軸;x/y 物件可各別設定。

type RepeatAxisValue = number | { x?: number; y?: number };

範例

const example: RepeatAxisValue = { x: 120, y: 80 };

AnimationProperty

可動畫的屬性名或巢狀路徑;每種 View 僅支援列出的子集。

type AnimationProperty =
  | 'width'
  | 'height'
  | 'padding'
  | 'x'
  | 'y'
  | 'opacity'
  | 'cornerRadius'
  | 'blur'
  | 'shadowOpacity'
  | 'shadowRadius'
  | 'shadowX'
  | 'shadowY'
  | 'fontSize'
  | 'letterSpacing'
  | 'minimumScaleFactor'
  | 'spacing'
  | 'radius'
  | 'radiusX'
  | 'radiusY'
  | 'startAngle'
  | 'sweepAngle'
  | 'lineWidth'
  | 'dashPhase'
  | 'trimStart'
  | 'trimEnd'
  | 'barThickness'
  | 'value'
  | 'offset'
  | 'anchor'
  | 'period'
  | 'minLength'
  | 'color'
  | 'stroke'
  | 'background'
  | 'shadowColor'
  | 'transform.translateX'
  | 'transform.translateY'
  | 'transform.scaleX'
  | 'transform.scaleY'
  | 'transform.rotation'
  | 'transform.skewX'
  | 'transform.skewY'
  | 'transform.anchorX'
  | 'transform.anchorY'
  | 'period.x'
  | 'period.y'
  | 'value.x'
  | 'value.y'
  | 'anchor.x'
  | 'anchor.y'
  | 'offset.x'
  | 'offset.y'
  | 'start.x'
  | 'start.y'
  | 'end.x'
  | 'end.y'
  | 'rangeX.0'
  | 'rangeX.1'
  | 'rangeY.0'
  | 'rangeY.1'
  | 'xAxis.lineWidth'
  | 'xAxis.dashPhase'
  | 'yAxis.lineWidth'
  | 'yAxis.dashPhase'
  | 'xAxis.stroke'
  | 'yAxis.stroke'
  | 'regionOffset.x'
  | 'regionOffset.y'
  | 'pathStyle.lineWidth'
  | 'pathStyle.outlineWidth'
  | 'pathStyle.dashPhase'
  | 'pathStyle.stroke'
  | 'pathStyle.outlineStroke';

範例

const example: AnimationProperty = 'transform.rotation';

Animation

屬性改變時播放動畫。duration/delay 用秒,from/to 設端點,repeat/autoreverse 控制重複,wrap 設週期。

type Animation = {
  property: AnimationProperty;
  duration?: number;
  delay?: number;
  curve?: string;
  from?: number | Color;
  to?: number | Color;
  repeat?: number | 'forever';
  autoreverse?: boolean;
  wrap?: number;
};

範例

const example: Animation = {
  property: 'opacity',
  duration: 0.25,
  curve: 'easeOut',
};

CircleAnimation

圓弧圖形/布局動畫:sweepAngle 不支援 wrap;startAngle 可設 wrap: 360。

type CircleAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'background'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'dashPhase'
        | 'stroke'
        | 'lineWidth'
        | 'startAngle'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'sweepAngle';
      from?: number;
      to?: number;
      wrap?: never;
    });

範例

const example: CircleAnimation = {
  property: 'sweepAngle',
  duration: 0.3,
  from: 0,
  to: 270,
};

EllipseAnimation

圓弧圖形/布局動畫:sweepAngle 不支援 wrap;startAngle 可設 wrap: 360。

type EllipseAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'background'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'dashPhase'
        | 'stroke'
        | 'lineWidth'
        | 'startAngle'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'sweepAngle';
      from?: number;
      to?: number;
      wrap?: never;
    });

範例

const example: EllipseAnimation = {
  property: 'startAngle',
  duration: 0.3,
  wrap: 360,
};

ArcAnimation

圓弧圖形/布局動畫:sweepAngle 不支援 wrap;startAngle 可設 wrap: 360。

type ArcAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'background'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'dashPhase'
        | 'stroke'
        | 'lineWidth'
        | 'startAngle'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'sweepAngle';
      from?: number;
      to?: number;
      wrap?: never;
    });

範例

const example: ArcAnimation = {
  property: 'sweepAngle',
  duration: 0.3,
  from: 0,
  to: -180,
};

MapAnimation

地圖動畫作用於支援的 View 和路線樣式;相機另用 cameraAnimation。

type MapAnimation =
  | (Omit<Animation, 'property' | 'from' | 'to'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'cornerRadius'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY'
        | 'regionOffset.x'
        | 'regionOffset.y'
        | 'pathStyle.lineWidth'
        | 'pathStyle.outlineWidth'
        | 'pathStyle.dashPhase';
      from?: number;
      to?: number;
    })
  | (Omit<Animation, 'property' | 'from' | 'to' | 'wrap'> & {
      property: 'pathStyle.stroke' | 'pathStyle.outlineStroke';
      from?: Color;
      to?: Color;
      wrap?: never;
    });

範例

const example: MapAnimation = {
  property: 'pathStyle.stroke',
  duration: 0.25,
  from: 'red',
  to: 'blue',
};

LineChartAnimation

圖表動畫支援範圍和軸線樣式;範圍不使用 wrap,顏色路徑接受顏色。

type LineChartAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'stroke'
        | 'lineWidth'
        | 'dashPhase'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'rangeX.0' | 'rangeX.1' | 'rangeY.0' | 'rangeY.1';
      from?: number;
      to?: number;
      wrap?: never;
    })
  | (Omit<Animation, 'property' | 'from' | 'to'> & {
      property:
        | 'xAxis.lineWidth'
        | 'xAxis.dashPhase'
        | 'yAxis.lineWidth'
        | 'yAxis.dashPhase';
      from?: number;
      to?: number;
    })
  | (Omit<Animation, 'property' | 'from' | 'to' | 'wrap'> & {
      property: 'xAxis.stroke' | 'yAxis.stroke';
      from?: Color;
      to?: Color;
      wrap?: never;
    });

範例

const example: LineChartAnimation = { property: 'rangeX.1', duration: 0.25 };

BarChartAnimation

圖表動畫支援範圍和軸線樣式;範圍不使用 wrap,顏色路徑接受顏色。

type BarChartAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'background'
        | 'cornerRadius'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'barThickness'
        | 'stroke'
        | 'lineWidth'
        | 'dashPhase'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'rangeY.0' | 'rangeY.1';
      from?: number;
      to?: number;
      wrap?: never;
    })
  | (Omit<Animation, 'property' | 'from' | 'to'> & {
      property:
        | 'xAxis.lineWidth'
        | 'xAxis.dashPhase'
        | 'yAxis.lineWidth'
        | 'yAxis.dashPhase';
      from?: number;
      to?: number;
    })
  | (Omit<Animation, 'property' | 'from' | 'to' | 'wrap'> & {
      property: 'xAxis.stroke' | 'yAxis.stroke';
      from?: Color;
      to?: Color;
      wrap?: never;
    });

範例

const example: BarChartAnimation = { property: 'rangeY.1', duration: 0.25 };

RadialLayoutAnimation

圓弧圖形/布局動畫:sweepAngle 不支援 wrap;startAngle 可設 wrap: 360。

type RadialLayoutAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'padding'
        | 'x'
        | 'y'
        | 'opacity'
        | 'background'
        | 'cornerRadius'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'radius'
        | 'radiusX'
        | 'radiusY'
        | 'startAngle'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'sweepAngle';
      from?: number;
      to?: number;
      wrap?: never;
    });

範例

const example: RadialLayoutAnimation = {
  property: 'startAngle',
  duration: 0.25,
  wrap: 360,
};

SpacerAnimation

Spacer 僅對 width、height、minLength 做布局動畫,沒有視覺特效。

type SpacerAnimation = {
  property: 'width' | 'height' | 'minLength';
  duration?: number;
  delay?: number;
  curve?: string;
  from?: number;
  to?: number;
  repeat?: number | 'forever';
  autoreverse?: boolean;
  wrap?: number;
};

範例

const example: SpacerAnimation = {
  property: 'minLength',
  duration: 0.25,
  from: 8,
  to: 24,
};

ContentAnimation

內容改變時的過渡效果;選擇該 View 支援的 type,duration/delay 用秒。

type ContentAnimation = {
  type: 'fade' | 'slide' | 'scale' | 'numericText' | 'interpolate';
  duration?: number;
  delay?: number;
  curve?: string;
  direction?: string;
  distance?: number;
  scale?: number;
};

範例

const example: ContentAnimation = { type: 'numericText', duration: 0.18 };

ViewSize

測量後的 width 和 height,單位為螢幕點。

type ViewSize = { width: number; height: number };

範例

const example: ViewSize = { width: 240, height: 140 };

ViewInsets

top、leading、bottom、trailing 四側內距,單位為點。

type ViewInsets = {
  top: number;
  leading: number;
  bottom: number;
  trailing: number;
};

範例

const example: ViewInsets = {
  top: 24,
  leading: 12,
  bottom: 12,
  trailing: 12,
};

CardContentMetrics

傳給 renderer 的 size、contentSize、headerSize、safeInsets,用於避開標題安排正文。

type CardContentMetrics = {
  size: ViewSize;
  contentSize: ViewSize;
  headerSize: ViewSize;
  safeInsets: ViewInsets;
};

範例

const example: CardContentMetrics = {
  size: { width: 240, height: 160 },
  contentSize: { width: 216, height: 124 },
  headerSize: { width: 216, height: 24 },
  safeInsets: { top: 24, leading: 12, bottom: 12, trailing: 12 },
};