濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > canvas簡(jiǎn)單連線(xiàn)動(dòng)畫(huà)的實(shí)現(xiàn)代碼

canvas簡(jiǎn)單連線(xiàn)動(dòng)畫(huà)的實(shí)現(xiàn)代碼

熱門(mén)標(biāo)簽:地圖標(biāo)注軟件打印出來(lái) 惡搞電話(huà)機(jī)器人 黃石ai電銷(xiāo)機(jī)器人呼叫中心 高德地圖標(biāo)注商戶(hù)怎么標(biāo) 欣鼎電銷(xiāo)機(jī)器人 效果 智能電銷(xiāo)機(jī)器人被禁用了么 如何查看地圖標(biāo)注 電話(huà)機(jī)器人技術(shù) ok電銷(xiāo)機(jī)器人

前言:canvas動(dòng)畫(huà)入門(mén)系列之簡(jiǎn)單連線(xiàn)動(dòng)畫(huà)。雖然簡(jiǎn)單,但連線(xiàn)動(dòng)畫(huà)應(yīng)用場(chǎng)景還挺多,因此做了個(gè)小demo,一通百通。

step1:繪制點(diǎn)

首先創(chuàng)建個(gè)標(biāo)簽<canvas id="canvas"></canvas>
設(shè)置幾個(gè)點(diǎn)的坐標(biāo)

   const points = [
        [200, 100], //上
        [300, 200], //右
        [100, 200], //左
        [200, 100], //上
        [200, 300], //下
        [100, 200], //左
        [300, 200], //右
        [200, 300]
      ];
      const canvas = document.querySelector("canvas");
      const ctx = canvas.getContext("2d");

然后把點(diǎn)給畫(huà)出來(lái)

points.forEach(([x, y]) => {
          drawDot(x, y);
        });
function drawDot(x1, y1, r) {
          ctx.save();
          ctx.beginPath(); //不寫(xiě)會(huì)和線(xiàn)連起來(lái)
          ctx.fillStyle = "red";
          //繪制成矩形
          ctx.arc(x1, y1, r ? r : 2, 0, 2 * Math.PI);
          ctx.fill();
          ctx.restore();
        }

step2:繪制線(xiàn)條

我們封裝一個(gè)方法,傳入起點(diǎn)終點(diǎn),繪制一根線(xiàn)條

function drawLine(x1, y1, x2, y2) {
          ctx.save();
          ctx.beginPath(); //不寫(xiě)每次都會(huì)重繪上次的線(xiàn)
          ctx.lineCap = "round";
          ctx.lineJoin = "round";
          var grd = ctx.createLinearGradient(x1, y1, x2, y2);

          ctx.moveTo(x1, y1);
          ctx.lineTo(x2, y2);
          ctx.closePath();
          ctx.strokeStyle = "rgba(255,255,255,1)";
          ctx.stroke();
          ctx.restore();
        }

step3:線(xiàn)條動(dòng)畫(huà)

這里面需要計(jì)算兩點(diǎn)之間的斜率,然后x坐標(biāo)每次挪動(dòng)±1單位,已知斜率和x偏移,即可計(jì)算出y的偏移。值得注意的是,這個(gè)坐標(biāo)系和數(shù)學(xué)中的xy坐標(biāo)系有點(diǎn)不一樣,y軸是反的。然后可以引入額外的參數(shù)speed控制速度

function lineMove(points) {
          if (points.length < 2) {
              
            return;
          }
          const [[x1, y1], [x2, y2]] = points;
          let dx = x2 - x1;
          let dy = y2 - y1;
          if (Math.abs(dx) < 1 && Math.abs(dy) < 1) {
            points = points.slice(1);
            lineMove(points);
            return;
          }
          let x = x1,
            y = y1; //線(xiàn)條繪制過(guò)程中的終點(diǎn)
          if (dx === 0) {
            (x = x2), (y += (speed * dy) / Math.abs(dy));
          } else if (dy === 0) {
            x += (speed * dx) / Math.abs(dx);
            y = y2;
          } else if (Math.abs(dx) >= 1) {
            let rate = dy / dx;
            x += (speed * dx) / Math.abs(dx);
            y += (speed * rate * dx) / Math.abs(dx);
          }
          drawLine(x1, y1, x, y);
          points[0] = [x, y];
          window.requestAnimationFrame(function() {
            lineMove(points);
          });
        }

