欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js canvas實現(xiàn)圓形流水動畫

 更新時間:2021年04月19日 11:19:59   作者:莫兮是我  
這篇文章主要為大家詳細(xì)介紹了js canvas實現(xiàn)圓形流水動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了canvas實現(xiàn)圓形流水動畫的具體代碼,供大家參考,具體內(nèi)容如下

前言

特效展示

效果展示

代碼展示

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <link rel="stylesheet" href="style.css" > -->
</head>
<body>
    <script src="main.js"></script>
</body>
</html>

main.js

/*
 * Noel Delgado - @pixelia_me
 */

(function() {
  var ctx, w, h, cx, cy, PI, PI_HALF, cos, sin, random, lineWidth, C, 
      rings, ringsLength, data;

  ctx = document.createElement('canvas').getContext('2d');
  w = 600;
  h = 600;
  cx = (w / 2);
  cy = (h / 2);
  rings = [];
  ringsLength = 0;
  
  PI = Math.PI;
  PI_HALF = PI / 2;
  cos = Math.cos;
  sin = Math.sin;
  random = Math.random;

  lineWidth = 0.2;
  C = ["#ABF8FF", "#E76B76", "#1D2439", "#4F3762", "#67F9FF", "#0C0F18"];
  
  data = [
    /* ring {t:total_particles, r:radius, d:distance, s:speed, c:color} */
    [
      {t:80, r:(cx-10), d:40, s:30, c:C[1]},
      {t:60, r:(cx-20), d:40, s:80, c:C[2]},
      {t:20, r:(cx-30), d:20, s:80, c:C[2]},
    ],
    [
     {t:80, r:(cx-80),  d:40, s:40, c:C[4]},
       {t:80, r:(cx-90),  d:20, s:40, c:C[4]},
       {t:20, r:(cx-100), d:20, s:40, c:C[2]},
       {t:40, r:(cx-110), d:20, s:40, c:C[2]},
    ],
    [
     {t:60, r:(cx-160), d:40, s:20, c:C[2]},
       {t:20, r:(cx-170), d:30, s:60, c:C[2]},
       {t:40, r:(cx-180), d:40, s:60, c:C[2]},
    ],
    [
     {t:40, r:(cx-230), d:40, s:20, c:C[5]},
       {t:20, r:(cx-240), d:20, s:10, c:C[5]},
    ],
    [
       {t:10, r:(cx-290), d:10, s:10, c:C[4]}
    ]
  ];
 
  /* */
  ctx.canvas.width = w;
  ctx.canvas.height = h;
  document.body.appendChild(ctx.canvas);

  data.forEach(function(group) {
    var ring = [];
    
    group.forEach(function(orbit, i) {
      var total_particles, index;
      
      total_particles = orbit.t;
      index = 0;
      
      for (; index < total_particles; index++) {
        var radius, distance, speed, color, opacity;

        radius = orbit.r;
        distance = orbit.d;
        speed = random() / orbit.s;
        speed = i % 2 ? speed : speed * -1;
        color = orbit.c;
        opacity = orbit.o;

        ring.push(new P(radius, distance, speed, color, opacity));

        radius = distance = speed = color = opacity = null;
      }
    });
    
    rings.push(ring);
  });

  ringsLength = rings.length;
 
  /* */
  function P(radius, distance, speed, color) {
    this.a = PI / 180;
    this.d = distance;
    this.d2 = (this.d * this.d);
    this.x = cx + radius * cos(this.a);
    this.y = cy + radius * sin(this.a);
    this.c = color;
    this.r = (random() * 8);
    this.R = random() > 0.5 ? radius : radius - 5;
    this.s = speed;
    this.pos = random() * 360;
  }
  
  function draw() {
    var i, j, k, xd, yd, d, ring, ringLength, ringLength2, particle, p2;

    ctx.beginPath();
    ctx.globalCompositeOperation = "source-over";
    ctx.rect(0, 0 , w, h);
    ctx.fillStyle = "#151a28";
    ctx.fill();
    ctx.closePath();

    for (i = 0; i < ringsLength; i++) {
      ring = rings[i];
      ringLength = ring.length;
      ringLength2 = ringLength - 100;
      
      for (j = 0; j < ringLength; j++) {
        particle = ring[j];

        particle.x = cx + particle.R * sin(PI_HALF + particle.pos);
        particle.y = cy + particle.R * cos(PI_HALF + particle.pos);
        particle.pos += particle.s;

        ctx.beginPath();
        ctx.globalAlpha = 0.12;
        ctx.globalCompositeOperation = "lighter";
        ctx.fillStyle = particle.c;
        ctx.arc(particle.x, particle.y, particle.r, PI * 2, false);
        ctx.fill();
        ctx.closePath();

        for (k = 0; k < ringLength2; k++) {
          p2 = ring[k];

          yd = p2.y - particle.y;
          xd = p2.x - particle.x;
          d = ((xd * xd) + (yd * yd));

          if (d < particle.d2) {
            ctx.beginPath();
            ctx.globalAlpha = 1;
            ctx.lineWidth = lineWidth;
            ctx.moveTo(particle.x, particle.y);
            ctx.lineTo(p2.x, p2.y);
            ctx.strokeStyle = p2.c;
            ctx.stroke();
            ctx.closePath();
          }
        }
      }
    }
  }

  function loop() {
    draw();
    requestAnimationFrame(loop);
  }

  loop();
  
})();

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

