星际穿越粒子特效
代码语言:html
所属分类:粒子
代码描述:星际穿越粒子特效,按住左键出现粒子放大
代码标签: 特效
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> /******************** Common ********************/ html, body { height: 100%; width: 100%; font-family: Helvetica, verdana, monospace; color: #FFF; font-size: 100%; padding: 0; margin: 0; letter-spacing: 0.2rem; overflow: hidden; background: #000; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } a { color: #FFF; text-decoration: none; } h1 { font-size: 1.6rem; } p { padding: 0.8rem 0; font-size: 0.8rem; } header#header { position: absolute; top: 0; left: 0; padding: 1.6rem; } /******************** Contents ********************/ canvas#canvas { background: #000; } </style> </head> <body translate="no"> <header id="header"> <h1>goToTheMoon</h1> <p>Created date / April 19, 2020</p> </header> <main id="main"> <canvas id="canvas">This browser cannot use a canvas.</canvas> </main> <script> (function () { 'use strict'; window.addEventListener('load', function () { var canvas = document.getElementById('canvas'); if (!canvas || !canvas.getContext) { return false; } /******************** Random Number ********************/ function rand(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } /******************** Var ********************/ // canvas var ctx = canvas.getContext('2d'); var X = canvas.width = window.innerWidth; var Y = canvas.height = window.innerHeight; var mouseX = X / 2; var mouseY = Y / 2; /******************** Animation ********************/ window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function (cb) { setTimeout(cb, 17); }; /******************** Moon ********************/ var moonNum = 1; var moons = []; var radius = X / 2; if (X < 768) { radius = X / 2; } function Moon(ctx, x, y) { this.ctx = ctx; this.init(x, y); } Moon.prototype.init = function (x, y) { this.x = x; this.y = y; this.c = '255, 255, 255'; this.r = radius; }; Moon.prototype.resize = function () { this.x = X / 2; this.y = Y / 2; this.r = radius; }; Moon.prototype.render = function () { this.draw(); }; Moon.prototype.draw = function () { ctx.save(); ctx.beginPath(); ctx.globalAlpha = 0.8; var col = this.c; var g = ctx.createRadialGradient(this.x, this.y, this.r, X / 2 - this.r, Y / 2, 0); g.addColorStop(0, "rgba(" + col + ", " + 1 * 1 + ")"); g.addColorStop(0.5, "rgba(" + col + ", " + 1 * 0.2 + ")"); g.addColorStop(1, "rgba(" + col + ", " + 1 * 0 + ")"); ctx.fillStyle = g; ctx.arc(this.x, this.y, this.r, Math.PI * 2, false); ctx.fill(); ctx.restore(); }; for (var i = 0; i < moonNum; i++) { var moon = new Moon(ctx, 0, 0); moons.push(moon); } /******************** Particle ********************/ var particleNum = 2000; var particles = []; var maxParticles = 1; var colors = ['rgb(54, 38, 112)', 'rgb(98, 98, 159)', 'rgb(0, 137, 190)', 'rgb(0, 108, 154)']; if (X < 768) { particleNum = 1000; } function Particle(ctx, x, y, r) { this.ctx = ctx; this.init(x, y, r); } Particle.prototype.init = function (x, y, r) { this.x = x; this.y = y; this.r = r; this.s = 0.1; this.ga = rand(0, 1) + 0.1; this.v = { x: 0, y: 0 }; /* this.c = { r: rand(0, 255), .........完整代码请登录后点击上方下载按钮下载查看
网友评论0