主要代碼就這么多,先看效果

完整代碼

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

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>canvas-連線(xiàn)動(dòng)畫(huà)</title>
</head>

<body>
  <canvas id="canvas" width="400" height="400"></canvas>
  <script>
    //起點(diǎn):10,20 終點(diǎn):150,200
    const points = [
      [200, 100], //上
      [300, 200], //右
      [100, 200], //左
      [200, 100], //上
      [200, 300], //下
      [100, 200], //左
      [300, 200], //右
      [200, 300]
    ];
    const canvas = document.querySelector("canvas");
    const ctx = canvas.getContext("2d");
    // const img = new Image();
    const speed = 10; //速度
    // img.onload = function() {
    // canvas.width = img.width;
    // canvas.height = img.height;
    animate(ctx);
    // };

    // img.src = "./imgs/demo.png";
    function animate(ctx) {
      // ctx.drawImage(img, 0, 0);
      ctx.fillRect(0, 0, canvas.width, canvas.height);
      points.forEach(([x, y]) => {
        drawDot(x, y);
      });
      lineMove(points);
    }
    function lineMove(points) {
      if (points.length < 2) {
        return;
      }
      const [[x1, y1], [x2, y2]] = points;
      let dx = x2 - x1;
      let dy = y2 - y1;
      if (Math.abs(dx) < 1 && Math.abs(dy) < 1) {
        points = points.slice(1);
        lineMove(points);
        return;
      }
      let x = x1,
        y = y1; //線(xiàn)條繪制過(guò)程中的終點(diǎn)
      if (dx === 0) {
        (x = x2), (y += (speed * dy) / Math.abs(dy));
      } else if (dy === 0) {
        x += (speed * dx) / Math.abs(dx);
        y = y2;
      } else if (Math.abs(dx) >= 1) {
        let rate = dy / dx;
        x += (speed * dx) / Math.abs(dx);
        y += (speed * rate * dx) / Math.abs(dx);
      }
      drawLine(x1, y1, x, y);
      points[0] = [x, y];
      window.requestAnimationFrame(function () {
        lineMove(points);
      });
    }

    function drawLine(x1, y1, x2, y2) {
      ctx.save();
      ctx.beginPath(); //不寫(xiě)每次都會(huì)重繪上次的線(xiàn)
      ctx.lineCap = "round";
      ctx.lineJoin = "round";
      var grd = ctx.createLinearGradient(x1, y1, x2, y2);

      ctx.moveTo(x1, y1);
      ctx.lineTo(x2, y2);
      ctx.closePath();
      ctx.strokeStyle = "rgba(255,255,255,1)";
      ctx.stroke();
      ctx.restore();
    }

    function drawDot(x1, y1, r) {
      ctx.save();
      ctx.beginPath(); //不寫(xiě)會(huì)和線(xiàn)連起來(lái)
      ctx.fillStyle = "red";
      //繪制成矩形
      ctx.arc(x1, y1, r ? r : 2, 0, 2 * Math.PI);
      ctx.fill();
      ctx.restore();
    }
  </script>
</body>

</html>

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

標(biāo)簽:中山 赤峰 萍鄉(xiāng) 聊城 金昌 阿壩 盤(pán)錦 綏化

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《canvas簡(jiǎn)單連線(xiàn)動(dòng)畫(huà)的實(shí)現(xiàn)代碼》,本文關(guān)鍵詞  canvas,簡(jiǎn)單,連線(xiàn),動(dòng),畫(huà)的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《canvas簡(jiǎn)單連線(xiàn)動(dòng)畫(huà)的實(shí)現(xiàn)代碼》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于canvas簡(jiǎn)單連線(xiàn)動(dòng)畫(huà)的實(shí)現(xiàn)代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    苍梧县| 大悟县| 天门市| 泸水县| 东港市| 涞水县| 麦盖提县| 崇文区| 喀喇沁旗| 敖汉旗| 西藏| 犍为县| 融水| 汝南县| 浪卡子县| 石首市| 蒙山县| 墨玉县| 城市| 万州区| 西宁市| 定兴县| 磐石市| 潞西市| 睢宁县| 汉寿县| 新津县| 屏东市| 黎川县| 从化市| 昌乐县| 东安县| 开鲁县| 富锦市| 承德市| 凤山县| 昌图县| 巴青县| 抚远县| 宿松县| 丹寨县|