HTML5 Canvas宇宙星云动画特效

这是一个使用HTML5 Canvas和jQuery实现的宇宙星云动画效果。屏幕上的多彩光点模仿了宇宙星云,并且这些光点是动态变化的,营造出一种令人惊叹的视觉体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是一个宇宙星云动画效果,基于jquery.js和HTML5 Canvas制作,屏幕上会产生很多大大小小的光点,色彩斑斓,酷似宇宙的星云图,并且,星云图还是运动的,非常好看的一款HTML5动画实例。
这里写图片描述

查看原文

查看在线演示Demo和更多原文内容教程: HTML5 Canvas宇宙星云动画特效

在线演示

源码下载

### 使用 HTML 和 CSS 或 JavaScript 创建动态宇宙星空效果 #### 方法一:基于 HTML5 Canvas 的解决方案 通过使用 `HTML5` 中的 `<canvas>` 元素可以轻松创建复杂的图形和动画,包括模拟星空的效果。下面是一段简单的代码片段来展示如何利用 `Canvas API` 来构建这样的视觉体验。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Dynamic Starry Sky with Canvas</title> <style> body { overflow: hidden; margin: 0; background-color: black; } canvas { display: block; position: absolute; top: 0; left: 0; } </style> </head> <body> <canvas id="starrySky"></canvas> <script> const canvas = document.getElementById('starrySky'); const ctx = canvas.getContext('2d'); // 设置画布大小等于窗口尺寸 function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; } resizeCanvas(); window.addEventListener('resize', resizeCanvas); class Star { constructor(x, y, radius) { this.x = x || Math.random() * canvas.width; this.y = y || Math.random() * canvas.height; this.radius = radius || Math.random() / 2 + 0.5; this.speedX = (Math.random() - 0.5); this.speedY = (Math.random() - 0.5); } draw() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false); const gradient = ctx.createRadialGradient( this.x, this.y, 0, this.x, this.y, this.radius ); gradient.addColorStop(0, 'white'); gradient.addColorStop(.7, '#ccc'); gradient.addColorStop(1, 'transparent'); ctx.fillStyle = gradient; ctx.fill(); } update() { this.draw(); // 移动星星位置并处理边界反弹逻辑 if (this.x >= canvas.width || this.x <= 0) { this.speedX *= -1; } if (this.y >= canvas.height || this.y <= 0) { this.speedY *= -1; } this.x += this.speedX; this.y += this.speedY; } } let starsArray = []; for(let i=0;i<300;i++){ let star = new Star(); starsArray.push(star); } function animate(){ requestAnimationFrame(animate); ctx.clearRect(0, 0, canvas.width, canvas.height); for(var j=0;j<starsArray.length;j++){ starsArray[j].update(); } } animate(); </script> </body> </html> ``` 这段代码定义了一个名为 `Star` 的类用来表示单颗星体,并且每帧都会更新它们的位置以及重新渲染整个场景以达到流畅运动的目的[^1]。 #### 方法二:仅依赖于 CSS 的静态星空背景 如果不需要交互性的动画而只是想要一个美观的固定星空作为网站或其他项目的装饰,则可以通过纯粹的 CSS 完成此任务: ```css /* Static starry sky using only CSS */ body::before{ content:""; position:absolute; z-index:-1; top:0; bottom:0; right:0; left:0; background-image:url('data:image/svg+xml;base64,...'); /* Base64 encoded SVG pattern of small dots representing stars */ filter:brightness(90%); } ``` 这里采用了一种技巧——将许多微小圆点组成的图案编码为 base64 字符串形式嵌入到样式表内充当背景图像[^2]。不过这种方法通常不会像前者那样生动活泼,因为缺乏真正的动态变化。 对于更高级的需求比如让这些光斑随时间缓慢闪烁或是跟随用户的鼠标指针移动等特性来说,还是推荐选用带有脚本支持的方式去实现更为合适一些。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值