欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

使用canvas怎么實現(xiàn)一個粒子動畫背景

使用canvas怎么實現(xiàn)一個粒子動畫背景?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

海州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

創(chuàng)建canvas

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

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

隨機生成一定數(shù)量的點坐標信息

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

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

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

// 隨機生成100個點的坐標信息
for (let i = 0; i < 100; i++) {
  points.push(this.getPoint());
}

將生成的點數(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移動點的坐標
    if(item.x > 0 && item.x < width && item.y > 0 && item.y < height) {
      item.x += item.rateX * rate;
      item.y += item.rateY * rate;
    } else {
      // 如果粒子運動超出了邊界,將這個粒子去除,同時重新生成一個新點。
      points.splice(i, 1);
      points.push(getPoint(radius));
    }
  });
}

畫線

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

// 計算兩點之間的距離
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;
  //對圓心坐標進行兩兩判斷
  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);

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

動畫

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

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

看完上述內容,你們掌握使用canvas怎么實現(xiàn)一個粒子動畫背景的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站標題:使用canvas怎么實現(xiàn)一個粒子動畫背景
文章位置:http://chinadenli.net/article18/jpdcgp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、電子商務品牌網(wǎng)站建設、網(wǎng)站排名網(wǎng)頁設計公司

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化