news 2026/6/13 0:56:03

Canvas粒子动画:打造炫酷鼠标追踪效果

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Canvas粒子动画:打造炫酷鼠标追踪效果

粒子动画效果

使用Canvas创建粒子动画效果,粒子会跟随鼠标移动或形成特定图案。以下代码实现了一个基础的粒子系统:

const canvas = document.getElementById('particleCanvas'); const ctx = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; const particles = []; for (let i = 0; i < 100; i++) { particles.push({ x: Math.random() * canvas.width, y: Math.random() * canvas.height, size: Math.random() * 5 + 1, speedX: Math.random() * 3 - 1.5, speedY: Math.random() * 3 - 1.5 }); } function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); particles.forEach(particle => { particle.x += particle.speedX; particle.y += particle.speedY; if (particle.x < 0 || particle.x > canvas.width) particle.speedX *= -1; if (particle.y < 0 || particle.y > canvas.height) particle.speedY *= -1; ctx.beginPath(); ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2); ctx.fillStyle = 'rgba(100, 200, 255, 0.8)'; ctx.fill(); }); requestAnimationFrame(animate); } animate();

文字渐变效果

实现文字颜色渐变和动画效果,可以通过CSS配合JS完成:

const textElement = document.getElementById('animatedText'); const colors = ['#ff3366', '#20b2aa', '#9370db', '#32cd32']; let currentIndex = 0; function changeColor() { textElement.style.color = colors[currentIndex]; currentIndex = (currentIndex + 1) % colors.length; const scale = 1 + Math.sin(Date.now() / 500) * 0.1; textElement.style.transform = `scale(${scale})`; } setInterval(changeColor, 1000);

页面滚动视差

创建具有深度感的滚动视差效果:

window.addEventListener('scroll', () => { const scrollPosition = window.pageYOffset; const parallaxElements = document.querySelectorAll('.parallax'); parallaxElements.forEach(element => { const speed = parseFloat(element.dataset.speed); const yPos = -(scrollPosition * speed); element.style.transform = `translate3d(0, ${yPos}px, 0)`; }); });

鼠标跟随光环

实现一个跟随鼠标移动的光环效果:

document.addEventListener('mousemove', (e) => { const halo = document.getElementById('mouseHalo'); halo.style.left = `${e.clientX - 50}px`; halo.style.top = `${e.clientY - 50}px`; const size = 100 + Math.sin(Date.now() / 200) * 20; halo.style.width = `${size}px`; halo.style.height = `${size}px`; halo.style.opacity = 0.7 + Math.sin(Date.now() / 300) * 0.3; });

3D卡片翻转

使用CSS 3D变换和JS实现卡片翻转效果:

const cards = document.querySelectorAll('.flip-card'); cards.forEach(card => { card.addEventListener('click', () => { card.classList.toggle('flipped'); }); });

配合CSS:

.flip-card { transition: transform 0.6s; transform-style: preserve-3d; } .flip-card.flipped { transform: rotateY(180deg); }

波浪动画效果

使用SVG和JS创建波浪动画:

const wave = document.getElementById('wave'); let offset = 0; function animateWave() { offset += 0.05; const pathData = `M0,100 Q25,130 50,100 T100,100 T150,100 T200,100 T250,100 T300,100 T350,100 T400,100 T450,100 T500,100 V200 H0 Z`; wave.setAttribute('d', pathData); requestAnimationFrame(animateWave); } animateWave();

烟花爆炸效果

Canvas实现的烟花爆炸动画:

