Communitygithub.com

vibe-motion/skills

Remotion プロジェクトでアニメーションする金融用Kライン図(キャンドルチャート)を作成します。データ取得(Yahoo Finance)、プラグインアーキテクチャ(config / timeline / Scene / plugin)、Canvas フレーム描画、ダークなトレーディング端末スタイル、Remotion Studio プレビューとレンダリング出力を網羅します。トリガーワード:K線図、キャンドルチャート、candlestick、OHLC、株アニメーション、金融チャート動画、Remotion チャート。

skills とは?

このスキルは、vibe-motion-app という Remotion プロジェクト上で、S&P 500 のKライン図(キャンドルチャート)をアニメーション作成するための完全な手順を提供します。Yahoo Finance から OHLC データを取得し、Remotion のプラグインアーキテクチャ(config、timeline、Scene、plugin)を使って、<canvas>上で1フレームごとに描画します。ダークなトレーディング端末風のデザインで、フェードイン&下から生えるアニメーションを実装し、Remotion Studio でリアルタイムプレビューやパラメーター調整が可能です。プラグインパターンに従えば、任意の金融データや他のチャートスタイルにも容易に拡張できます。

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/vibe-motion/skills/tree/HEAD/remotion-candlestick

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

Remotion キャンドルチャート (Candlestick) アニメーション制作フロー

本スキルは、vibe-motion-app という Remotion プロジェクトで S&P 500 Kライン図を作成する完全な制作手順を記録しています。

前提条件

  • Remotion 4.0.438 プロジェクト、React 19 + TailwindCSS v4 が設定済みであること
  • プラグインアーキテクチャ:motion/ でシーンを定義、remotion/ で Remotion 合成パイプラインを処理
  • pnpm をパッケージマネージャーとして使用

ステップ 1:金融データの取得

Yahoo Finance は無料の過去 OHLC データを提供します(API Key 不要)。

scripts/fetch-sp500-data.mjs を作成:

// コア:Yahoo Finance Chart API を呼び出す
const url = `https://query1.finance.yahoo.com/v8/finance/chart/%5EGSPC?period1=${PERIOD1}&period2=${PERIOD2}&interval=1wk`;
const resp = await fetch(url);
const json = await resp.json();
const result = json.chart?.result?.[0];
const ts = result.timestamp;
const quote = result.indicators.quote[0];
// 抽出 { t: timestamp, o: open, h: high, l: low, c: close }

public/data/sp500-weekly.json に保存し、Remotion から staticFile() で読み込みます。

パラメーター説明:

  • %5EGSPC = S&P 500 の Yahoo シンボル(^GSPC の URL エンコード)
  • interval=1wk = 週足;オプション:1d, 1mo
  • period1 / period2 = Unix タイムスタンプ、約40年をカバー(1984-2026)

ステップ 2:プラグインファイル構造の作成

Remotion プラグインごとに、以下の4つのコアファイル+レンダリングプリセットが必要です:

motion/プラグイン名/
  config.js                 → デフォルトパラメーター
  timeline.js               → シーンコンテキスト+フレームごとのアニメーション計算
  Scene.jsx                 → React コンポーネント(Canvas 描画)
  plugin.js                 → プラグイン登録オブジェクト
  render-presets/
    default.json            → レンダリングプリセット

2a. config.js — デフォルトパラメーター

export const DEFAULT_LAYOUT_PARAMS = Object.freeze({
  videoWidth: 1920,    // 16:9 横画面
  videoHeight: 1080,
});

export const DEFAULT_ANIMATION_PARAMS = Object.freeze({
  durationSeconds: 28,  // アニメーション総時間
  title: "S&P 500",
  subtitle: "過去40年・週足Kライン",
});

export const DEFAULT_MOTION_PROPS = Object.freeze({
  ...DEFAULT_LAYOUT_PARAMS,
  ...DEFAULT_ANIMATION_PARAMS,
});

2b. timeline.js — アニメーションロジック

3つのコア関数を実装する必要があります:

// 1. シーンコンテキスト:すべてのパラメーターを検証・強制型変換
import { resolveXxxSceneContext } from "./timeline.js";
export const resolveXxxSceneContext = (pluginParams) => { ... }

// 2. フレーム数計算:durationSeconds * fps
import { getXxxDurationInFrames } from "./timeline.js";
export const getXxxDurationInFrames = ({ fps, sceneContext }) => { ... }

// 3. フレームごとの props:frame/fps から progress などのアニメーション値を計算
import { buildXxxSceneProps } from "./timeline.js";
export const buildXxxSceneProps = ({ frame, fps, loop, sceneContext }) => { ... }

重要な設計原則:すべてのアニメーション状態は frame パラメーターのみに依存し、Remotion の並列レンダリング要件を満たします。

2c. Scene.jsx — Canvas 描画コンポーネント

大量の DOM 要素ではなく <canvas> を使用することで、パフォーマンスが向上します。

データ読み込みパターン:

const [dataHandle] = useState(() => delayRender("データ読み込み中"));

useEffect(() => {
  fetch(staticFile("data/xxx.json"))
    .then(r => r.json())
    .then(data => { setData(data); continueRender(dataHandle); })
    .catch(err => cancelRender(err));
}, [dataHandle]);

