canvas实现夜晚竹林萤火虫闪烁飞行动画效果代码

代码语言:html

所属分类:动画

代码描述:canvas实现夜晚竹林萤火虫闪烁飞行动画效果代码

代码标签: canvas 夜晚 竹林 萤火虫 闪烁 飞行 动画

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

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
<style>
    * {
  font-family: 'arial black', Helvetica, verdana, monospace, sans-serif;
  letter-spacing: 0.2rem;
  margin: 0;
  padding: 0;
  color: #FFF;
  overflow: hidden;
}

body {
  position: relative;
}

canvas#canvas {
  display: block;
  background: #00222B;
}
</style>

</head>

<body>
    <!-- partial:index.partial.html -->
    <canvas id="canvas">Canvas not supported.</canvas>
    <!-- partial -->
    <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 ctx = canvas.getContext('2d');
    var X = canvas.width = window.innerWidth;
    var Y = canvas.height = window.innerHeight;
    var shapeNum = 500;
    var shapes = [];
    var style = {
      black: 'black',
      white: 'white',
      lineWidth: 4,
    };
    
    if (X <768) {
      shapeNum = 250;
    }

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

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

    /********************
      Bamboo
    ********************/
    
    var bamboos = [];

    function Bamboo(ctx, x, y, w, h, n) {
      this.ctx = ctx;
      this.init(x, y, w, h, n);
    }
    
    Bamboo.prototype.init = function(x, y, w, h, n) {
      this.x = x;
      this.y = y;
      this.w = w;
      this.h = h;
      this.n = n;
      this.d = 3;
      this.a = rand(-5, 5) * Math.random();
      this.rad = this.a * Math.PI / 180;
      this.random = Math.random() * 2;
    };

    Bamboo.prototype.draw = function() {
      var ctx  = this.ctx;
      ctx.save();
      ctx.fillStyle = style.black;
      ctx.globalAlpha = this.random;
      ctx.translate(this.x, this.y);
      //ctx.rotate(0.01 * Math.sin(this.rad));
      ctx.rotate(this.rad);
      ctx.translate(-this.x, -this.y);
      for (var i = 0; i < this.n; i++) {
        ctx.beginPath();
        ctx.moveTo(this.x - this.w / 2, this.y - this.h * i - this.d * i);
        ctx.quadraticCurveTo(this.x - this.w / 5, this.y - this.h * i - this.d * i - this.h / 2, this.x - this.w / 2, this.y - this.h * i - this.d * i - this.h);
        ctx.lineTo(this.x + this.w / 2, this.y - this.h * i - this.d * i- this.h);
        ctx.quadraticCurveTo(this.x + this.w / 5, this.y - this.h * i - this.d * i - this.h / 2, this.x + this.w / 2, this.y - this.h * i - this.d * i);
        ctx.closePath();
        ctx.fill();
      }
      ctx.restore();
    };

    Bamboo.prototype.render = function() {
      this.draw();
    };

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

网友评论0