canvas实现多彩线条交汇变形动画效果代码

代码语言:html

所属分类:动画

代码描述:canvas实现多彩线条交汇变形动画效果代码

代码标签: canvas 多彩 线条 交汇 变形 动画

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

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

<head>
    <meta charset="UTF-8">

    <style>
        body {
    			background-color: black;
    			margin: 0;
    			overflow: hidden;
    		}
    </style>
</head>

<body>
 
    <canvas id="canvas"></canvas>
  
    <script>
        var PHI = (1 + Math.sqrt(5)) / 2, // 1.618033988749895
      maxGeneration = (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) ? 5 : 6, // Let's go easy on Firefox
      frameDuration = 1000 / 60,
      duration = 3000,
      rotationSpeed = 0.3,
      totalIterations = Math.floor(duration / frameDuration),
      maxBaseSize = 100,
      baseSizeSpeed = 0.02;
    
    var canvas = document.getElementById("canvas"),
      ctx = canvas.getContext("2d"),
      canvasWidth = document.documentElement.clientWidth,
      canvasHeight = document.documentElement.clientHeight,
      shapes = [],
      sizeVariation,
      iteration = 0,
      animationDirection = 1,
      sizeVariationRange = .15,
      baseRotation = 0,
      baseSize = 50,
      color = new Color(43, 205, 255);
    
    canvas.setAttribute("width", canvasWidth);
    canvas.setAttribute("height", canvasHeight);
    
    function Color(r, g, b) {
      this.r = r;
      this.g = g;
      this.b = b;
      this.rS = this.gS = this.bS = 1;
    }
    
      Color.prototype.change = function() {
        if (this.r == 0 || this.r == 255) this.rS *= -1;
        if (this.g == 0 || this.g == 255) this.gS *= -1;
        if (this.b == 0 || this.b == 255) this.bS *= -1;
        this.r += this.rS;
        this.g += this.gS;
        this.b += this.bS;
      }
    
    function Shape(gen, x, y, size, rotation) {
      this.generation = gen;
      this.size = size;
      this.rotation = -rotation;
      this.start = {
        x: x,
        y: y
      };
      this.end = {
        x_1: this.start.x + Math.cos(degToRad(this.rotation)) * this.size,
        y_1: this.start.y + Math.sin(degToRad(this.rotation)) * this.size,
        x_2: this.start.x + Math.cos(degToRad(this.rotation + 360 / 3)) * this.size,
        y_2: this.start.y + Math.sin(degToRad(this.rotation + 360 / 3)) * this.size,
        x_3:
          this.start.x +
          Math.cos(degToRad(this.rotation + 360 / 3 * 2)) * this.size,
        y_3:
          this.start.y + Math.sin(degToRad(this.rotation + 360 / 3 * 2)) * this.size
      };
    
      this.init();
    }
    
    var S = Shape.prototype;
    
    S.init = function() {
      if (this.generation < maxGeneration) {
        var gen = this.generation + 1,
          newSize = this.size * sizeVariati.........完整代码请登录后点击上方下载按钮下载查看

网友评论0