canvas线条变色旋转动画效果代码

代码语言:html

所属分类:动画

代码描述:canvas线条变色旋转动画效果代码

代码标签: canvas 线条 变色 旋转 动画

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

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

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

  
  


  
  
</head>

<body >
  
  
      <script  >
"use strict"; // Paul Slaymaker, paul25882@gmail.com
const body = document.getElementsByTagName("body").item(0);
body.style.background = "#000";
const EM = location.href.endsWith("em");
const TP = 2 * Math.PI;
const CSIZE = 400;

const ctx = (() => {
  let d = document.createElement("div");
  d.style.textAlign = "center";
  body.append(d);
  let c = document.createElement("canvas");
  c.width = c.height = 2 * CSIZE;
  d.append(c);
  return c.getContext("2d");
})();
ctx.translate(CSIZE, CSIZE);
ctx.lineCap = "round";

onresize = () => {
  let D = Math.min(window.innerWidth, window.innerHeight) - 40;
  ctx.canvas.style.width = ctx.canvas.style.height = D + "px";
};

const getRandomInt = (min, max, low) => {
  if (low) return Math.floor(Math.random() * Math.random() * (max - min)) + min;else
  return Math.floor(Math.random() * (max - min)) + min;
};

function Color() {
  const CBASE = 144;
  const CT = 255 - CBASE;
  this.getRGB = ct => {
    let red = Math.round(CBASE + CT * Math.cos(this.RK2 + ct / this.RK1));
    let grn = Math.round(CBASE + CT * Math.cos(this.GK2 + ct / this.GK1));
    let blu = Math.round(CBASE + CT * Math.cos(this.BK2 + ct / this.BK1));
    return "rgb(" + red + "," + grn + "," + blu + ")";
  };
  this.randomize = () => {
    this.RK1 = 360 + 360 * Math.random();
    this.GK1 = 360 + 360 * Math.random();
    this.BK1 = 360 + 360 * Math.random();
    this.RK2 = TP * Math.random();
    this.GK2 = TP * Math.random();
    this.BK2 = TP * Math.random();
  };
  this.randomize();
}

var color = new Color();

var stopped = true;
var start = () => {
  if (stopped) {
    stopped = false;
    requestAnimationFrame(animate);
  } else {
    stopped = true;
  }
};
body.addEventListener("click", start, false);

var tt = 0;
var pause = 0;
var gs = true;
var animate = ts => {
  if (stopped) return;
  tt++;
  if (tt % 18 == 0) {
    if (gs) {
      grow();
      if (ca.length > 160) gs = false;
    } else {
      ca.pop();
      if (ca.length < 2) {
        gs = true;
        color.randomize();
        KF = 0.07 + 0.06 * Math.random();
        sym = [getRPath, getHexPath][getRandomInt(0, 2)];
      }
    }
  }
  draw();
  requestAnimationFrame(animate);
};

var Circle = function () {
  this.dir = false;
  this.r = 16;
  this.lvl = 0;
  this.randomize = () => {
    this.kr2 = 150 + 300 * Math.random();
    this.ka = TP * Math.random();
    this.ka2 = 150 + 300 * Math.random();
  };
  this.randomize();
  this.setRA = () => {
    this.r = 16 + 12 * Math.sin(tt / this.kr2);
    this.a2 = TP / 6 + 2.09 * (1 + Math.sin(this.ka + tt / this.ka2)) / 2;
    if (this.dir) this.a2 = -this.a2;
  };
  this.setPath2 = () => {
    if (this.p) {
      if (this.cont) {
        th.........完整代码请登录后点击上方下载按钮下载查看

网友评论0