一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務(wù)器之家 - 編程語(yǔ)言 - JavaScript - js教程 - js canvas實(shí)現(xiàn)圓形流水動(dòng)畫

js canvas實(shí)現(xiàn)圓形流水動(dòng)畫

2022-03-06 21:30莫兮是我 js教程

這篇文章主要為大家詳細(xì)介紹了js canvas實(shí)現(xiàn)圓形流水動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

前言

特效展示

效果展示

js canvas實(shí)現(xiàn)圓形流水動(dòng)畫

代碼展示

index.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<!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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * 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();
  
})();

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

原文鏈接:https://blog.csdn.net/u013362192/article/details/115247626

延伸 · 閱讀

精彩推薦
  • js教程Strve.js開發(fā)一個(gè)屬于自己的庫(kù)或框架

    Strve.js開發(fā)一個(gè)屬于自己的庫(kù)或框架

    Strve.js是一個(gè)可以將字符串轉(zhuǎn)換為視圖的JS庫(kù)。這里的字符串指的是模板字符串,所以你僅需要在JavaScript中開發(fā)視圖。Strve.js不僅易于上手,還便于靈活拆裝...

    前端歷劫之路6072021-12-23
  • js教程JS實(shí)現(xiàn)點(diǎn)擊掉落特效

    JS實(shí)現(xiàn)點(diǎn)擊掉落特效

    這篇文章主要介紹了JS實(shí)現(xiàn)點(diǎn)擊掉落特效,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下...

    小楊的旺仔沒有牛奶11812022-01-11
  • js教程微信小程序授權(quán)登錄的優(yōu)雅處理方式

    微信小程序授權(quán)登錄的優(yōu)雅處理方式

    這篇文章主要給大家介紹了關(guān)于微信小程序授權(quán)登錄的優(yōu)雅處理方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值...

    FTD止水11622022-02-15
  • js教程nestjs返回給前端數(shù)據(jù)格式的封裝實(shí)現(xiàn)

    nestjs返回給前端數(shù)據(jù)格式的封裝實(shí)現(xiàn)

    這篇文章主要介紹了nestjs返回給前端數(shù)據(jù)格式的封裝實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋...

    水痕018882022-01-22
  • js教程詳解CocosCreator優(yōu)化之DrawCall

    詳解CocosCreator優(yōu)化之DrawCall

    這篇文章主要介紹了CocosCreator中DrawCall的優(yōu)化,想研究游戲性能的同學(xué),一定要看一看...

    路飛的小迷弟7062022-03-02
  • js教程JavaScript Dom實(shí)現(xiàn)輪播圖原理和實(shí)例

    JavaScript Dom實(shí)現(xiàn)輪播圖原理和實(shí)例

    這篇文章主要為大家詳細(xì)介紹了JavaScript Dom實(shí)現(xiàn)輪播圖原理和實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下...

    ALonelyLemon3762022-01-21
  • js教程javascript中l(wèi)ayim之查找好友查找群組

    javascript中l(wèi)ayim之查找好友查找群組

    這篇文章主要介紹了javascript中l(wèi)ayim之查找好友查找群組,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下...

    踮腳敲代碼9182022-01-19
  • js教程JavaScript中展開運(yùn)算符及應(yīng)用的實(shí)例代碼

    JavaScript中展開運(yùn)算符及應(yīng)用的實(shí)例代碼

    這篇文章主要介紹了JavaScript中展開運(yùn)算符及應(yīng)用的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以...

    banana peel9672021-12-31
主站蜘蛛池模板: 亚洲人成在线观看一区二区 | 精品一区二区91 | 亚州在线 | 四虎www| 99国产自偷色久 | 国产色视频网站 | 日韩一级欧美一级一级国产 | 99视频在线观看视频一区 | 精品女同一区二区三区免费站 | 2021国产精品成人免费视频 | 久久噜国产精品拍拍拍拍 | 欧美成人香蕉在线观看 | 成人在线播放视频 | 欧美视频一区二区三区在线观看 | 国产精品亚洲午夜不卡 | 国内精品露脸在线视频播放 | 大ji巴好好爽好深网站 | 色啪啪888.com | 日本中文字幕一区二区高清在线 | 久草大 | 国产99视频精品免视看9 | 亚洲免费福利视频 | 热久久最新 | 日本人妖网站 | 日韩欧美国内 | 欧美日韩一品道 | 91国产在线第7页 | 91制片厂制作传媒免费版樱花 | 国产一区二区在线看 | 床戏小说| 午夜国产小视频 | 操碰人人 | 91视频破解版 | 四虎2020紧急免费入口 | 国产动作大片 | free性丰满hd性欧美人体 | 九九热免费在线观看 | 亚洲国产精品日本无码网站 | 成人午夜视频一区二区国语 | 黑人与欧洲女子性大战 | 久久热在线视频精品店 |