pixi实现canvas流体粒子鼠标交互动画效果代码

代码语言:html

所属分类:粒子

代码描述:pixi实现canvas流体粒子鼠标交互动画效果代码

代码标签: pixi canvas 流体 粒子 鼠标 交互 动画

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

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

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

  
  
<style>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  overflow: hidden;
}

#mock-canvas {
  position: absolute;
  z-index: 99;
  background-color: white;
  display: none;
}
</style>


  
</head>

<body>
  <canvas id="mock-canvas"></canvas>
<div class="canvas-wrap"></div>
  <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/pixi.7.2.4.js"></script>
      <script  type="module">

const app = new PIXI.Application({ resizeTo: window });
const canvasWrap = document.querySelector('.canvas-wrap');
canvasWrap.appendChild(app.view);

let countDown = 600;
let intervalId = -1;
let textPoints = [];
const MOCK_CANVAS_WIDTH = 90;
const MOCK_CANVAS_HEIGHT = 45;
const mockCanvas = document.getElementById("mock-canvas");
mockCanvas.width = MOCK_CANVAS_WIDTH;
mockCanvas.height = MOCK_CANVAS_HEIGHT;
const drawTime = () => {
  textPoints = [];
  const ctx = mockCanvas.getContext("2d");
  ctx.clearRect(0, 0, MOCK_CANVAS_WIDTH, MOCK_CANVAS_HEIGHT);
  ctx.font = "48px Roboto";
  const text = ('00' + countDown).substr(-3);
  ctx.fillText(text, 0, 40);

  const imgData = ctx.getImageData(0, 0, MOCK_CANVAS_WIDTH, MOCK_CANVAS_HEIGHT).data;
  let x;
  let y;
  const centerX = MOCK_CANVAS_WIDTH / 2;
  const centerY = MOCK_CANVAS_HEIGHT / 2;
  for (y = 0; y < MOCK_CANVAS_HEIGHT; y++) {
    for (x = 0; x < MOCK_CANVAS_WIDTH; x++) {
      var i = (MOCK_CANVAS_WIDTH * y + x) * 4;
      var a = imgData[i + 3];
      if (a !== 0) {
        // console.log(x, y);
        textPoints.push({
          x: x - centerX,
          y: y - centerY });

      }
    }
  }

  countDown -= 5;
  if (countDown < 0) {
    clearInterval(intervalId);
    return;
  }
};
drawTime();
intervalId = setInterval(() => {
  drawTime();
}, 5000);

const sprites = new PIXI.Container();
app.stage.addChild(sprites);

const getRandomColor = () => {
  const o = Math.round;
  const r = Math.random;
  const s = 255;
  return `rgb(${o(r() * s)},${o(r() * s)},${o(r() * s)})`;
};

const arr = [];
const TOTAL_SPRITES = app.renderer instanceof PIXI.Renderer ? 5000 : 200;
for (let i = 0; i < TOTAL_SPRITES; i++) {
  const g = new PIXI.Graphics();
  g.beginFill(getRandomColor());
  g.drawCircle(0, 0, 4);
  g.x = Math.random() * app.screen.width;
  g.y = Math.random() * app.screen.height;
  g.vx = g.vy = 0;
  g.size = 1;
  // g.alpha = 0.1;
  // g.blendMode = PIXI.BLEND_MODES.ADD;
  sprites.addChild(g);
  arr.push(g);
}

const FRICTION = 0.96;
let screenW = window.innerWidth;
let screenH.........完整代码请登录后点击上方下载按钮下载查看

网友评论0