/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import React, { useEffect, useRef } from 'react'; const MapleBackground = () => { const canvasRef = useRef(null); useEffect(() => { const canvas = canvasRef.current; const ctx = canvas.getContext('2d'); let animationFrameId; let width, height; // Realistic Maple Leaf Path Data (SVG path) const leafPathData = 'M150.6,90.8c-5.8,1.6-11.3-1.6-14.9-5.3c-3.6-3.7-5.5-8.5-4.8-13.6c-0.6-2.6-1.5-5.1-2.9-7.4c-3.2-5.4-8.8-8.8-14.8-9.5 c-3.8-0.5-7.6,0.3-11.1,2.1c-0.8,0.4-1.6,0.9-2.5,1.5c-0.1-0.1-0.2-0.3-0.3-0.4c-3-5.2-7.8-9.1-13.4-10.9c-2.3-0.7-4.7-1.1-7-1.1 c-0.8,0-1.6,0-2.3,0.1c0.1-1.3,0.1-2.7-0.1-4c-0.4-3.4-1.7-6.5-3.6-9.1c-4.3-5.7-11.2-8.5-18.1-7.4c-6.8,1.1-12.2,6-14.3,12.7 c-0.6,1.9-0.8,3.9-0.7,5.9c-2.6-0.3-5.3-0.2-7.8,0.5c-6.2,1.5-11.2,6.1-13.6,12.1c-0.7,1.8-1.2,3.7-1.3,5.6c-0.8-0.6-1.7-1.1-2.6-1.6 c-3.5-1.8-7.3-2.6-11.1-2.1c-6.1,0.7-11.6,4.1-14.8,9.5c-1.4,2.3-2.3,4.8-2.9,7.4c0.7,5.1-1.2,9.9-4.8,13.6 c-3.6,3.7-9.1,6.9-14.9,5.3c-2.6-0.7-5.1-1.9-7.2-3.5c-1.3-1-2.3-2.3-3.1-3.7c-0.6,3.1-0.1,6.4,1.4,9.2c2.6,4.9,7.6,8.2,13,8.7 c3.5,0.3,6.9-0.5,9.9-2.1c0.4,2.5,1.2,4.8,2.5,6.9c2.6,4.3,6.8,7.3,11.6,8.4c0.8,0.2,1.6,0.3,2.4,0.3c0.7,0,1.3-0.1,2-0.2 c-0.1,1.1-0.1,2.3,0,3.4c0.6,5,3.6,9.2,8.1,11.5c4.5,2.3,9.8,2.3,14.3,0c1.7-0.9,3.2-2.1,4.4-3.5c0.8,3.1,2.6,5.8,5.1,7.8 c4.4,3.4,10.2,4.3,15.3,2.4c1.1-0.4,2.2-0.9,3.2-1.6l0.5,21.7c0.1,2.5,2.2,4.5,4.7,4.4c2.5-0.1,4.5-2.2,4.4-4.7l-0.5-22.6 c1.1,0.5,2.2,0.9,3.4,1.2c1.7,0.4,3.4,0.6,5.1,0.6c3.9,0,7.6-1.2,10.7-3.6c2.5-1.9,4.4-4.6,5.2-7.6c1.3,1.4,2.8,2.6,4.5,3.5 c4.5,2.3,9.8,2.3,14.3,0c4.5-2.3,7.5-6.5,8.1-11.5c0.1-1.1,0.1-2.3,0-3.4c0.7,0.1,1.3,0.2,2,0.2c0.8,0,1.6-0.1,2.4-0.3 c4.8-1.1,9-4.1,11.6-8.4c1.2-2.1,2.1-4.4,2.5-6.9c3.1,1.7,6.4,2.4,9.9,2.1c5.5-0.5,10.4-3.8,13-8.7c1.5-2.8,2-6.1,1.4-9.2 C157.9,87,156.9,88.3,155.6,89.3C153.5,90.9,153.2,90.1,150.6,90.8z'; const leafPath = new Path2D(leafPathData); // 检测是否为移动设备 const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( navigator.userAgent, ); // 简化的云朵类(性能优化版) class Cloud { constructor() { this.reset(); this.x = Math.random() * width; // 随机初始位置 } reset() { this.x = -150; this.y = Math.random() * (height * 0.3); // 屏幕上方 30% this.speed = Math.random() * 0.15 + 0.08; this.scale = Math.random() * 0.4 + 0.4; this.opacity = Math.random() * 0.2 + 0.3; } update() { this.x += this.speed; if (this.x > width + 150) { this.reset(); } } draw(ctx) { ctx.save(); ctx.translate(this.x, this.y); ctx.scale(this.scale, this.scale); ctx.globalAlpha = this.opacity; ctx.fillStyle = '#FFFFFF'; // 简化云朵:只用 3 个圆形(原版 4 个) ctx.beginPath(); ctx.arc(0, 0, 25, 0, Math.PI * 2); ctx.arc(30, -8, 30, 0, Math.PI * 2); ctx.arc(55, 0, 25, 0, Math.PI * 2); ctx.fill(); ctx.restore(); } } class Leaf { constructor() { this.init(true); } init(randomY = false) { this.x = Math.random() * width; this.y = randomY ? Math.random() * height : -50; this.size = (Math.random() * 0.5 + 0.3) * 0.4; // Scaled down for the path size this.speed = Math.random() * 1.5 + 0.5; this.sway = Math.random() * 0.02 + 0.005; this.swayOffset = Math.random() * Math.PI * 2; this.rotation = Math.random() * Math.PI * 2; this.rotationSpeed = (Math.random() - 0.5) * 0.02; // Realistic Autumn Colors const colors = [ '#C0392B', // Dark Red '#E74C3C', // Red '#D35400', // Pumpkin '#E67E22', // Carrot '#F39C12', // Orange '#F1C40F', // Yellow '#A0522D', // Sienna (Brown) '#8B4513', // SaddleBrown ]; this.color = colors[Math.floor(Math.random() * colors.length)]; } update() { this.y += this.speed; this.x += Math.sin(this.swayOffset) * 0.8; // More gentle sway this.swayOffset += this.sway; this.rotation += this.rotationSpeed; if (this.y > height + 50) { this.init(); } } draw(ctx) { ctx.save(); ctx.translate(this.x, this.y); ctx.rotate(this.rotation); ctx.scale(this.size, this.size); // Offset to center the path rotation ctx.translate(-75, -75); // 添加轻微阴影以保留立体感(性能影响小) ctx.shadowColor = 'rgba(0,0,0,0.15)'; ctx.shadowBlur = 3; ctx.shadowOffsetX = 1; ctx.shadowOffsetY = 1; ctx.fillStyle = this.color; ctx.fill(leafPath); // 移除阴影以避免影响叶脉 ctx.shadowColor = 'transparent'; // 只在桌面端绘制叶脉,移动端跳过以提升性能 if (!isMobile) { ctx.strokeStyle = 'rgba(0, 0, 0, 0.2)'; ctx.lineWidth = 1.5; ctx.lineCap = 'round'; // 简化叶脉:绘制主脉和 2 条侧脉 ctx.beginPath(); ctx.moveTo(78, 115); ctx.lineTo(78, 20); ctx.stroke(); // 上侧脉 ctx.beginPath(); ctx.moveTo(78, 100); ctx.quadraticCurveTo(60, 80, 30, 45); ctx.stroke(); ctx.beginPath(); ctx.moveTo(78, 100); ctx.quadraticCurveTo(96, 80, 125, 45); ctx.stroke(); } ctx.restore(); } } let leaves = []; let clouds = []; // 根据设备性能自适应调整元素数量 const LEAF_COUNT = isMobile ? 10 : 20; const CLOUD_COUNT = isMobile ? 2 : 3; // 简化版:移动端 2 朵,桌面端 3 朵 const initElements = () => { leaves = []; clouds = []; for (let i = 0; i < LEAF_COUNT; i++) { leaves.push(new Leaf()); } for (let i = 0; i < CLOUD_COUNT; i++) { clouds.push(new Cloud()); } }; const drawSkyAndField = () => { // 保留天空渐变(性能影响小,视觉提升大) const skyGrad = ctx.createLinearGradient(0, 0, 0, height); skyGrad.addColorStop(0, '#5CACEE'); skyGrad.addColorStop(0.6, '#B0E0E6'); skyGrad.addColorStop(1, '#E0F7FA'); ctx.fillStyle = skyGrad; ctx.fillRect(0, 0, width, height); // 绘制云朵(在山丘后面) clouds.forEach((cloud) => { cloud.update(); cloud.draw(ctx); }); // 简化山丘:只保留 2 层以保持层次感 ctx.beginPath(); ctx.moveTo(0, height * 0.85); ctx.bezierCurveTo( width * 0.4, height * 0.75, width * 0.6, height * 0.9, width, height * 0.82, ); ctx.lineTo(width, height); ctx.lineTo(0, height); ctx.fillStyle = '#8FBC8F'; ctx.fill(); ctx.beginPath(); ctx.moveTo(0, height); ctx.lineTo(0, height * 0.95); ctx.bezierCurveTo( width * 0.5, height * 1.05, width * 0.8, height * 0.92, width, height * 0.96, ); ctx.lineTo(width, height); ctx.fillStyle = '#556B2F'; ctx.fill(); }; const animate = () => { ctx.clearRect(0, 0, width, height); drawSkyAndField(); leaves.forEach((leaf) => { leaf.update(); leaf.draw(ctx); }); animationFrameId = requestAnimationFrame(animate); }; const resize = () => { width = window.innerWidth; height = window.innerHeight; canvas.width = width; canvas.height = height; }; window.addEventListener('resize', resize); resize(); initElements(); animate(); return () => { window.removeEventListener('resize', resize); cancelAnimationFrame(animationFrameId); }; }, []); return ( ); }; export default MapleBackground;