news 2026/4/3 1:34:45

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/3/17 2:04:11

PHP8.4重磅更新:性能飙升新特性

PHP 8.4 的新特性PHP 8.4 预计于 2024 年 11 月发布&#xff0c;目前处于开发阶段。以下是已公布或计划中的主要更新内容&#xff1a;JIT 改进 PHP 8.4 将进一步优化 JIT&#xff08;Just-In-Time&#xff09;编译器&#xff0c;提升性能表现。新的优化策略将针对特定代码模式进…

作者头像 李华
网站建设 2026/3/26 16:03:39

架构之DID(Design-Implement-Deploy)方法论

架构之DID方法论&#xff1a;设计-实施-部署 概述 DID&#xff08;Design-Implement-Deploy&#xff09;是一种架构可扩展性方法论&#xff0c;旨在以最经济有效的方式保证系统的可扩展性。通过在系统生命周期的不同阶段采用不同的容量规划策略&#xff0c;实现资源利用的最优化…

作者头像 李华
网站建设 2026/3/27 23:37:37

企业级光储充电站能量协调控制系统的设计与应用

安科瑞刘鸿鹏 摘要随着企业级光伏、储能及电动汽车充电设施的集中建设&#xff0c;光储充电站逐渐从单一能源设施演变为具备多源接入、多目标运行的复杂用能系统。如何在保障安全运行的前提下&#xff0c;实现新能源有效消纳、储能经济调度及充电负荷柔性管理&#xff0c;成为企…

作者头像 李华
网站建设 2026/3/30 13:49:52

Notation 英文单词学习

1️、基本信息单词&#xff1a;notation词性&#xff1a;名词发音&#xff1a; &#x1f1fa;&#x1f1f8; /noʊˈteɪ.ʃən/&#x1f1ec;&#x1f1e7; /nəʊˈteɪ.ʃən/词源&#xff1a; 来自拉丁语 notatio&#xff08;标记、符号&#xff09; → notation 符号 / 标…

作者头像 李华