Tài liệu này hướng dẫn cách thêm hiệu ứng visual mới vào dự án. Dự án sử dụng một hệ thống animation module hóa, hỗ trợ cả Three.js và Canvas 2D.
/assets/js/three.min.jsHệ thống animation được tổ chức theo kiến trúc module như sau:
assets/
├── js/
│ ├── three.min.js # Three.js local library (r128)
│ ├── animations/
│ │ ├── animation-utils.js # Tiện ích và cấu hình dùng chung
│ │ ├── animation-manager.js # Quản lý animation module
│ │ ├── chapter1-interference.js
│ │ ├── chapter2-tessellation.js
│ │ ├── chapter3-binaryflow.js
│ │ ├── chapter4-verticalbars.js
│ │ ├── chapter5-emptyvessel.js
│ │ ├── chapter6-particleflower.js
│ │ ├── chapter7-pinecone.js
│ │ ├── chapter8-waterascii.js
│ │ ├── chapter9-canyonflows.js
│ │ ├── chapter10-yinyang.js
│ │ ├── chapter11-voidarchitecture.js
│ │ ├── chapter12-metamorphosis.js
│ │ ├── chapter13-doublehelix.js
│ │ ├── chapter14-formlesswaves.js
│ │ ├── chapter15-watchfulwaves.js
│ │ ├── chapter16-morphingcontours.js
│ │ ├── chapter17-kaleidoscope.js
│ │ └── chapter18-hasharchitecture.js
│ │ ├── chapter19-vortexparticles.js
│ │ ├── chapter20-flowingpaths.js
│ │ ├── chapter21-bouncingpointcloud.js
│ └── animations-import.js # Bridge giữa module và global scope
│ ├── metadata/
│ │ ├── chapter1-metadata.js
│ │ ├── chapter2-metadata.js
│ │ ├── chapter3-metadata.js
│ │ └── index.js
│ ├── loaders/
│ │ └── index.js
loaders/index.js)metadata/index.js// assets/js/animations/chapter-X-name.js
import animationUtils from './animation-utils.js';
const chapterXAnimation = {
// Thiết lập riêng cho animation này
settings: {
colors: animationUtils.colors, // Sử dụng màu dùng chung
// Các thiết lập khác cho chapter
},
init(container) {
// Khởi tạo animation
if (!container) return null;
// Hiển thị loader trong quá trình thiết lập
const loader = container.querySelector('.animation-loader');
// Thiết lập animation
// ...
// Ẩn loader khi animation đã sẵn sàng
animationUtils.fadeOutLoader(container);
// Trả về object có hàm cleanup
return {
cleanup: () => {
// Dọn dẹp tài nguyên
}
};
}
};
export default chapterXAnimation;
assets/js/animations/metadata/chapterX-metadata.js:
export default {
name: "animationName",
themes: "Theme 1, Theme 2, ...",
visualization: "Mô tả visualization",
description: "Mô tả chi tiết về ý nghĩa của animation"
};
assets/js/animations/metadata/index.js:
import chapterX from './chapterX-metadata.js';
export default {
...existing code...
X: chapterX,
};
assets/js/animations/loaders/index.js:
animationName: () => import('../chapterX-animationfile.js'),
// Trong main.js, thêm điều kiện cho chapter mới
if (data.id === 1 || data.id === 2 || data.id === X) { // Thêm số chapter của bạn
contentHTML += `
<div class="animation-container" id="animation-container">
<div class="animation-loader">
<div>
<span class="loading-text">Đang tải hiệu ứng visual...</span>
<noscript>JavaScript cần được bật để hiển thị hiệu ứng</noscript>
</div>
</div>
</div>
`;
}
Khi bạn có một component React (ví dụ từ thư mục raw_animation/) và muốn chuyển đổi thành module JavaScript cho dự án, hãy làm theo các bước sau:
// Từ React props
const initialZoom = 6;
// Thành
settings: {
initialZoom: 6,
waveSourceCount: 5,
resolution: 32,
colors: animationUtils.colors,
lineOpacity: 0.4
}
// Từ React useEffect
useEffect(() => {
if (!containerRef.current) return;
// ...code khởi tạo
}, []);
// Thành
init(container) {
if (!container) return null;
// ...code khởi tạo giữ nguyên
}
// Từ React cleanup
return () => {
// ...code dọn dẹp
};
// Thành
return {
cleanup: () => {
// ...code dọn dẹp giữ nguyên
}
};
// Từ React inline styles
style=
// Thành CSS trong animations.css
.animation-container {
margin: 0;
background: #F0EEE6;
/* ... */
}
// Cuối file
export default chapterAnimation;
animation-utils.jsimport và export đúng cú phápanimation-manager.js