データ・プロパティ・コンポーネント
データを事前に読み込み、設定を公開し、JSON からコンポーネントを再利用します。
描画前にデータを読み込む
各フレームは同期的に描画するため、コンポーネントでファイルや通信を待てません。代わりに async prepare() エクスポートを使います。プレビューも書き出しも最初のフレーム前に一度だけ実行されます。結果をモジュールの変数に保存し、描画中に読み取ります。
let headlines: string[] = ['Hello from Celesta'];
export async function prepare() {
try {
const response = await fetch('https://example.com/headlines.json');
headlines = (await response.json()) as string[];
} catch (error) {
console.error('Using the fallback headline', error);
}
}
オフラインでも描画できるよう既定値を用意します。同じ理由で loadLipSync()、loadPsdPreset()、preloadMedia() も prepare() 内で実行します。
データと props でコンポーネントを再利用する
設定は通常のオブジェクトや読み込んだ JSON データに置き、React コンポーネントの props として渡します。データ用の JSON は、.celesta.json のタイムライン形式とは別のものです。props に型を付けると、エディタで使い方を確認できます。
import { Composition, Text } from '@celesta.studio/react';
const settings = { title: 'Chapter 3', accent: '#a68bbf' };
type TitleProps = { title: string; accent: string };
function Title({ title, accent }: TitleProps) {
return (
<Text x={960} y={540} anchorX={0.5} anchorY={0.5}
style={{ fontSize: 96, fill: { type: 'solid', color: accent } }}>
{title}
</Text>
);
}
export default function Root() {
return (
<Composition width={1920} height={1080} fps={30} durationInFrames={90}>
<Title {...settings} />
</Composition>
);
}
コードを変えずにパレットを切り替える
パレットは、フィルムの色を 4 つの役割に分けます。ground はいちばん後ろに敷く地の色、main は主役の色、second は文字の色、accent はところどころに差す色です。少し明るいカードや半透明の線のように役割から作る色は derived に一度だけ名前を付けておけば、パレットごとにそのパレットの色から作られます。役割と派生色の名前だけで描いておけば、パレットを替えるだけでフィルム全体の色が変わります。
import {
Composition, Rect, Text, Theme, alpha, defineProjectProperties, definePalettes, getProjectProperty, mix,
} from '@celesta.studio/react';
const palettes = definePalettes({
harbor: { ground: '#0F2A3D', main: '#5FD3B5', second: '#EAF4F2', accent: '#F2B544' },
paper: { ground: '#F4F1EA', main: '#1F4E79', second: '#111111', accent: '#D93A12' },
}, {
derived: {
card: mix('ground', 'main', 0.14), // ground 86%、main 14%
veil: alpha('second', 0.2), // second を不透明度 20% で
},
});
defineProjectProperties({ palette: palettes.property({ label: 'Palette' }) });
export default function Film() {
return (
<Composition width={1920} height={1080} fps={30} durationInFrames={90}>
<Theme palette={palettes.get(getProjectProperty('palette'))}>
<Rect width={1920} height={1080} fill="ground" />
<Rect x={160} y={160} width={1600} height={760} fill="card" stroke="veil" strokeWidth={2} />
<Text x={240} y={240} fontSize={96} fontWeight={700} color="second">Harbor report</Text>
<Rect x={240} y={380} width={320} height={12} fill="accent" />
</Theme>
</Composition>
);
}
<Theme palette>は、役割と派生色を<Theme>の色名として使えるようにします。<Text color>、図形のfillとstroke、@celesta.studio/componentsの色の props など、<Theme>の色名が使えるところならどこでも使えます。一緒に渡したcolorsはパレットの色より優先され、内側の<Theme>はこれまでどおり名前を足します。いま効いているパレットはuseTheme().paletteで取れます。mix(a, b, amount)はaからbへamountだけ寄せた色で、CSS のcolor-mix(in srgb, …)と同じです。alpha(color, amount)は色を変えずに不透明度をamount倍にします。どちらも、役割、それより上で定義した派生色、hex の色を受け取ります。palettes.property()はパレット名を選択肢にしたメニューのプロパティです。プロパティパネル、エントリと同じフォルダーのprops.json、エクスポーターの--props '{"palette":"paper"}'や--props-fileでパレットを選べます。パレットにない名前は最初のフレームより前に弾かれます。- 役割はどれも
#RRGGBBか#RRGGBBAAの色にします。そうでなければdefinePalettes()が例外を投げます。groundに対するsecondのコントラストが文字に足りない 4.5:1 未満のときは、パレット名と比率を書いた警告を出します。同じ指摘はpalettes.check()でも受け取れます。