原生js实现canvas大风车和稻谷丰收动画效果代码

代码语言:html

所属分类:动画

代码描述:原生js实现canvas大风车和稻谷丰收动画效果代码

代码标签: canvas 大风车 稻谷 丰收 动画 效果

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

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

<head>

  <meta charset="UTF-8">
  

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

canvas#canvas {
  display: block;
  background: #FFEFBA;  /* fallback for old browsers */
  background: -webkit-linear-gradient(to bottom, #FFFFFF, #FFEFBA);  /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to bottom, #FFFFFF, #FFEFBA); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}
</style>



</head>

<body translate="no" >
  <canvas id="canvas">Canvas not supported.</canvas>

  
      <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 mouseX = X / 2;
    var mouseY = Y / 2;
    var shapes = [];
    var shapeNum = 1500;
    var windmills = [];
    var windmillNum = 5;
    var xSplit = X / 4;

    if (X < 768) {
      shapeNum = 700;
      windmillNum = 3;
      xSplit = X / 2;
    }

    /********************
        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.i = i;
      this.r = rand(50, 150);
      this.c = rand(40, 60);
      this.a = i;
      this.rad = this.a * Math.PI / 180;
      this.lw = Math.random();
    };

    Shape.prototype.draw = function () {
      var ctx = this.ctx;
      ctx.save();
      ctx.lineWidth = 1;
      ctx.strokeStyle = 'hsl(' + this.c + ', 90%, 70%)';
      ctx.beginPath();
      ctx.moveTo(this.x, this.y);
      ctx.quadraticCurveTo(Math.cos(this.rad) * 10 + this.x, this.y - this.r / 2, Math.sin(this.rad) * 10 + this.x, this.y - this.r);
      ctx.stroke();
      ctx.restore();
    };

    Shape.prototype.updateParams = function () {
      this.a += 1;
      this.rad = this.a * Math.PI / 180;
    };

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

    for (var i = 0;.........完整代码请登录后点击上方下载按钮下载查看

网友评论0