canvas随机图形变换动画效果代码

代码语言:html

所属分类:动画

代码描述:canvas随机图形变换动画效果代码

代码标签: canvas 随机 图形 变换 动画

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

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

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


  
  
</head>

<body translate="no">
  
  
      <script  >
"use strict"; // Paul Slaymaker, paul25882@gmail.com
const body = document.getElementsByTagName("body").item(0);
body.style.background = "#000";
const TP = 2 * Math.PI;
const CSIZE = 400;
const S6 = Math.sin(TP / 6);
const C6 = 0.5;

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);

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 = 160;
  const CT = 255 - CBASE;
  this.getRGB = o => {
    let red = Math.round(CBASE + CT * Math.cos(o + this.RK2 + t / this.RK1));
    let grn = Math.round(CBASE + CT * Math.cos(o + this.GK2 + t / this.GK1));
    let blu = Math.round(CBASE + CT * Math.cos(o + this.BK2 + t / this.BK1));
    return "rgb(" + red + "," + grn + "," + blu + ")";
  };
  this.randomize = () => {
    this.RK1 = 30 + 140 * Math.random();
    this.GK1 = 30 + 140 * Math.random();
    this.BK1 = 30 + 140 * Math.random();
    this.RK2 = TP * Math.random();
    this.GK2 = TP * Math.random();
    this.BK2 = TP * Math.random();
  };
  this.randomize();
}

var color = new Color();

function Point(xp, yp) {
  this.x = xp;
  this.y = yp;
  this.randomize = () => {
    this.K1 = 800 + 800 * Math.random();
    this.K2 = TP * Math.random();
  };
  this.randomize();
}

function Fraction() {
  this.randomize = () => {
    this.K1 = 800 + 800 * Math.random();
    this.K2 = TP * Math.random();
  };
  this.randomize();
  this.setFraction = () => {this.f = (1 + 0.8 * Math.sin(this.K2 + TP * t / this.K1)) / 2;};
}

var f1 = new Fraction();
var f2 = new Fraction();
var f3 = new Fraction();
var f4 = new Fraction();

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

var t = 0;
var animate = ts => {
  if (stopped) return;
  t++;
  draw();
  requestAnimationFrame(animate);
};

var pt1 = new Point(0, 0); // 3 moveable tiling points
var pt2 = new Point(0, 0);
var pt3 = new Point(0, 0);

const p1 = new Point(0, 0); // 8 drawing points
p1.setLocation = () => {
  p1.x = pt2.x * (1 + Math.sin(p1.K2 + TP * t / p1.K1)) / 2;
};
const p2 = new Point();
p2.setLocation = () => {
  let f = (1 + Math.sin(p2.K2 + TP * t / p2.K1)) / 2;
  p2.x = pt1.x * f;
  p2.y = .........完整代码请登录后点击上方下载按钮下载查看

网友评论0