canvas实现鼠标跟随粒子连线圈圈冒泡交互动画效果代码
代码语言:html
所属分类:动画
代码描述:canvas实现鼠标跟随粒子连线圈圈冒泡交互动画效果代码
代码标签: canvas 鼠标 跟随 粒子 连线 圈圈 冒泡 交互 动画
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body, html { margin: 0; padding: 0; overflow: hidden; } canvas { display: block; } </style> </head> <body translate="no"> <canvas id="canvas"></canvas> <script > // Grab the canvas element from the DOM const canvas = document.getElementById("canvas"); // Get a 2D drawing context from the canvas const ctx = canvas.getContext("2d"); // Arrays to hold various particle types // (General particles, fireworks, dusty background, and ripples) const particles = []; const fireworkParticles = []; const dustParticles = []; const ripples = []; const techRipples = []; // A simple mouse state object to track the user's cursor const mouse = (() => { let state = { x: null, y: null }; return { get x() { return state.x; }, get y() { return state.y; }, set({ x, y }) { // Update the mouse position whenever the user moves the cursor state = { x, y }; }, reset() { // Clear mouse position when it leaves the canvas state = { x: null, y: null }; } }; })(); // Some global state variables for background shifting and frame counting let backgroundHue = 0; let frameCount = 0; let autoDrift = true; // If true, particles gently drift on their own // Dynamically adjust the number of particles based on canvas size function adjustParticleCount() { const particleConfig = { heightConditions: [200, 300, 400, 500, 600], widthConditions: [450, 600, 900, 1200, 1600], particlesForHeight: [40, 60, 70, 90, 110], particlesForWidth: [40, 50, 70, 90, 110] }; let numParticles = 130; // Check the height and pick a suitable particle count for (let i = 0; i < particleConfig.heightConditions.length; i++) { if (canvas.height < particleConfig.heightConditions[i]) { numParticles = particleConfig.particlesForHeight[i]; break; .........完整代码请登录后点击上方下载按钮下载查看
网友评论0