html--瀑布

html 复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <style>
  html, body{
  margin: 0;
  padding: 0;
  overflow: hidden;
}
  </style>
 </HEAD>

 <BODY>
  <canvas id="canvas"></canvas>
  <script>
  var canvas = document.getElementById('canvas'),
    c = canvas.getContext("2d"),
    particles = {},
    particleIndex = 0,
    particleNum = 50,
    gravity = 0.7;

// full screen
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

// particle
function Particle(){
  
  this.posX = canvas.width / 2; // position X
  this.posY = canvas.height / 8; // position Y
  this.vx = Math.random() * 10 - 5; // velocity X
  this.vy = Math.random() * 10 - 5 ; // velocity Y
  this.width = 1; // particle width
  this.height = Math.random() * 6 - 3; // particle height
  
  particleIndex++;
  particles[particleIndex] = this;
  this.id = particleIndex;
  
  this.life= 0;
  this.death = 140;
  
  //random color
  this.colors = [
    "rgba(100, 100, 100,"+ (Math.random() + .5) +")",
    "rgba(52, 152, 200,"+ (Math.random() + .5) +")",
    "rgba(41, 128, 250,"+ (Math.random() + .5) +")"];
  this.color = this.colors[Math.floor(Math.random() * 3)];
}

// draw
Particle.prototype.draw = function(){
  this.posX += this.vx;
  this.posY += this.vy;
  
  this.life++;

  if(this.life >= this.death){
    delete particles[this.id];
  }
  
  if(this.posY > (canvas.height - 5)){
    this.vx *= 0.8;
    this.vy *= -0.5;
    this.posY = (canvas.height - 5);
  }
  
  this.vy += gravity;
  
  c.fillStyle = this.color;
  c.fillRect(this.posX,this.posY, this.width, this.height);
}

setInterval(function(){ 
  c.fillStyle = "rgba(0,0,0,0.4)";
  c.fillRect(0,0,canvas.width,canvas.height);
  
  for(var i = 0; i < particleNum; i++){
    new Particle();
  }
  
  for(var i in particles){
    particles[i].draw();
  }
}, 30);
  </script>
 </BODY>
</HTML>
相关推荐
runningshark26 分钟前
Lecture: The ‘Why & How‘ Principle: Moving Beyond Simple Statements
开发语言·前端·javascript
公爵爱学习31 分钟前
无人机多点导航笔记
java·前端·笔记
是立不是利36 分钟前
HTML 的尊严:一种被低估的语言
前端·html
秋天的一阵风40 分钟前
🤔首屏Banner压到40KB,LCP还是4秒?原来一直搞错了最大渲染元素
前端·人工智能·面试
秋天的一阵风40 分钟前
💬面试官:Markdown 流式解析如何避免标签截断?「直接重新让 marked 全部渲染」行不行?
前端·面试·ai编程
芳心粽伙饭41 分钟前
CSS第二章 选择器
前端·css
计算机魔术师42 分钟前
OpenAI内部实习生已顶配3.1倍人力,你的团队还在等人工排期?
前端
雪芽蓝域zzs1 小时前
第三十一节:角色管理页面 + 权限分配树形弹窗
前端·javascript·vue.js
zz-zjx1 小时前
Python实用转换模板
开发语言·前端·python
暖焰核心1 小时前
C++模板进阶——特化全解
javascript·c++·jquery