gsap实现平行四边形穿插动画效果代码

代码语言:html

所属分类:动画

代码描述:gsap实现平行四边形穿插动画效果代码

代码标签: 穿插 动画 效果

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


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

<head>

  <meta charset="UTF-8">
  

  
  
  
<style>
html, body { height: 100%; }
body {
  margin: 0;
  display: grid;
  justify-items: center;
  align-items: center;
}
#container {
  width: 100vmin;
  height: 100vmin;
}
</style>



</head>

<body>
  <div id="container">
  <canvas></canvas>
</div>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/gsap.3.5.2.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/CustomEase3.js"></script>
      <script  >
const container = document.querySelector("#container");
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");

// Set in our resize function
let width, height, minX, maxX, minY, maxY, cellDiff, halfGridItemThickness, numColors, gridItems, parallelograms;

// Init some settings to use
const settings = {
  gridRows: 9,
  gridDotsPerCol: 5,
  clearColor: "#e0e0e0",
  gridItemColor: "#9e9e9e",
  gridItemThickness: 1,
  numParallelograms: 150,
  parallelogramColors: ["#fc705b", "#212f42", "#3f5c86"],
  shadowColor: "#443f3c" };


// Alternative theme from https://twitter.com/MAKIO135/status/1404488303102005250
// settings.clearColor = "#dfede0";
// settings.parallelogramColors = ["#dfede0", "#201f1e", "#f5ce4e"];

// Alternative theme from https://twitter.com/MAKIO135/status/1404397957810688013
// settings.clearColor = "#7be4e1";
// settings.parallelogramColors = ["#44c0d5", "#162c9b", "#fefac1"];
// settings.shadowColor = "#0f2034";


numColors = settings.parallelogramColors.length;
halfGridItemThickness = settings.gridItemThickness / 2;

// Some helper functions
const wrap01 = gsap.utils.wrap(0, 1);
const progressEase = CustomEase.create("custom", "M0,0,C0,0,0.3,0.5,0.5,0.5,0.7,0.5,1,1,1,1");
const normalizeUpperHalf = gsap.utils.normalize(0.5, 1);

// An animation that drives the progress of all other animations
const overallProgress = { val: 0 };
const progressAnim = gsap.to(overallProgress, {
  val: 1,
  repeat: -1,
  ease: "none",
  duration: 2 });


// An animation that rotates the pluses in the background grid
const rot = { val: 0 };
const rotAnim = gsap.to(rot, {
  val: Math.PI / 2,
  duration: 1,
  repeat: -1,
  repeatDelay: 3,
  ease: "none" });



// Creates our grid and parallelograms with the proper sizing
const resize = () => {
  gridItems = [];
  parallelograms = [];

  width = height = canvas.width = canvas.height = container.offsetWidth;
  cellDiff = width / (settings.gridRows * (settings.gridDotsPerCol - 1));

  minX = minY = width * 0.1;
  maxX = maxY = width * 0.9;

  createGridItems();
  createParallelograms();
};

// Throttle the resize event
let resizeTimeout;
const handleResize = () => {
  if (resizeTimeout) resizeTimeout.kill();

  resizeTimeout = gsap.delayedCall(0.3, resize);
};

// Create the grid background
const createGridItems = () => {
  for (let row = 0; row < settings.gridRows * (settings.gridDotsPerCol - 1); row++) {
    for (let col = 0; col < settings.gridRows * (settings.gridDotsPerCol - 1); col++) {
      // const index = col * settings.gridRows + row;
      const gridItem = {
        x: col * cellDiff + cellDiff / 2,
        y: row * cellDiff + cellDiff / 2,
        type: col % settings.gridDotsPerCol === 0 && row % settings.gridDotsPerCol === 0 ? "plus" : "dot" };

      gridItems.push(gridItem);
    }
  }
};

const getRestOfValues = parallelogram => {
  parallelogram.distanceX = parallelogram.horizDir * parallelogram.length;
  parallelogram.distanceY = parallelogram.vertDir * parallelogram.length;
  parallelogram.endX = parallelogram.startX + parallelogram.distanceX;
  parallelogram.endY = parallelogram.startY + parallelogram.distanceY;
  parallelogram.mirrorStartX = width - parallelogram.startX - parallelogram.thickness;
  parallelogram.mirrorEndX = parallelogram.mirrorStartX - parallelogram.distanceX;
};

// Create the foreground parallelograms
const createParallelograms = () => {
  for (let i = 0; i < settings.numParallelograms; i++) {
    // Randomly generate some parameters to use
    const parallelogram = {
      color: settings.parallelogramColors[i % numColors],
      startX: gsap.utils.random(0, width, 1),
      startY: gsap.utils.random(0, height, 1),
      horizDir: Math.random() < 0.5 ? 1 : -1, // Determines the angle of the p-gram
      vertDir: Math.random() < 0.5 ? 1 : -1, // Determines the way the p-gram "grows"
      thickness: gsap.utils.random(10, 50, 1),
      length: gsap.utils.random(width / 10, width / 2),
      progress: Math.random(),
      shadowOffset: gsap.utils.random(2, 10, 1) };


    getRestOfValues(parallelogram);

    // Cheap attempt to improve the choices of the horizonta.........完整代码请登录后点击上方下载按钮下载查看

网友评论0