原生js模拟粒子自由落体碰撞运动效果

代码语言:html

所属分类:粒子

代码描述:原生js模拟粒子自由落体碰撞运动效果

代码标签: 粒子 自由落体 碰撞 运动 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<style>
* {
  margin: 0;
  padding: 0;
  color: #000;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
}

canvas#canvas {
  display: block;
  background: #F4F5F7;
}

ul#navigation {
  position: absolute;
  height: 100%;
  top: 0;
  right: 0;
  text-align: center;
  margin-right: 1.6rem;
  font-size: 0.8rem;
  -webkit-writing-mode: vertical-rl;
      -ms-writing-mode: tb-rl;
          writing-mode: vertical-rl;
}

ul#navigation > li {
  display: inline-block;
}
</style>

</head>
<body translate="no">
<canvas id="canvas">Canvas not supported.</canvas>
<ul id="navigation">
<li>Scroll up, down, and click.</li>
</ul>

<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 colors = ['rgb(1, 1, 1)', 'rgb(78, 196, 193)', 'rgb(236, 86, 107)', 'rgb(229, 227, 53)'];
    var ctx = canvas.getContext('2d');
    var X = canvas.width = window.innerWidth;
    var Y = canvas.height = window.innerHeight;
    var mouseX = null;
    var mouseY = null;
    // shape
    var shapeMax = 1000;
    var shapes = [];
    var shapeNum = 1;
    var gravity = 0.3;
    var friction = 0.8;
    // wall
    var walls = [];
    var splitNum = 8;
    var split = X / splitNum;
    var yNum = Y / split;

    if (X < 768) {
      splitNum = 3;
      split = X / splitNum;
      yNum = Y / split;
    }

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

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

    /********************
         Shape
       ********************/

    function Shape(ctx, x, y, i) {
      this.ctx = ctx;
      this.init(x, y, i);
    }

    Shape.prototype.init = function (x, y, i) {
      this.x = x;
      this.y = y;
      this.r = 10;
      this.c = colors[rand(0, colors.length - 1)];
      this.v = {
        x: rand(-10, 10) * Math.random() * Math.random(),
        y: rand(-10, 10) * Math.random() * Math.random() };

    };

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

    Shape.prototype.wrapPosition = function () {
      if (this.x - this.r < 0) {
        this.x = 0 + this.r;
        this.v.x = -this.v.x;
      }
      if (this.x + this.r > X) {
        this.x = X - this.r;
        this.v.x = -this.v.x;
      }
      if (this.y - this.r < 0) {
        //this.v.y = - this.v.y;
      }
      if (this.y - this.r > Y) {
        this.y = 0 - this.r;
        //this.v.y *= friction;
        //this.v.y = - this.v.y;
        this.v = {
          x: rand(-10, 10) * Math.random() * Math.random(),
          y: rand(-10, 10) * Math.random() * Math.random() };

        if (shapes.length > 1000) {
          return;
        }
        var s = new Shape(ctx, rand(0, X), 0 - 100);
        shapes.push(s);
      }
    };

    Shape.prototype.collisionWall = function () {
      for (var i = 0; i < walls.length; i++) {
        var wall = walls[i];
        // left top
        if (this.r * this.r > (this.x - wall.x) * (this.x - wall.x) + (this.y - wall.y) * (this.y - wall.y)) {
          this.y = wall.y - this.r;
          this.v.y *= friction;
          this.v.y = -this.v.y;
          break;
        }
        // left down
        if (this.r * this.r > (this.x - wall.x) * (this.x - wall.x) + (this.y - wall.y - wall.wi) * (this.y - wall.y - wall.wi)) {
          this.x = wall.x - this.r;
          //this.v.y = - this.v.y;
          this.v.x = -this.v.x;
          break;
        }
        // right top
        if (this.r * this.r > (this.x - wall.x - wall.len) * (this.x - wall.x - wall.len) + (this.y - wall.y) * (this.y - wall.y)) {
          this.y = wall.y - this.r;
          this.v.y *= friction;
          this.v.y = -this.v.y;
          break;
        }
        // right down
        if (this.r * this.r > (this.x - wall.x - wall.len) * (this.x - wall.x - wall.len) + (this.y - wall.y - wall.wi) * (this.y - wall.y - wall.wi)) {
          this.x = wall.x + wall.len + this.r;
          //this.v.y = - this.v.y;
      .........完整代码请登录后点击上方下载按钮下载查看

网友评论0