Create static/js/solar_animation.js
Browse files- static/js/solar_animation.js +143 -0
static/js/solar_animation.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// solar_animation.js
|
| 2 |
+
|
| 3 |
+
const canvas = document.getElementById('solarCanvas');
|
| 4 |
+
const ctx = canvas.getContext('2d');
|
| 5 |
+
|
| 6 |
+
let animationFrameId;
|
| 7 |
+
let energyFlowOffset = 0; // Dịch chuyển để tạo hiệu ứng dòng chảy
|
| 8 |
+
|
| 9 |
+
// Kích thước và vị trí các thành phần
|
| 10 |
+
const solarPanel = { x: 50, y: 50, width: 100, height: 80 };
|
| 11 |
+
const inverter = { x: 250, y: 150, width: 80, height: 60 };
|
| 12 |
+
const battery = { x: 450, y: 250, width: 70, height: 100 };
|
| 13 |
+
const homeLoad = { x: 650, y: 150, width: 80, height: 70 };
|
| 14 |
+
|
| 15 |
+
// Định nghĩa màu sắc và thuộc tính
|
| 16 |
+
const colors = {
|
| 17 |
+
solar: '#FFD700', // Vàng năng lượng mặt trời
|
| 18 |
+
inverter: '#4CAF50', // Xanh lá cây
|
| 19 |
+
battery: '#ADD8E6', // Xanh dương nhạt
|
| 20 |
+
home: '#FF4500', // Đỏ cam
|
| 21 |
+
flow: '#FFA500', // Cam cho dòng năng lượng
|
| 22 |
+
text: '#333333'
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
// Hàm vẽ một hộp (thành phần)
|
| 26 |
+
function drawComponent(comp, label, color) {
|
| 27 |
+
ctx.fillStyle = color;
|
| 28 |
+
ctx.fillRect(comp.x, comp.y, comp.width, comp.height);
|
| 29 |
+
ctx.strokeStyle = '#333';
|
| 30 |
+
ctx.lineWidth = 2;
|
| 31 |
+
ctx.strokeRect(comp.x, comp.y, comp.width, comp.height);
|
| 32 |
+
|
| 33 |
+
ctx.fillStyle = colors.text;
|
| 34 |
+
ctx.font = '14px Arial';
|
| 35 |
+
ctx.textAlign = 'center';
|
| 36 |
+
ctx.fillText(label, comp.x + comp.width / 2, comp.y + comp.height + 20);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
// Hàm vẽ dòng năng lượng
|
| 40 |
+
function drawFlow(startX, startY, endX, endY, direction = 'right', speed = 5) {
|
| 41 |
+
ctx.strokeStyle = colors.flow;
|
| 42 |
+
ctx.lineWidth = 4;
|
| 43 |
+
ctx.lineCap = 'round'; // Để đầu mũi tên tròn hơn
|
| 44 |
+
|
| 45 |
+
ctx.beginPath();
|
| 46 |
+
ctx.moveTo(startX, startY);
|
| 47 |
+
ctx.lineTo(endX, endY);
|
| 48 |
+
ctx.stroke();
|
| 49 |
+
|
| 50 |
+
// Vẽ hiệu ứng "dòng chảy"
|
| 51 |
+
ctx.save(); // Lưu trạng thái canvas
|
| 52 |
+
ctx.clip(); // Clip để chỉ vẽ trong đường đã vẽ
|
| 53 |
+
|
| 54 |
+
ctx.strokeStyle = 'rgba(255, 255, 255, 0.7)'; // Màu sáng cho hiệu ứng dòng chảy
|
| 55 |
+
ctx.lineWidth = 2;
|
| 56 |
+
|
| 57 |
+
const numDots = 10;
|
| 58 |
+
const lineLength = Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2));
|
| 59 |
+
const dotSpacing = lineLength / numDots;
|
| 60 |
+
|
| 61 |
+
for (let i = 0; i < numDots; i++) {
|
| 62 |
+
let currentPos = (energyFlowOffset + i * dotSpacing) % lineLength;
|
| 63 |
+
if (currentPos < 0) currentPos += lineLength; // Xử lý khi offset âm
|
| 64 |
+
|
| 65 |
+
let ratio = currentPos / lineLength;
|
| 66 |
+
let x = startX + (endX - startX) * ratio;
|
| 67 |
+
let y = startY + (endY - startY) * ratio;
|
| 68 |
+
|
| 69 |
+
ctx.beginPath();
|
| 70 |
+
if (direction === 'right' || direction === 'down') {
|
| 71 |
+
ctx.arc(x, y, 3, 0, Math.PI * 2); // Vẽ chấm tròn
|
| 72 |
+
} else { // Ví dụ: dòng đi lên hoặc sang trái
|
| 73 |
+
ctx.arc(x, y, 3, 0, Math.PI * 2); // Vẽ chấm tròn
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
ctx.fill();
|
| 77 |
+
}
|
| 78 |
+
ctx.restore(); // Khôi phục trạng thái canvas
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
// Hàm chính để vẽ tất cả
|
| 83 |
+
function draw() {
|
| 84 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height); // Xóa canvas
|
| 85 |
+
|
| 86 |
+
// Vẽ các thành phần
|
| 87 |
+
drawComponent(solarPanel, 'Tấm pin', colors.solar);
|
| 88 |
+
drawComponent(inverter, 'Biến tần', colors.inverter);
|
| 89 |
+
drawComponent(battery, 'Pin lưu trữ', colors.battery);
|
| 90 |
+
drawComponent(homeLoad, 'Tải nhà', colors.home);
|
| 91 |
+
|
| 92 |
+
// Vẽ luồng năng lượng
|
| 93 |
+
// Pin -> Biến tần
|
| 94 |
+
drawFlow(
|
| 95 |
+
solarPanel.x + solarPanel.width / 2, solarPanel.y + solarPanel.height,
|
| 96 |
+
inverter.x + inverter.width / 2, inverter.y,
|
| 97 |
+
'down'
|
| 98 |
+
);
|
| 99 |
+
|
| 100 |
+
// Biến tần -> Tải nhà
|
| 101 |
+
drawFlow(
|
| 102 |
+
inverter.x + inverter.width, inverter.y + inverter.height / 2,
|
| 103 |
+
homeLoad.x, homeLoad.y + homeLoad.height / 2,
|
| 104 |
+
'right'
|
| 105 |
+
);
|
| 106 |
+
|
| 107 |
+
// Biến tần -> Pin lưu trữ
|
| 108 |
+
drawFlow(
|
| 109 |
+
inverter.x + inverter.width / 2, inverter.y + inverter.height,
|
| 110 |
+
battery.x + battery.width / 2, battery.y,
|
| 111 |
+
'down'
|
| 112 |
+
);
|
| 113 |
+
|
| 114 |
+
// Pin lưu trữ -> Biến tần (khi năng lượng được lấy từ pin)
|
| 115 |
+
drawFlow(
|
| 116 |
+
battery.x + battery.width / 2, battery.y,
|
| 117 |
+
inverter.x + inverter.width / 2, inverter.y + inverter.height,
|
| 118 |
+
'up', // Dòng đi lên
|
| 119 |
+
-5 // Tốc độ ngược lại
|
| 120 |
+
);
|
| 121 |
+
|
| 122 |
+
// Cập nhật offset để tạo hoạt hình dòng chảy
|
| 123 |
+
energyFlowOffset = (energyFlowOffset - 1) % 50; // Thay đổi tốc độ và độ dài dòng chảy
|
| 124 |
+
|
| 125 |
+
animationFrameId = requestAnimationFrame(draw); // Lặp lại hoạt hình
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
// Các hàm điều khiển hoạt hình
|
| 129 |
+
function startAnimation() {
|
| 130 |
+
if (!animationFrameId) { // Chỉ bắt đầu nếu chưa có hoạt hình đang chạy
|
| 131 |
+
draw();
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
function stopAnimation() {
|
| 136 |
+
if (animationFrameId) {
|
| 137 |
+
cancelAnimationFrame(animationFrameId);
|
| 138 |
+
animationFrameId = null;
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// Bắt đầu hoạt hình khi trang tải xong
|
| 143 |
+
window.onload = startAnimation;
|