<!DOCTYPE html> <html> <head> <Meta charset="UTF-8"> <title>动态星空</title> <style> body { margin: 0; padding: 0; } canvas { display: block; } </style> </head> <body> <script> // 创建画布 var canvas = document.createElement('canvas'),ctx = canvas.getContext('2d'),w = canvas.width = window.innerWidth,h = canvas.height = window.innerHeight,particles = [],properties = { bgColor : 'rgba(17,17,19,1)',particleColor : 'rgba(255,255,particleRadius : 3,particleCount : 60,particleMaxVeLocity : 0.2,lineLength : 150,particleLife : 6,}; // 把画布添加到页面 document.querySelector('body').appendChild(canvas); // 随机一组粒子 function Particle() { this.x = Math.random()*w; this.y = Math.random()*h; this.vx = Math.random()*properties.particleMaxVeLocity*2-properties.particleMaxVeLocity; this.vy = Math.random()*properties.particleMaxVeLocity*2-properties.particleMaxVeLocity; this.life = Math.random()*properties.particleLife*60; } // 给每个粒子添加各自的行为 Particle.prototype.draw = function() { ctx.beginPath(); ctx.arc(this.x,this.y,properties.particleRadius,Math.PI*2); ctx.closePath(); ctx.fillStyle = properties.particleColor; ctx.fill(); }; // 持续更新每个粒子的位置和行为 Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; if (this.x w) { this.vx = -this.vx; } if (this.y h) { this.vy = -this.vy; } this.life--; if (this.life 这段代码中,我们通过JavaScript的Canvas API来创建和维护动态星空效果,同时也使用了HTML和CSS来控制画布的样式和布局。这个效果可以通过修改各个参数来改变它的外观和行为,比如粒子的颜色、半径、数量、速度、寿命、粒子之间的连线长度等等。如果你喜欢这个效果,并且对JavaScript和Canvas API有一定的了解,那么你也可以尝试去自己修改它,来创造出你自己的星空世界!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。