canvas实现可配置参数的彩色粒子运动动画效果代码
代码语言:html
所属分类:粒子
代码描述:canvas实现可配置参数的彩色粒子运动动画效果代码
代码标签: canvas 配置 参数 彩色 粒子 运动 动画
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> body, html { margin: 0; padding: 0; overflow: hidden; background-color: black; font-family: Arial, sans-serif; } #canvas { display: block; } #config-panel { position: absolute; top: 60px; left: 10px; background-color: rgba(30, 30, 30, 0.9); color: #fff; padding: 15px; border-radius: 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); transition: all 0.3s ease-in-out; max-width: 300px; } #config-panel.collapsed { transform: translateX(calc(-100% - 10px)); } #toggle-panel { position: absolute; cursor: pointer; padding: 10px; top: 10px; left: 10px; background-color: rgba(200, 200, 200, 0.5); border-radius: 5px; margin-bottom: 15px; text-align: center; font-weight: bold; transition: background-color 0.3s ease; } #toggle-panel:hover { background-color: rgba(80, 80, 80, 0.8); } .slider-container { margin-bottom: 15px; } .slider-container label { display: block; margin-bottom: 5px; font-size: 14px; } input[type="range"] { width: 100%; -webkit-appearance: none; background: transparent; margin: 10px 0; } input[type="range"]:focus { outline: none; } input[type="range"]::-webkit-slider-runnable-track { width: 100%; height: 4px; cursor: pointer; background: #4caf50; border-radius: 2px; } input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; height: 16px; width: 16px; border-radius: 50%; background: #fff; cursor: pointer; margin-top: -6px; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); } .value-display { display: inline-block; width: 40px; text-align: right; font-size: 14px; float: right; } button { background-color: #3498db; color: #fff; border: none; padding: 10px; cursor: pointer; border-radius: 5px; transition: background-color 0.2s; } button:hover { background-color: #2980b9; } </style> </head> <body translate="no"> <canvas id="canvas"></canvas> <div id="toggle-panel"> ☰</div> <div id="config-panel"> <button id="fullscreenBtn">Toggle Fullscreen</button> <P><div class="slider-container"> <label for="particle-count">Particle Count: <span class="value-display" id="particle-count-value">150</span></label> <input type="range" id="particle-count" min="10" max="300" value="150"> </div> <div class="slider-container"> <label for="max-velocity">Max Velocity: <span class="value-display" id="max-velocity-value">4</span></label> <input type="range" id="max-velocity" min="0.5" max="10" step="0.1" value="4"> </div> <div class="slider-container"> <label for="mouse-radius">Mouse Radius: <span class="value-display" id="mouse-radius-value">33</span></label> <input type="range" id="mouse-radius" min="10" max="100" value="33"> </div> <div class="slider-container"> <label for="mouse-force">Mouse Force: <span class="value-display" id="mouse-force-value">4</span></label> <input type="range" id="mouse-force" min="1" max="10" step="0.1" value="4"> </div> <div class="slider-container"> <label for="connection-distance">Connection Distance: <span class="value-display" id="connection-distance-value">70</span></label> <input type="range" id="connection-distance" min="50" max="200" value="70"> </div> </div> <script > const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const configPanel = document.getElementById('config-panel'); const togglePanel = document.getElementById('toggle-panel'); togglePanel.addEventListener('click', () => { configPanel.classList.toggle('collapsed'); }); function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; } window.addEventListener('resize', resizeCanvas); resizeCanvas(); let particles = []; let particleCount = 150; let maxVelocity = 4; let mouseForce = 4; let temporaryMouseForce = mouseForce; let connectionDistance = 70; const mouse = { x: canvas.width / 2, y: canvas.height / 2, radius: 33, isActive: false }; canvas.addEventListener('mousemove', function (e) { const rect = canvas.getBoundingClientRect(); mouse.x = e.clientX - rect.left; mouse.y = e.clientY - rect.top; mouse.isActive = true; }); canvas.addEventListener('mouseout', function () { mouse.isActive = false; }); canvas.addEventListener('touchstart', function (e) { e.preventDefault(); const rect = canvas.getBoundingClientRect(); mouse.x = e.touches[0].clientX - rect.left; mouse.y = e.touches[0].clientY - rect.top; mouse.isActive = true; }, { passive: false }); canvas.addEventListener('touchmove', function (e) { e.preventDefault(); const rect = canvas.getBoundingClientRect(); mouse.x = e.touches[0].clientX - rect.left; mouse.y = e.touches[0].clientY - rect.top; mouse.isActive = true; }, { passive: false }); canvas.addEventListener('touchend', function (e) { e.preventDefault(); mouse.isActive = false; }, { passive: false }); canvas.addEventListener('click', function (e) { const rect = canvas.getBoundingClientRect(); const clickX = e.clientX - rect.left; const clickY = e.clientY - rect.top; let particleClicked = false; particles.forEach((p, index) => { const dx = p.x - clickX; const dy = p.y - clickY; const dist = Math.hypot(dx, dy); if (dist <= p.radius) { explodeParticle(index); particleClicked = true; } }); if (particleClicked) { temporaryMouseForce = mouseForce * 3; setTimeout(() => { temporaryMouseForce = mouseForce; }, 500); } }); function explodeParticle(index) { const parentParticle = particles[index]; particles.splice.........完整代码请登录后点击上方下载按钮下载查看
网友评论0