matter实现粒子沙漏重力流动动画效果代码

代码语言:html

所属分类:动画

代码描述:matter实现粒子沙漏重力流动动画效果代码

代码标签: matter 粒子 沙漏 重力 流动 动画

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

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

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

  
  
<style>
body {
  margin: 0;
  overflow: hidden;
  font-family: "Poppins", sans-serif;
}

.btn {
  position: fixed;
  top: 4rem;
  left: 50%;
  padding: 0.7rem 1.4rem;
  border: 3px double;
  background: rgba(0, 0, 0, 0);
  font-size: 1.4rem;
  color: #55ccff;
  transform: translate(-50%, -50%);
  cursor: pointer;
}

.Me {
  position: fixed;
  z-index: 10;
  bottom: 20px;
  left: 50%;
  color: #fff;
  transform: translateX(-50%);
  font-weight: 700;
  opacity: 0.5;
}
</style>


  
  
</head>

<body >
  <button class="btn">Gravity</button>

<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/matter.0.19.0.js"></script>
      <script >
setTimeout((event) => {
  const rotateBtn = document.querySelector(".btn");

  function getRandom(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
  }

  // module aliases
  var Engine = Matter.Engine,
    Render = Matter.Render,
    Runner = Matter.Runner,
    Bodies = Matter.Bodies,
    Body = Matter.Body,
    Composite = Matter.Composite;

  // create an engine
  var engine = Engine.create();

  // create a renderer
  var render = Render.create({
    element: document.body,
    engine: engine,
    options: {
      width: window.innerWidth,
      height: window.innerHeight,
      wireframeBackground: "#fff",
      wireframes: false
    }
  });

  const nbElements = window.screen.width > 700 ? 1000 : 300;
  const particlesSize =
    window.screen.width > 700
      ? window.innerWidth / 150
      : window.innerWidth / 100;
  const boxes = Array.from({ length: nbElements }, () => {
    const elementType = getRandom(0, 2);
    const constructor = elementType ? Bodies.rectangle : Bodies.circle;
    const constructorData = elementType
      ? [particlesSize, particlesSize]
      : [particlesSize / 2];
    return constructor(
      getRandom(window.innerWidth / 2.6, window.innerWidth / 1.6),
      getRandom(
        window.innerHeight / 2 - window.innerWidth / 8,
        window.innerHeight / 2 - window.innerWidth / 4
      ),
      ...constructorData,
      {
        mass: 0.01,
        render: {
          fillStyle: `#${Math.floor(Math.random() * 16777215).toString(16)}`,
          strokeStyle: "rgba(0,0,0,0)"
        }
      }
    );
  });

  const lineParams = {
    width: window.innerWidth / 4,
    height: 2,
    options: {
      isStatic: true,
      render: {
        strokeStyle: "#55ccff",
        lineWidth: 2
      }
    }
  };
  const maskParams = {
    width: window.innerWidth / 4 + 60,
    height: 80,
    options: {
      isStatic: true,
      render: {
        fillStyle: "rgba(0,0,0,0)"
      }
    }
  };
  const hourglassData = [
    {
      // top
      x: window.innerWidth / 2,
      y: window.innerHeight / 2 - window.innerWidth / 4,
      ...lineParams
    },
    {
      // bottom
      x: window.innerWidth / 2,
      y: window.innerHeight / 2 + window.innerWidth / 4,
      ...lineParams
    },
    {
      // top left
      x: window.innerWidth / 2 - window.innerWidth / 8,
      y: window.innerHeight / 2 - window.innerWidth / 5.4,
      rotate: Math.PI / 2,
      ...lineParams,
      width: window.innerWidth / 7.4
    },
    {
      // top right
      x: window.innerWidth / 2 + window.innerWidth / 8,
      y: window.innerHeight / 2 - window.innerWidth / 5.4,
      rotate: Math.PI / 2,
      ...lineParams,
      width: window.innerWidth / 7.4
    },
    {
      // bottom left
      x: window.innerWidth / 2 - window.innerWidth / 8,
      y: window.innerHeight / 2 + window.innerWidth / 5.4,
      rotate: Math.PI / 2,
      ...lineParams,
      width: window.innerWidth / 7.4
    },
    {
      // bottom right
      x: window.innerWidth / 2 + window.innerWidth / 8,
      y: window.innerHeight / 2 + window.innerWidth / 5.4,
      rotate: Math.PI / 2,
      ...lineParams,
      width: window.innerWidth / 7.4.........完整代码请登录后点击上方下载按钮下载查看

网友评论0