相關(guān)文章

  • JavaScript常用數(shù)組去重的方法及對比詳解

    JavaScript常用數(shù)組去重的方法及對比詳解

    數(shù)組去重在面試和工作中都是比較容易見到的問題。這篇文章主要是來測試多個方法,對下面這個數(shù)組的去重結(jié)果進(jìn)行分析討論,需要的可以參考一下
    2022-07-07
  • 微信小程序?qū)崿F(xiàn)圖片上傳、刪除和預(yù)覽功能的方法

    微信小程序?qū)崿F(xiàn)圖片上傳、刪除和預(yù)覽功能的方法

    這篇文章主要介紹了微信小程序?qū)崿F(xiàn)圖片上傳、刪除和預(yù)覽功能的方法,涉及微信小程序界面布局、事件響應(yīng)及圖片操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2017-12-12
  • 小程序?qū)崿F(xiàn)Token生成與驗證

    小程序?qū)崿F(xiàn)Token生成與驗證

    本文主要介紹了小程序?qū)崿F(xiàn)Token生成與驗證,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 一行代碼告別document.getElementById

    一行代碼告別document.getElementById

    很久以前做網(wǎng)頁的時候,幾乎沒有聽說過 getElementById 這一玩意兒。在那個ie獨(dú)占天下的年代里,做的頁面也幾乎都是ie only的。只要ie沒問題,那就OK了
    2012-06-06
  • js判斷橫豎屏及禁止瀏覽器滑動條示例

    js判斷橫豎屏及禁止瀏覽器滑動條示例

    這篇文章主要介紹了使用js如何判斷橫豎屏及禁止瀏覽器滑動條,需要的朋友可以參考下
    2014-04-04
  • JavaScript中的this/call/apply/bind的使用及區(qū)別

    JavaScript中的this/call/apply/bind的使用及區(qū)別

    這篇文章主要介紹了JavaScript中的this/call/apply/bind的使用及區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 前端國際化JS庫i18n配置使用方法

    前端國際化JS庫i18n配置使用方法

    在Web開發(fā)中,實現(xiàn)前端的國際化i18n是一項重要的任務(wù),它允許網(wǎng)站內(nèi)容根據(jù)不同地區(qū)的用戶需求進(jìn)行本地化,下面這篇文章主要給大家介紹了關(guān)于前端國際化JS庫i18n配置使用的相關(guān)資料,需要的朋友可以參考下
    2024-06-06
  • 使用Javascript開發(fā)sliding-nav帶滑動條效果的導(dǎo)航插件

    使用Javascript開發(fā)sliding-nav帶滑動條效果的導(dǎo)航插件

    這篇文章主要介紹了使用Javascript開發(fā)sliding-nav帶滑動條效果的導(dǎo)航插件,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 鼠標(biāo)左鍵單擊沖突的問題解決方法(防止冒泡)

    鼠標(biāo)左鍵單擊沖突的問題解決方法(防止冒泡)

    一個頁面實現(xiàn)了兩種右鍵菜單,當(dāng)鼠標(biāo)左鍵單擊空白處時,右鍵菜單并不隱藏,下面為大家解決鼠標(biāo)左鍵單擊沖突的問題
    2014-05-05
  • 一篇文章弄懂javascript內(nèi)存泄漏

    一篇文章弄懂javascript內(nèi)存泄漏

    js的垃圾回收機(jī)制就是為了防止內(nèi)存泄漏的,這篇文章主要給大家介紹了如何通過一篇文章弄懂javascript內(nèi)存泄漏的相關(guān)資料,需要的朋友可以參考下
    2021-05-05

最新評論