原生js实现伞状动画效果

代码语言:html

所属分类:动画

代码描述:原生js实现伞状动画效果

代码标签: 伞状 动画 效果

下面为部分代码预览,完整代码请点击下载或在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: #FCFAEE;
}
</style>

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

<script>
/*
* File Name / wagasa.js
* Created Date / Aug 11, 2020
* Aurhor / Toshiya Marukubo
* Twitter / https://twitter.com/toshiyamarukubo
*/

(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 shapes = [];
    var rad = Math.PI * 2 / 36;
    var style = {
      black: 'black',
      white: 'white',
      lineWidth: 4 };

    var colors = [
    'rgb(77, 121, 155)',
    'rgb(248, 215, 205)',
    'rgb(177, 201, 179)'];


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

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

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

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

    Shape.prototype.init = function (x, y, r) {
      this.x = x;
      this.y = y;
      this.r = 1;
      this.ri = r;
      this.c = colors[rand(0, colors.length - 1)];
      this.num = rand(0, 2);
      this.a = rand(0, 360);
      this.rad = this.a * Math.PI / 180;
      this.random = Math.random();
    };

    Shape.prototype.draw = function () {
      var ctx = this.ctx;
      ctx.save();
      ctx.fillStyle = this.c;
      ctx.lineWidth = this.r / 100;
      ctx.strokeStyle = style.white;
      ctx.translate(this.x, this.y);
      ctx.rotate(Math.sin(Math.sin(this.rad)));
      ctx.translate(-this.x, -this.y);
      ctx.beginPath();
      for (var i = 0; i < 36; i++) {
        var x = Math.cos(rad * i) * this.r + this.x;
        var y = Math.sin(rad * i) * this.r + this.y;
        if (i === 0) {
          ctx.moveTo(x, y);
        } else {
          ctx.lineTo(x, y);
        }
      }
      ctx.closePath();
      ctx.fill();
      ctx.stroke();
      for (var i = 0; i < 36; i++) {
        var x = Math.cos(rad * i) * this.r * 1.01 + this.x;
        var y = Math.sin(rad * i) * this.r * 1.01 + this.y;
        ctx.beginPath();
        ctx.moveTo(this.x, this.y);
        ctx.lineTo(x, y);
        ctx.stroke();
      }
      ctx.strokeStyle = 'rgb(252, 250, 238)';
      // pattern
      switch (this.num) {
        case 0:
          this.drawCircle();
          break;
        case 1:
          this.doubleCircle();
          break;
        default:
          break;}

      ctx.restore();
    };

    Shape.prototype.drawCircle = function () {
      ctx.lineWidth = this.r / 5;
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.r / 2, 0, Math.PI * 2, false);
      ctx.stroke();
    };

    Shape.prototype.doubleCircle =.........完整代码请登录后点击上方下载按钮下载查看

网友评论0