Canvas 描画コアロジック:

const draw = useCallback(() => {
  const ctx = canvas.getContext("2d");
  const dpr = window.devicePixelRatio || 1;

  // 高DPI画面対応
  canvas.width = W * dpr;
  canvas.height = H * dpr;
  ctx.setTransform(dpr, 0, 0, dpr, 0, 0);

  // 1. 背景塗りつぶし
  // 2. グリッド線 + Y軸価格目盛
  // 3. キャンドル描画(wick + body)
  // 4. X軸年ラベル
  // 5. タイトル/サブタイトル/統計情報
  // 6. プログレスバー
}, [data, progress, ...]);

useEffect(() => { draw(); }, [draw]);

キャンドル描画の重要パラメーター:

const CANDLE_GAP_RATIO = 0.28;  // キャンドル間隔比率
const CANDLE_FADE_FRAMES = 22;  // 出現アニメーションフレーム数
const MAX_CANDLE_W = 8;         // 最大キャンドル幅(スケール後)
const MIN_CANDLE_W = 0.6;       // 最小キャンドル幅

// キャンドル幅は表示数に応じて自動調整
const slotW = plotWidth / visibleCount;
const bodyW = Math.max(MIN_CANDLE_W, Math.min(MAX_CANDLE_W * scale, slotW * (1 - GAP_RATIO)));

出現アニメーション(フェードイン+下から生える):

const appearFrame = (i / (N - 1)) * revealEndFrame;
const candleAge = currentFrame - appearFrame;
const fadeAlpha = Math.min(1, Math.max(0, candleAge / CANDLE_FADE_FRAMES));

// 成長アニメーション
if (candleAge < CANDLE_FADE_FRAMES && candleAge > 0) {
  const growT = easeOutCubic(candleAge / CANDLE_FADE_FRAMES);
  displayBodyBottom = lerp(lowY, bodyBottom, growT);
}

ctx.globalAlpha = fadeAlpha;
// ... wick + body を描画 ...
ctx.globalAlpha = 1;

ダークテーマカラー:

const COLORS = {
  bg: "#1a1a19",
  grid: "#2c2c2a",
  upBorder: "#e66767",       // 陽線枠線(赤)
  upFill: "rgba(230,103,103,0.15)",  // 陽線塗り(半透明)
  downFill: "#199e70",        // 陰線塗り(緑)
  textPrimary: "#ffffff",
  textSecondary: "#c3c2b7",
  textMuted: "#898781",
  accent: "#3987e5",
};

2d. plugin.js — プラグイン登録

export const xxxPlugin = Object.freeze({
  id: "プラグインID",
  defaultProps: DEFAULT_MOTION_PROPS,
  SceneComponent: XxxScene,
  waitForSceneLayoutReady: true,  // データ読み込み完了を待つ

  resolveSceneContext: (params) => ...,
  getDurationInFrames: ({ fps, sceneContext }) => ...,
  buildSceneProps: ({ frame, fps, loop, sceneContext }) => ...,
  getLayout: ({ sceneContext }) => sceneContext.layout,
});

waitForSceneLayoutReady: true により、Scene コンポーネントが onAutoLayoutReady() を呼び出すまでフレームのレンダリングを待機します。

2e. render-presets/default.json

{
  "title": "S&P 500",
  "subtitle": "過去40年・週足Kライン",
  "videoWidth": 1920,
  "videoHeight": 1080,
  "durationSeconds": 28
}

ステップ 3:合成の登録

motion/project.js を修正し、ACTIVE_COMPOSITION を新しいプラグインに切り替えます:

import { xxxPlugin } from "./xxx/plugin.js";

export const ACTIVE_COMPOSITION = Object.freeze({
  id: "SP500Kline",
  fps: 30,
  plugin: xxxPlugin,
});

ステップ 4:検証とレンダリング

# 合成の登録を確認
pnpm run remotion:compositions

# Studio を起動(タイムラインのドラッグ、パラメーター調整に対応)
pnpm run dev

# 単一フレームのスクリーンショットをレンダリング
npx remotion still SP500Kline out/frame.png --frame=420

# 完全な動画をレンダリング
npx remotion render SP500Kline out/output.mp4

# プリセットを使用してレンダリング
REMOTION_PROPS_FILE=motion/xxx/render-presets/default.json pnpm run remotion:render

よくある問題

Yahoo Finance API の CORS 問題

Remotion のレンダリング環境(Node.js ヘッドレスブラウザ)では CORS 問題は発生しません。 ブラウザプレビューで CORS が発生する場合は、staticFile() を使用して事前に取得したローカルの JSON を読み込んでください。

Canvas が高 DPI 画面でぼやける

常に devicePixelRatio を掛けてください:

canvas.width = W * dpr;
canvas.height = H * dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);

キャンドル数が多すぎて幅が小さくなる

最小幅 0.6px を設定し、slotW < 1 の場合は間隔比を縮小するかデータを集約してください。

アニメーション進捗の制御

区間ごとのイージング関数を使用します:

  • 開始 3% でベースラインを素早く確立
  • 中間 90% で一定速度で表示
  • 最後は若干減速してドラマ性を強調

Individual skills in this repo

This repo contains 4 individual skills — each has its own dedicated page.

関連スキル