js实现彩色圆盘canvas动画效果代码

代码语言:html

所属分类:动画

代码描述:js实现彩色圆盘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";
body.style.display = "grid";
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 = 2 * CSIZE;
  c.height = 2 * CSIZE;
  d.append(c);
  return c.getContext("2d");
})();
ctx.translate(CSIZE, CSIZE);
ctx.lineWidth = 0.4;

var 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;
  }
};

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

var Tile = function (p1, p2, p3, p4, i, ts) {
  this.v = [p1, p2, p3, p4];
  this.draw = f => {
    ctx.beginPath();
    ctx.moveTo(this.v[0].x, this.v[0].y);
    ctx.lineTo((1 - f) * this.v[1].x + f * this.v[3].x, (1 - f) * this.v[1].y + f * this.v[3].y);
    ctx.lineTo(this.v[2].x, this.v[2].y);
    ctx.lineTo((1 - f) * this.v[3].x + f * this.v[1].x, (1 - f) * this.v[3].y + f * this.v[1].y);
    ctx.closePath();
    //    ctx.stroke();
  };
};

var colors = [];
var setColors = () => {
  colors = [];
  let hue = getRandomInt(0, 360);
  let hd = getRandomInt(90, 270);
  let n = getRandomInt(3, 33);
  for (let i = 0; i < n; i++) {
    let sat = 80 + 20 * Math.random();
    //let lum=60+20*Math.random();
    let lum = 50 + 30 * Math.random();
    colors.push("hsl(" + (hue + i * hd) % 360 + "," + sat + "%," + lum + "%)");
  }
  (() => {
    let no = [];
    do {
      no.push(colors.splice(getRandomInt(0, colors.length), 1)[0]);
    } while (colors.length > 0);
    colors = no;
  })();
};

var TileSet = function (idx) {
  this.tiles = [];
  this.sat = 80 + 20 * Math.random();
  this.lum = 60 + 20 * Math.random();
  this.idx = idx;
  this.color = colors[idx % colors.length];
  if (sync) {
    this.os = syncM * Math.floor(idx / 2);
    this.rate = 400;
  } else {
    this.os = 0;
    this.rate = 200 + 400 * Math.random();
  }
  this.drawTiles = () => {
    ctx.fillStyle = this.color;
    for (let tile of this.tiles) {
      let f = (1 + Math.sin(this.os + TP * t / this.rate)) / 2;
      tile.draw(f);
      ctx.fill();
    }
  };
};

var W = 2; // layers+1
var C = 4; // radials
var pts .........完整代码请登录后点击上方下载按钮下载查看

网友评论0