gsap实现led灯矩阵旋转动画效果代码

代码语言:html

所属分类:动画

代码描述:gsap实现led灯矩阵旋转动画效果代码

代码标签: gsap led 矩阵 旋转 动画

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

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

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

  
<style>
*,
*:after,
*:before {
	box-sizing: border-box;
}

body {
	display: grid;
	place-items: center;
	min-height: 100vh;
	font-family:  'Google Sans', sans-serif, system-ui;
	background: black;
}

canvas {
	width: 48vmin;
	aspect-ratio: 1;
	background: black;
}
</style>


  
  
</head>

<body>
  <canvas></canvas>

  <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/gsap.3.11.0.js"></script>
      <script  type="module">


gsap.defaults({ ease: "none" });

const NUMBER_OF_CELLS = 24;

const CANVAS = document.querySelector("canvas");
const CONTEXT = CANVAS.getContext("2d");
const RATIO = window.devicePixelRatio || 1;

const HUE_ONE = 10;
const HUE_TWO = 50;

CANVAS.width = CANVAS.offsetWidth * RATIO;
CANVAS.height = CANVAS.offsetHeight * RATIO;

const CELL_SIZE = CANVAS.width / NUMBER_OF_CELLS;
const CELL_BORDER = CELL_SIZE * 0.1;

const CELL_MAP = [];
const CELLS = [];
const NUMBER_OF_TRAILS = NUMBER_OF_CELLS * 0.5;

// How do you model the two square growth?
// 1. Each cell is two rects
// 2. One red, one yellow
// 3. One scales before the other so you have two scales
// 4. Yellow changes to white
// 5. They both scale down...

for (let t = 0; t < NUMBER_OF_TRAILS; t++) {
  // In here generate the trails starting at x which will be the half way point?
  // Given the trail number, you can work out the length of a side, etc.
  const side = NUMBER_OF_CELLS - t * 2;
  const total = side * 4 - 4;
  const TRAIL_CELLS = [];
  const START = {
    x: NUMBER_OF_CELLS * 0.5,
    y: t
  };
  const LIMIT = {
    x: [t, NUMBER_OF_CELLS - t],
    y: [t, NUMBER_OF_CELLS - t]
  };
  // console.info({ t, side, START, total })
  let x = NUMBER_OF_CELLS * 0.5;
  let y = t;
  for (let c = 0; c < total; c++) {
    TRAIL_CELLS.push({
      index: c,
      x,
      y,
      lightness: 50,
      scaleOne: 0,
      scaleTwo: 0,
      scale: 0,
      trail: t
    });

    const TOP_SIDE = side * 0.5 - 1;
    const RIGHT_SIDE = TOP_SIDE + (side - 1);
    const BOTTOM_SIDE = RIGHT_SIDE + (side - 1);
    const LEFT_SIDE = BOTTOM_SIDE + (side - 1);

    if (c < TOP_SIDE) {
      x += 1;
    } else if (c >= TOP_SIDE && c < RIGHT_SIDE) {
      y += 1;
    } else if (c >= RIGHT_SIDE && c < BOTTOM_SIDE) {
      x -= 1;
    } else if (c >= BOTTOM_SIDE && c < LEFT_SIDE) {
      y -= 1;
    } else {
      x += 1;
    }
  }
  CELLS.push(TRAIL_CELLS);
}
// const RENDER_CELLS = [...CELLS[0]]
const RENDER_CELLS = CELLS.flat();

const TRAIL_TIMELINES = [];

const DRAW = () => {
  CONTEXT.clearRect(0, 0, CANVAS.width, CANVAS.height);
  for (const CELL of RENDER_CELLS) {
    // const HUE = (360 / (NUMBER_OF_CELLS * 0.5)) * CELL.trail

    // Draw the red rectangle first
    CONTEXT.fillStyle = `hsl(${HUE_ONE} 100% ${CELL.lightness}%)`;
    CONTEXT.fillRect(
      CELL.x * CELL_SIZE +
        CELL_BORDER +
        (CELL_SIZE - CELL_SIZE * CELL.scaleOne) * 0.5,
      CELL.y * CELL_SIZE +
     .........完整代码请登录后点击上方下载按钮下载查看

网友评论0