原生js实现新冠状病毒传播风险模拟粒子碰撞效果

代码语言:html

所属分类:动画

代码描述:原生js实现新冠状病毒传播风险模拟粒子碰撞效果,电脑模拟显示待在家中才是最安全的

代码标签: 新冠状 ( 冠状 ) 病毒 传播 风险 模拟 粒子 碰撞 效果

下面为部分代码预览,完整代码请点击下载或在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: #FFFFFF;
  font-size: 100%;
  padding: 0;
  margin: 0;
  letter-spacing: 0.2rem;
  overflow: hidden;
  background: #46B3B3; 
}

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;
}

p#loading {
  position: absolute;
  bottom: 0;
  right: 0;
  padding: 1.6rem;
}

/********************
  Contents
********************/

canvas#canvas {
  background: #46B3B3; 
}

div.controller {
  position: absolute;
  bottom: 1.6rem;
  left: 1.6rem;
}

div.controller > p {
  font-size: 2.6rem;
}
</style>

</head>
<body translate="no">
<header id="header">
<h1>stayHome</h1>
<p>Created date / April 12, 2020</p>
<p><a href="../../index.html">Back</a></p>
</header>

<main id="main">
<canvas id="canvas">This browser cannot use a canvas.</canvas>
<div class="controller">
<p>Healthy <span id="countHealthy">63</span><br>Sick <span id="countSick">1</span></p>
<button id="resetBtn">Reset</button>
<button id="stayBtn">Stay Home</button>
</div>
</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
      ********************/

    var resetBtn = document.getElementById('resetBtn');
    var stayBtn = document.getElementById('stayBtn');
    var stay = false;
    var countSick = document.getElementById('countSick');
    // canvas 
    var ctx = canvas.getContext('2d');
    var X = canvas.width = window.innerWidth;
    var Y = canvas.height = window.innerHeight;

    var splitNum = 16;

    if (X < 768) {
      splitNum = 8;
    }

    var xSplit = X / splitNum;
    var ySplit = Y / splitNum;
    var sick = 1;
    var healthy = splitNum * splitNum - 1;

    /********************
                                             Animation
                                           ********************/

    window.requestAnimationFrame =
    window.requestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function (cb) {
      setTimeout(cb, 17);
    };

    /********************
         Circle
       ********************/

    // var
    var rowMax = splitNum;
    var colMax = splitNum;
    var circles = [];

    function Circle(ctx, x, y, c) {
      this.ctx = ctx;
      this.init(x, y, c);
    }

    Circle.prototype.init = function (x, y, c) {
      this.ctx = ctx;
      this.x = x;
      this.y = y;
      this.x1 = this.x;
      this.y1 = this.y;
      this.v = {
        x: rand(-1, 1),
        y: rand(-1, 1) };

      this.c = c;
      this.r = ySplit / 8;
    };

    Circle.prototype.draw = function () {
      var ctx = this.ctx;
      ctx.beginPath();
      ctx.fillStyle = this.c;
      ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);
      ctx.closePath();
      ctx.fill();
    };

    Circle.prototype.stayHome = function () {
      this.v.x = 0;
      this.v.y = 0;
    };

    function c.........完整代码请登录后点击上方下载按钮下载查看

网友评论0