原生js实现泡泡球动画效果

代码语言:html

所属分类:动画

代码描述:原生js实现泡泡球动画效果

代码标签: 泡泡 动画 效果

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

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

<style>
* {
  margin: 0;
  padding: 0;
}

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

div.controller {
  position: absolute;
  bottom: 0;
  left: 0;
  padding: 1.6rem
}
</style>

</head>
<body translate="no">
<canvas id="canvas">Canvas not supported.</canvas>
<div class="controller">
<p><input type="range" id="count" min="1" max="300" step="1" value="200"></p>
</div>

<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 count = document.getElementById('count');
    var X = canvas.width = window.innerWidth;
    var Y = canvas.height = window.innerHeight;
    var mouseX = X / 2;
    var mouseY = Y / 2;
    var ease = 0.2;
    var friction = 0.9;
    var flg = false;

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

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

    /********************
         Particle
       ********************/

    var particleNum = count.value;
    var particles = [];

    if (X < 768) {
      particleNum = 100;
    }

    function Particle(ctx, x, y) {
      this.ctx = ctx;
      this.init(x, y);
    }
    Particle.prototype.init = function (x, y) {
      this.x = x;
      this.y = y;
      this.x1 = this.x;
      this.y1 = this.y;
      this.r = rand(5, 30);
      this.lw = this.r / 4;
      this.a = rand(0, 360);
      this.rad = this.a * Math.PI / 180;
      this.v = {
        x: 0,
        y: 0,
        x1: rand(-100, 100),
        y1: rand(-100, 100) };

      this.c = {
        r: rand(0, 255),
        g: rand(0, 255),
        b: rand(200, 255) };

      this.gc = {
        r: this.c.r * 1.5,
        g: this.c.g * 1.5,
        b: this.c.b * 1.5 };

    };
    Particle.prototype.draw = function () {
      var ctx = this.ctx;
      ctx.save();
      ctx.fillStyle = 'rgb(' + this.c.r + ', ' + this.c.g + ', ' + this.c.b + ')';
      ctx.beginPath();
   .........完整代码请登录后点击上方下载按钮下载查看

网友评论0