佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > canvas粒子動畫背景的實(shí)現(xiàn)示例

canvas粒子動畫背景的實(shí)現(xiàn)示例

熱門標(biāo)簽:檢查注冊表項 使用U盤裝系統(tǒng) 美圖手機(jī) 智能手機(jī) 百度競價點(diǎn)擊價格的計算公式 網(wǎng)站建設(shè) 硅谷的囚徒呼叫中心 阿里云

效果 :)

不帶連線效果:

帶連線的效果:

教程

要實(shí)現(xiàn)這樣的效果其實(shí)很簡單,大概分為這么幾個步驟:

創(chuàng)建canvas

首先需要在需要展示粒子背景的父元素中創(chuàng)建一個canvas標(biāo)簽, 指定widthheight, 在我們生成隨機(jī)點(diǎn)坐標(biāo)的時候需要用widthheight來做參照。widthheight等于父元素的寬和高。

// 假如父元素是body
const ele = document.body;
const canvas = document.createElement('canvas');
canvas.width = ele.clientWidth;
canvas.height = ele.clientHeight;
// 將canvas標(biāo)簽插入
ele.appendChild(canvas);

隨機(jī)生成一定數(shù)量的點(diǎn)坐標(biāo)信息

widthheight做參照隨機(jī)生成一定數(shù)量的點(diǎn)坐標(biāo)信息,包含x, y, rateX(點(diǎn)在X軸的移動速率), rateY(點(diǎn)在Y軸移動的速率), rateXrateY決定了點(diǎn)的運(yùn)動軌跡。

const points = [];
// 隨機(jī)生成點(diǎn)的坐標(biāo),需指定radius的最大值
function getPoint(radius) {
  const x = Math.ceil(Math.random() * this.width), // 粒子的x坐標(biāo)
    y = Math.ceil(Math.random() * this.height), // 粒子的y坐標(biāo)
    r = +(Math.random() * this.radius).toFixed(4), // 粒子的半徑
    rateX = +(Math.random() * 2 - 1).toFixed(4), // 粒子在x方向運(yùn)動的速率
    rateY = +(Math.random() * 2 - 1).toFixed(4); // 粒子在y方向運(yùn)動的速率

  return { x, y, r, rateX, rateY };
}

// 隨機(jī)生成100個點(diǎn)的坐標(biāo)信息
for (let i = 0; i < 100; i++) {
  points.push(this.getPoint());
}

將生成的點(diǎn)數(shù)組畫到canvas上

function drawPoints() {
  points.forEach((item, i) => {
    ctx.beginPath();
    ctx.arc(item.x, item.y, item.r, 0, Math.PI*2, false);
    ctx.fillStyle = '#fff';
    ctx.fill();
    // 根據(jù)rateX和rateY移動點(diǎn)的坐標(biāo)
    if(item.x > 0 && item.x < width && item.y > 0 && item.y < height) {
      item.x += item.rateX * rate;
      item.y += item.rateY * rate;
    } else {
      // 如果粒子運(yùn)動超出了邊界,將這個粒子去除,同時重新生成一個新點(diǎn)。
      points.splice(i, 1);
      points.push(getPoint(radius));
    }
  });
}

畫線

遍歷點(diǎn)數(shù)組,兩兩比較點(diǎn)坐標(biāo),如果兩點(diǎn)之間距離小于某個值,在兩個點(diǎn)之間畫一條直線,lineWidth隨兩點(diǎn)之間距離改變,距離越大,線越細(xì)。

// 計算兩點(diǎn)之間的距離
function dis(x1, y1, x2, y2) {
  var disX = Math.abs(x1 - x2),
    disY = Math.abs(y1 - y2);

  return Math.sqrt(disX * disX + disY * disY);
}

function drawLines() {
  const len = points.length;
  //對圓心坐標(biāo)進(jìn)行兩兩判斷
  for(let i = 0; i < len; i++) {
    for(let j = len - 1; j >= 0; j--) {
      const x1 = points[i].x,
        y1 = points[i].y,
        x2 = points[j].x,
        y2 = points[j].y,
        disPoint = dis(x1, y1, x2, y2);

      // 如果兩點(diǎn)之間距離小于150,畫線
      if(disPoint <= 150) {
        ctx.beginPath();
        ctx.moveTo(x1, y1);
        ctx.lineTo(x2, y2);
        ctx.strokeStyle = '#fff';
        //兩點(diǎn)之間距離越大,線越細(xì),反之亦然
        ctx.lineWidth = 1 - disPoint / distance;
        ctx.stroke();
      }
    }
  }
}

動畫

使用requestAnimationFrame循環(huán)調(diào)用draw方法(draw方法里包含畫點(diǎn)和畫線),同時在draw的時候根據(jù)rateXrateY來改動點(diǎn)的位置。

// 調(diào)用draw函數(shù)開啟動畫
(function draw() {
  ctx.clearRect(0, 0, width, height);
  drawPoints();
  // 如果不需要畫線,取消下面這行代碼即可。
  drawLines();
  window.requestAnimationFrame(draw);
}());

完整代碼請看: https://github.com/PengJiyuan/particle-bg

我的Github:https://github.com/PengJiyuan

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:湖北 煙臺 通遼 山南 懷化 賀州 湘潭 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《canvas粒子動畫背景的實(shí)現(xiàn)示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    措美县| 广州市| 延长县| 灌南县| 张家川| 军事| 郸城县| 社旗县| 巴楚县| 平舆县| 肇源县| 和平区| 曲阜市| 华亭县| 乌兰察布市| 凌云县| 柞水县| 达拉特旗| 增城市| 尼玛县| 怀来县| 河北区| 贡觉县| 邻水| 姚安县| 邵阳市| 永福县| 抚松县| 北京市| 周口市| 盐城市| 仲巴县| 台安县| 崇左市| 朔州市| 汕头市| 大同市| 太仆寺旗| 鄂州市| 浦北县| 嘉黎县|