class Firework { constructor(x, y, targetX, targetY) { this.x = x; this.y = y; this.targetX = targetX; this.targetY = targetY; this.particles = []; this.alive = true; } update() { // 更新烟花位置和粒子逻辑 } draw(ctx) { // 绘制烟花和粒子 } } const fireworks = []; document.addEventListener('click', (e) => { fireworks.push(new Firework( canvas.width/2, canvas.height, e.clientX, e.clientY )); });

背景星空效果

创建随机闪烁的星空背景:

function createStars() { const stars = []; for (let i = 0; i < 200; i++) { stars.push({ x: Math.random() * canvas.width, y: Math.random() * canvas.height, size: Math.random() * 1.5, opacity: Math.random(), speed: Math.random() * 0.05 }); } return stars; } function drawStars(ctx, stars) { stars.forEach(star => { star.opacity += (Math.random() - 0.5) * star.speed; star.opacity = Math.max(0, Math.min(1, star.opacity)); ctx.beginPath(); ctx.arc(star.x, star.y, star.size, 0, Math.PI * 2); ctx.fillStyle = `rgba(255, 255, 255, ${star.opacity})`; ctx.fill(); }); }

页面过渡动画

实现页面切换时的平滑过渡:

function pageTransition(outPage, inPage) { outPage.style.opacity = '0'; outPage.style.transform = 'translateX(-50px)'; outPage.style.transition = 'all 0.5s ease'; setTimeout(() => { outPage.style.display = 'none'; inPage.style.display = 'block'; setTimeout(() => { inPage.style.opacity = '1'; inPage.style.transform = 'translateX(0)'; }, 50); }, 500); }

编程语言C++m.renkangtang.net++c语言的魅力
编程语言C++m.pengdongny.com++c语言的魅力
编程语言C++m.sy-zjzx.com++c语言的魅力
编程语言C++m.spsrshop.com++c语言的魅力
编程语言C++m.5lue.com++c语言的魅力
编程语言C++m.ynlzz.com++c语言的魅力
编程语言C++m.hudongc.com++c语言的魅力
编程语言C++m.fmzhenxi.com++c语言的魅力
编程语言C++m.shangai.net++c语言的魅力
编程语言C++m.scw023.com++c语言的魅力

编程语言C++m.hengshuidongtong.com++c语言的魅力
编程语言C++m.meta12cLoud.com++c语言的魅力
编程语言C++m.shuangving.com++c语言的魅力
编程语言C++wab.hengshuidongtong.com++c语言的魅力
编程语言C++wab.meta12cLoud.com++c语言的魅力
编程语言C++wab.shuangving.com++c语言的魅力

编程语言C++moblie.songfudaojia.com++c语言的魅力
编程语言C++m.carandfan.com++c语言的魅力
编程语言C++wap.tlxgpsgs.com++c语言的魅力
编程语言C++blog.songfudaojia.com++c语言的魅力
编程语言C++moblie.carandfan.com++c语言的魅力
编程语言C++m.tlxgpsgs.com++c语言的魅力
编程语言C++wap.songfudaojia.com++c语言的魅力
编程语言C++blog.carandfan.com++c语言的魅力
编程语言C++moblie.tlxgpsgs.com++c语言的魅力
编程语言C++m.songfudaojia.com++c语言的魅力
编程语言C++wap.carandfan.com++c语言的魅力
编程语言C++blog.tlxgpsgs.com++c语言的魅力
编程语言C++moblie.songfudaojia.com++c语言的魅力
编程语言C++m.carandfan.com++c语言的魅力
编程语言C++wap.tlxgpsgs.com++c语言的魅力
————————————————
版权声明:本文为CSDN博主「e***9857」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/2509_94095094/article/details/157142302

打字机效果

模拟打字机逐个显示文字的效果:

function typeWriter(element, text, speed) { let i = 0; function typing() { if (i < text.length) { element.innerHTML += text.charAt(i); i++; setTimeout(typing, speed); } } typing(); } typeWriter(document.getElementById('typeText'), 'Hello, this is a typing effect!', 100);
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 12:56:05

第64集科立分板机:分板机常见类型及优缺点介绍

分板机是用于分割电路板的设备&#xff0c;主要将连接在一起的电路板分离成单个单元&#xff0c;广泛应用于电子产品制造业&#xff0c;已基本取代传统人工折板方式。以下为你详细介绍&#xff1a;常见类型及优缺点走刀式分板机&#xff1a;成本低&#xff0c;但只能进行直线分…

作者头像 李华
网站建设 2026/6/10 18:05:01

计算机毕业设计springboot医疗管理系统 基于Spring Boot的医疗信息化管理系统设计与实现 Spring Boot框架下的智慧医疗管理系统开发

计算机毕业设计springboot医疗管理系统sz655&#xff08;配套有源码 程序 mysql数据库 论文&#xff09; 本套源码可以在文本联xi,先看具体系统功能演示视频领取&#xff0c;可分享源码参考。随着信息技术的飞速发展&#xff0c;传统的医疗管理模式已难以满足现代社会对高效、便…

作者头像 李华
网站建设 2026/6/11 3:42:12

多门店管理系统如何选?功能、成本与服务全解析

于零售行业数字化转型的浪潮之际&#xff0c;多门店管理系统已然成了连锁品牌以及实体商家达成高效运营的关键工具&#xff0c;这类系统借助整合商品、库存、会员、财务以及员工管理等模块&#xff0c;助力管理者破除信息孤岛&#xff0c;达成对零散门店的集中化、标准化管控&a…

作者头像 李华
网站建设 2026/5/30 23:56:28

免费查文献的网站推荐:实用且可靠的文献查询平台汇总

做科研的第一道坎&#xff0c;往往不是做实验&#xff0c;也不是写论文&#xff0c;而是——找文献。 很多新手科研小白会陷入一个怪圈&#xff1a;在知网、Google Scholar 上不断换关键词&#xff0c;结果要么信息过载&#xff0c;要么完全抓不到重点。今天分享几个长期使用的…

作者头像 李华