Biznomad - 影片滾動動畫網站(Apple 等級互動式說故事)
這項技能提供了完整的架構模式、FFmpeg 壓縮流程與生產級 JS/CSS 程式碼,用於建構符合 Apple、Stripe、Lusion 及 Awwwards 每日網站標準的高效能影片滾動動畫網站。
產業研究與架構解析
在打造以滾動驅動的影片體驗時,頂尖科技與設計公司會從三種主要渲染策略中選擇:
| 策略 | 業界參考 | 最佳應用場景 | 優點 | 缺點 |
|---|---|---|---|---|
| Canvas 影像序列 | Apple(MacBook、AirPods、iPhone) | 精確搓洗、零延遲、完美幀率 | 60–120fps 流暢平滑,適用於所有行動 GPU | 需要預載幀(20–60MB 陣列) |
| ALL-I 編碼 HTML5 影片 | Stripe、Nike、Tesla | 快速設定、單一檔案低頻寬 | 單一 MP4 下載,初始化快速 | 需要低關鍵幀間距(-g 1) |
| WebGL 紋理著色器 | Lusion、Active Theory、Cuberto | 3D 景深扭曲、液體玻璃效果 | 超高創意性,可控制光線與折射 | GPU 負載較高,著色器複雜度高 |
1. 影片準備與 FFmpeg 編碼管線
為實現即時、流暢且無停頓的影片搓洗,影片必須以關鍵幀距離為 1(每個影格都是 Intra-frame / 關鍵幀)進行編碼。
選項 A:為 HTML5 影片搓洗準備影片(ALL-I 編碼)
執行此 FFmpeg 指令以產生超快速搓洗的 MP4:
ffmpeg -i input.mp4 -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2" -c:v libx264 -preset slow -crf 18 -g 1 -keyint_min 1 -an -movflags +faststart output_scrub.mp4
選項 B:為 Canvas 搓洗準備幀序列(Apple 風格)
提取壓縮後的 1080p WebP 或 JPEG 幀:
mkdir -p frames
ffmpeg -i input.mp4 -vf "scale=1920:1080" -q:v 85 frames/frame_%04d.jpg
# 或使用 WebP(檔案更小):
ffmpeg -i input.mp4 -vf "scale=1920:1080" -c:v libwebp -quality 85 frames/frame_%04d.webp
2. 技術棧配置
- 平滑滾動引擎:
@studio-freight/lenis或Lenis(消除抖動並提供慣性滾動) - 動畫驅動器:
GSAP 3.12+與ScrollTrigger - 渲染層:使用 2D 環境的
<canvas>,或同步currentTime的<video> - 排版與 HUD:固定式毛玻璃疊加卡片,與幀時間軸觸發器同步
3. 生產架構:Apple 風格 Canvas 序列(推薦)
以下是 Apple 風格 Canvas 幀滾動序列的生產級實作,包含預載器、流暢的 Lenis 滾動以及浮動的互動式說故事文字層。
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Apple 風格影片滾動展示</title>
<!-- 字型及 GSAP + Lenis CDN -->
<link rel="preconnect" href="https://fonts.googleapis.com"/>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800;900&family=JetBrains+Mono:wght@500;700&display=swap" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/lenis.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/ScrollTrigger.min.js"></script>
<style>
:root {
--bg: #000000;
--paper: #f5f5f7;
--dim: #86868b;
--accent: #2997ff;
--accent-glow: rgba(41, 151, 255, 0.35);
--panel: rgba(22, 22, 23, 0.75);
--line: rgba(255, 255, 255, 0.12);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: var(--bg);
color: var(--paper);
font-family: "Inter", system-ui, -apple-system, sans-serif;
overflow-x: hidden;
-webkit-font-smoothing: antialiased;
}
/* 預載器 */
#preloader {
position: fixed;
inset: 0;
z-index: 999;
background: #000;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 16px;
transition: opacity 0.6s ease, visibility 0.6s ease;
}
#preloader.done { opacity: 0; visibility: hidden; }
.loader-bar {
width: 200px;
height: 3px;
background: rgba(255, 255, 255, 0.15);
border-radius: 999px;
overflow: hidden;
}
.loader-fill {
height: 100%;
width: 0%;
background: var(--accent);
transition: width 0.1s linear;
}
/* 固定滾動 Canvas 舞台 */
.scroll-stage {
position: relative;
height: 400vh; /* 決定滾動互動的長度 */
}
.sticky-viewport {
position: sticky;
top: 0;
width: 100vw;
height: 100vh;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
}
canvas#scrollCanvas {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
/* 浮動互動式說故事疊加面板 */
.hud-overlay {
position: absolute;
inset: 0;
pointer-events: none;
z-index: 10;
}
.hud-card {
position: absolute;
max-width: 460px;
background: var(--panel);
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
border: 1px solid var(--line);
border-radius: 20px;
padding: 32px;
opacity: 0;
transform: translateY(30px);
transition: opacity 0.5s ease, transform 0.5s ease;
}
.hud-card.active {
opacity: 1;
transform: translateY(0);
}
.hud-eyebrow {
font-family: "JetBrains Mono", monospace;
color: var(--accent);
font-size: 12px;
letter-spacing: 0.15em;
text-transform: uppercase;
font-weight: 700;
margin-bottom: 8px;
}
.hud-title {
font-size: 32px;
font-weight: 800;
letter-spacing: -0.03em;
line-height: 1.1;
margin-bottom: 12px;
}
.hud-desc {
color: var(--dim);
font-size: 15px;
line-height: 1.6;
}
/* 個別 HUD 位置 */
.hud-1 { top: 15%; left: 8%; }
.hud-2 { top: 35%; right: 8%; }
.hud-3 { bottom: 15%; left: 8%; }
/* 結尾區塊 */
.outro-sec {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
background: #08080a;
border-top: 1px solid var(--line);
padding: 80px 24px;
}
.outro-title {
font-size: clamp(40px, 6vw, 84px);
font-weight: 900;
letter-spacing: -0.04em;
margin-bottom: 24px;
}
.btn-cta {
display: inline-block;
background: var(--accent);
color: #fff;
font-weight: 700;
padding: 16px 36px;
border-radius: 999px;
text-decoration: none;
box-shadow: 0 10px 30px var(--accent-glow);
transition: transform 0.2s;
}
.btn-cta:hover { transform: scale(1.05); }
</style>
</head>
<body>
<!-- 預載器 -->
<div id="preloader">
<div style="font-weight:700;font-size:14px;letter-spacing:0.1em">載入體驗中</div>
<div class="loader-bar"><div class="loader-fill" id="loaderFill"></div></div>
</div>
<!-- 滾動影片容器 -->
<div class="scroll-stage" id="scrollStage">
<div class="sticky-viewport">
<canvas id="scrollCanvas"></canvas>
<!-- 互動式說故事 HUD 疊加層 -->
<div class="hud-overlay">
<div class="hud-card hud-1" id="hud1">
<div class="hud-eyebrow">01 / 架構</div>
<h2 class="hud-title">精密工程。</h2>
<p class="hud-desc">每個影格即時運算。無與倫比的視覺速度與流暢反應。</p>
</div>
<div class="hud-card hud-2" id="hud2">
<div class="hud-eyebrow">02 / 遙測</div>
<h2 class="hud-title">即時數據流。</h2>
<p class="hud-desc">使用者滾動輸入與 Canvas 呈現之間亞毫秒級的同步。</p>
</div>
<div class="hud-card hud-3" id="hud3">
<div class="hud-eyebrow">03 / 效能</div>
<h2 class="hud-title">120 FPS 精通。</h2>
<p class="hud-desc">GPU 加速 Canvas 渲染,零影格丟失或滾動卡頓。</p>
</div>
</div>
</div>
</div>
<section class="outro-sec">
<div>
<h1 class="outro-title">準備啟動。</h1>
<a href="#" class="btn-cta">體驗未來 →</a>
</div>
</section>
<script>
document.addEventListener('DOMContentLoaded', () => {
// 1. 初始化 Lenis 平滑滾動
const lenis = new Lenis({
duration: 1.2,
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
smoothWheel: true
});
function raf(time) {
lenis.raf(time);
requestAnimationFrame(raf);
}
requestAnimationFrame(raf);
// 將 Lenis 與 GSAP ScrollTrigger 同步
lenis.on('scroll', ScrollTrigger.update);
gsap.ticker.add((time) => lenis.raf(time * 1000));
gsap.ticker.lagSmoothing(0);
// 2. 設定 Canvas 與影像預載器
const canvas = document.getElementById('scrollCanvas');
const ctx = canvas.getContext('2d');
const loaderFill = document.getElementById('loaderFill');
const preloader = document.getElementById('preloader');
const totalFrames = 120; // 提取的影格總數
const images = [];
const airbnb = { frame: 0 };
// 影格 URL 產生器(請替換為您的相對路徑)
const currentFrame = (index) => `./frames/frame_${(index + 1).toString().padStart(4, '0')}.jpg`;
let loadedCount = 0;
for (let i = 0; i < totalFrames; i++) {
const img = new Image();
img.src = currentFrame(i);
img.onload = () => {
loadedCount++;
loaderFill.style.width = `${(loadedCount / totalFrames) * 100}%`;
if (loadedCount === totalFrames) {
preloader.classList.add('done');
renderCanvas();
}
};
images.push(img);
}
// 處理高 DPI Retina 顯示器
function resizeCanvas() {
const dpr = window.devicePixelRatio || 1;
canvas.width = window.innerWidth * dpr;
canvas.height = window.innerHeight * dpr;
ctx.scale(dpr, dpr);
renderCanvas();
}
window.addEventListener('resize', resizeCanvas);
function renderCanvas() {
if (!images[airbnb.frame]) return;
const img = images[airbnb.frame];
const w = window.innerWidth;
const h = window.innerHeight;
// 封面適應數學
const imgRatio = img.width / img.height;
const canvasRatio = w / h;
let drawW, drawH, offsetX, offsetY;
if (canvasRatio > imgRatio) {
drawW = w;
drawH = w / imgRatio;
offsetX = 0;
offsetY = (h - drawH) / 2;
} else {
drawW = h * imgRatio;
drawH = h;
offsetX = (w - drawW) / 2;
offsetY = 0;
}
ctx.clearRect(0, 0, w, h);
ctx.drawImage(img, offsetX, offsetY, drawW, drawH);
}
// 初始 Canvas 大小調整
resizeCanvas();
// 3. GSAP ScrollTrigger 序列
gsap.to(airbnb, {
frame: totalFrames - 1,
snap: "frame",
ease: "none",
scrollTrigger: {
trigger: "#scrollStage",
start: "top top",
end: "bottom bottom",
scrub: 0.5,
onUpdate: () => renderCanvas()
}
});
// 4. 互動式說故事 HUD 疊加觸發器
function setupHudTrigger(id, startPct, endPct) {
ScrollTrigger.create({
trigger: "#scrollStage",
start: `${startPct}% top`,
end: `${endPct}% top`,
onEnter: () => document.getElementById(id).classList.add('active'),
onLeave: () => document.getElementById(id).classList.remove('active'),
onEnterBack: () => document.getElementById(id).classList.add('active'),
onLeaveBack: () => document.getElementById(id).classList.remove('active')
});
}
setupHudTrigger('hud1', 10, 30);
setupHudTrigger('hud2', 35, 60);
setupHudTrigger('hud3', 65, 90);
});
</script>
</body>
</html>
4. 替代架構:直接 HTML5 影片搓洗
針對使用單一壓縮 .mp4 影片(採用 Intra-frame 編碼 -g 1)的專案:
const video = document.getElementById('scrubVideo');
video.pause();
// 確保已載入中繼資料以取得影片長度
video.onloadedmetadata = function() {
gsap.to(video, {
currentTime: video.duration,
ease: "none",
scrollTrigger: {
trigger: "#videoStage",
start: "top top",
end: "bottom bottom",
scrub: 0.3
}
});
};
5. Apple 等級品質的黃金法則
- Intra-frame 關鍵幀 (
-g 1):HTML5 影片搓洗的必要條件,否則在關鍵幀搜尋間隔時會發生卡頓。 - 裝置像素比 (DPR):務必將 Canvas 解析度乘以
window.devicePixelRatio,以獲得銳利的 Retina 輸出。 - 長寬比封面適應計算:在 Canvas 尺寸調整時計算
offsetX與offsetY,使每個影格保持滿版物件適應覆蓋。 - Lenis 滾動插值:絕不要省略平滑滾動慣性(
Lenis)。純原生的滾輪事件會產生離散步驟,從而降低影片搓洗品質。 - 行動裝置效能降級:在低功耗行動裝置上(
matchMedia('(prefers-reduced-motion: reduce)')或低頻寬),提供自動影片播放或靜態海報幀,以取代繁重的搓洗運算。