电路板流动动画效果

代码语言:html

所属分类:动画

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

<!DOCTYPE html>
<html lang="en">
<head>
<title>CodePen - Techy Nodes</title>
<style>
      html, body {
  height: 100%;
  overflow: hidden;
}

canvas {
  line-height: 0;
  margin-bottom: 0;
}

.nodes-canvas {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}

.cache-canvas {
  background-color: #6622ff;
}

.main-canvas {
  opacity: 1;
}

.connection-paths {
  display: none;
  position: absolute;
  top: 0;
  left: 50%;
  height: 100%;
  width: auto;
  -webkit-transform: translate(-50%);
          transform: translate(-50%);
}

.connection-path {
  stroke: white;
  stroke-width: 2px;
  fill: none;
}

.center {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
          transform: translate(-50%, -50%);
}

.message-text {
  text-align: center;
  color: #6622ff;
  font-size: 30vw;
  font-weight: 700;
  letter-spacing: -0.06em;
  line-height: 0;
}

.emblem {
  border-radius: 50%;
  height: 35vw;
  width: 35vw;
  background-color: #32e4b0;
}

    </style>

</head>
<body translate="no">
<canvas id="cache-canvas" class="nodes-canvas cache-canvas"></canvas>
<canvas id="main-canvas" class="nodes-canvas main-canvas"></canvas>
<script src='http://repo.bfw.wiki/bfwrepo/js/dat.gui.min.js'></script>
<script>
      const cacheCanvas = document.getElementById('cache-canvas');
const canvas = document.getElementById('main-canvas');
const cacheCtx = cacheCanvas.getContext('2d');
const ctx = canvas.getContext('2d');

const __bounds = {};
const __dpr = window.devicePixelRatio || 1;
let __nodePaths = [];
let __shouldDraw = true;
let __isDrawing = false;
const EPSILON = 0.0001;


const pathFunctions = {
  spider: function (startX, startY, endX, endY, distance) {
    return [
    startX,
    startY,
    startX + (Math.random() * 2 - 1) * distance,
    startY + (Math.random() * 2 - 1) * distance,
    endX + (Math.random() * 2 - 1) * distance,
    endY + (Math.random() * 2 - 1) * distance,
    endX,
    endY];

  },

  techy: function (startX, startY, endX, endY, distance) {
    const diffX = endX - startX;
    const diffY = endY - startY;
    const angle = Math.atan2(diffY, -diffX) + Math.PI * 1.25;

    const ret = [startX, startY,,, endX, endY];

    //if above or below point
    if (angle % Math.PI < Math.PI / 2) {
      if (Math.random() > 0.5) {
        ret[2] = endX - Math.abs(diffY) * Math.sign(diffX);
        ret[3] = startY;
      } else
      {
        ret[2] = startX + Math.abs(diffY) * Math.sign(diffX);
        ret[3] = endY;
      }
    }

    //if left or right of point
    else {
        if (Math.random() > 0.5) {
          ret[2] = startX;
          ret[3] = endY - Math.abs(diffX) * Math.sign(diffY);
        } else
        {
          ret[2] = endX;
          ret[3] = startY + Math.abs(diffX) * Math.sign(diffY);
        }
      }

    return ret;
  } };


const spring = {
  k: -8.5,
  b: -61 };


const settings = {
  grid: 100,
  pathFunction: pathFunctions['techy'] };


const tm = 200;
let tv = 0;
let tt = 0;
let ta = 0;
let t = 0.5;

function draw() {
  ctx.clearRect(__bounds.left, __bounds.top, __bounds.width, __bounds.height);
  let pt0, pt1;

  ctx.beginPath();
  const len = __nodePaths.length;
  for (let i = 0; i < len; i++) {
    plotPolyline(
    __nodePaths[i],
    clamp(t - 0.1, 0, 0.9999999),
    clamp(t + 0.1, 0, 0.9999999));

  }
  ctx.stroke();

  for (let i = 0; i < len; i++) {
    const p = __nodePaths[i];
    const ptsLen = p.length;
    ctx.beginPath();
    ctx.arc(
    p[0],
    p[1],
    Math.max(0, t * 10),
    0,
    Math.PI * 2);

    ctx.closePath();
    ctx.stroke();

    ctx.beginPath();
    ctx.arc(
    p[ptsLen - 2],
    p[ptsLen - 1],
    Math.max(0, t * 10),
    0,
    Math.PI * 2);

    ctx.closePath();
    ctx.fill();
  }
}

function loop() {
  requestAnimationFrame(loop);
  tv = tv + (spring.k * (t - tt) + spring.b * tv) / tm;
  if (Math.abs(tv) > EPSILON) {
    // if (t > 0.999999) tv *= -1;
    t = clamp(t + tv, 0, 0.999999);
    draw();
  }
}

function drawLines() {
  cacheCtx.clearRect(__bounds.left, __bounds.top, __bounds.width, __bounds.height);
  cacheCtx.beginPath();
  Array.prototype.forEach.call(__nodePaths, function (np) {
    cacheCtx.moveTo(np[0], np[1]);
    for (let i = 2, ii = np.length; i < ii; i += 2) {
      cacheCtx.lineTo(np[i], np[i + 1]);
    }
  });
  cacheCtx.stroke();
}

function plotPolyline(polyline, t1 = 0, t2 = 0.9999999) {
  const p0 = lerpPolyline(polyline, t1);.........完整代码请登录后点击上方下载按钮下载查看

网友评论0