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 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.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 start() {
  if (stopped) {
    requestAnimationFrame(animate);
    stopped = false;
  } else {
    stopped = true;
  }
}
ctx.canvas.addEventListener("click", start, false);

var stopped = true;
var t = 0;
var S = 2;
var s = 0;
function animate(ts) {
  if (stopped) return;
  t++;
  if (t < dur) draw();
  if (t == dur && ++s < S) {
    reset();
    t = 0;
    //if (EM) stopped=true;
  }
  if (t > dur + 240) {
    ctx.canvas.style.opacity = 1 - (t - dur - 240) / 60;
  }
  if (t > dur + 300) {
    reset();
    t = 0;
    s = 0;
    S = getRandomInt(1, 5);
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.clearRect(0, 0, 2 * CSIZE, 2 * CSIZE);
    ctx.canvas.style.opacity = 1;
  }
  requestAnimationFrame(animate);
}

onresize();

var dim = 50;
var F1 = 4 * Math.random();
var F2 = 4 * Math.random();
var F3 = 4 * Math.random();
var F4 = 4 * Math.random();
var P1 = 1 - 2 * Math.random();
var P2 = (1 - Math.abs(P1)) * [-1, 1][getRandomInt(0, 2)];
var P3 = 1 - 2 * Math.random();
var P4 = (1 - Math.abs(P3)) * [-1, 1][getRandomInt(0, 2)];
var dash1 = [];
var dash2 = [];
var reset = () => {
  F1 = 4 * Math.random();
  F2 = 4 * Math.random();
  F3 = 4 * Math.random();
  F4 = 4 * Math.random();
  P1 = 1 - 2 * Math.random();
  P2 = (1 - Math.abs(P3)) * [-1, 1][getRandomInt(0, 2)];
  P3 = 1 - 2 * Math.random();
  P4 = (1 - Math.abs(P3)) * [-1, 1][getRandomInt(0, 2)];
  K1 = 10 - 20 * Math.random();
  K2 = 10 - 20 * Math.random();
  if (Math.random() < 0.5) dash1 = [dim * Math.random(), dim * Math.random()];else
  dash1 = [];
  if (Math.random() < 0.5) dash2 = [dim * Math.random(), dim * Math.random()];else
  dash2 = [];
  setPattern();
};

var sq = 2 * CSIZE;
var getImageData = () => {
  let pi.........完整代码请登录后点击上方下载按钮下载查看

网友评论0