页面彩带效果

直接引入以下js文件:

function loadJS(url, callback) {
  var script = document.createElement('script')
  var fn = callback || function () { };
  script.type = 'text/javascript';
  if (script.readyState) {
    script.onreadystatechange = function () {
      if (script.readyState == 'loaded' || script.readyState == 'complete') {
        script.onreadystatechange = null;
        fn();
      }
    };
  } else {
    script.onload = function () {
      fn();
    };
  }
  script.src = url;
  document.getElementsByTagName('head')[0].appendChild(script);
}

!(function () {
  loadJS("https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js", () => {
    caidai();
  })
})();
const caidai = () => {
  $("body").css({ "margin": "0", "padding": "0"})
  .append($("<canvas id='canvascaidai'></canvas>"));
  $("#canvascaidai").css({ "width": "100vw", "height": "100vh", "position": "fixed", "top": "0", "left": "0", "z-index": "-100" });
  document.addEventListener('touchmove', event => event.preventDefault());
  let ctx = canvascaidai,
    c = ctx.getContext('2d'),
    basePixel = window.devicePixelRatio || 1,
    width = window.innerWidth,
    height = window.innerHeight,
    startOffset = 90,
    coordinateArr,
    startRadius = 0,
    radius = Math.PI * 2;
  ctx.width = width * basePixel,
    ctx.height = height * basePixel;
  c.scale(basePixel, basePixel),
    c.globalAlpha = 0.6;
  document.onclick = initialPotray;
  document.ontouchstart = initialPotray;
  initialPotray();
  function initialPotray() {
    c.clearRect(0, 0, width, height);
    // first
    coordinateArr = [
      { x: 0, y: height * .7 + startOffset },
      { x: 0, y: height * .7 - startOffset }
    ];
    while (coordinateArr[1].x < width + startOffset) drawTraingleImage(coordinateArr[0], coordinateArr[1]);
  }
  function drawTraingleImage(coor1, coor2) {
    c.beginPath();
    c.moveTo(coor1.x, coor1.y);
    c.lineTo(coor2.x, coor2.y);
    let coor3 = {
      x: coor2.x + (Math.random() * 2 - 0.25) * startOffset,
      y: reCalculate(coor2.y)
    };
    c.lineTo(coor3.x, coor3.y);
    c.closePath();
    startRadius -= radius / -50;
    c.fillStyle = '#' + (
      Math.cos(startRadius) * 127 + 128 << 16 |
      Math.cos(startRadius + radius / 3) * 127 + 128 << 8 |
      Math.cos(startRadius + radius / 3 * 2) * 127 + 128
    ).toString(16);

    c.fill();
    coordinateArr[0] = coordinateArr[1],
      coordinateArr[1] = coor3;
  }
  function reCalculate(yNum) {
    let tmp = yNum + (Math.random() * 2 - 1.1) * startOffset;
    return (tmp > height || tmp < 0) ? reCalculate(yNum) : tmp;
  }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小钱要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值