Javascript学习--烟花
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>烟花-喷泉</title> <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script> <style type="text/css"> * { padding: 0; margin: 0; } html, body { position: relative; width: 100%; height: 100%; } body { background: #eee; } canvas { background: black; display: block; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } </style> </head> <body> <canvas id="c"></canvas> <script type="text/javascript"> ; (function(main) { main(); })(function() { 'use strict'; var c = document.getElementById('c'); var ctx = c.getContext('2d'); var W = c.width = window.innerWidth; var H = c.height = window.innerHeight; var CX = W / 2; var CY = H / 2; var Particle = function(x, y, vx, vy) { this.x = x; this.y = y; this.ox = x; this.oy = y; this.vx = vx; this.vy = vy; this.alpha = Math.random(); this.color = 25; this.lineWidth = Math.random() * 5; }; Particle.prototype = { constructor: Particle, update: function() { this.vx += Math.random() * 0.5 - 0.25; this.vy += 0.8; this.vy *= 0.98; this.alpha *= 0.95; this.ox = this.x; this.oy = this.y; this.x += this.vx; this.y += this.vy; if(this.y < 0 || this.y > H || this.alpha < 0.1) { this.vx = Math.random() * 2 - 1; this.vy = Math.random() * -50; this.ox = this.x = CX; this.oy = this.y = H; this.alpha = Math.random(); } }, render: function(ctx) { ctx.save(); ctx.globalAlpha = 0.98; ctx.lineWidth = this.lineWidth; ctx.strokeStyle = 'hsla(' + (this.color) + ',100%,50%,' + this.alpha + ')'; ctx.beginPath(); ctx.moveTo(this.ox, this.oy); ctx.lineTo(this.x, this.y); ctx.stroke(); ctx.restore(); } }; var particleCount = 500; var particle = null; var particles = []; var interval = 0; for(var i = 0; i < 250; i++) { particle = new Particle(CX, H, Math.random() * 2 - 1, Math.random() * -50); particles.push(particle); } requestAnimationFrame(function loop() { requestAnimationFrame(loop); ctx.globalCompositeOperation = 'source-over'; ctx.fillStyle = 'rgba(0,0,0,0.1)'; ctx.fillRect(0, 0, W, H); ctx.globalCompositeOperation = 'lighter'; if(particles.length < particleCount) { particle = new Particle(CX, H, Math.random() * 2 - 1, Math.random() * -50); particles.push(particle); } for(var i = 0, len = particles.length; i < len; i++) { particle = particles[i]; particle.update(); particle.render(ctx); } }); }); </script> </body> </html>
作者:眾尋
出处:https://www.cnblogs.com/ZXdeveloper/p/7867804.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)