服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

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

服务器之家 - 编程语言 - JavaScript - js教程 - js canvas实现圆形流水动画

js canvas实现圆形流水动画

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

这篇文章主要为大家详细介绍了js canvas实现圆形流水动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了canvas实现圆形流水动画的具体代码,供大家参考,具体内容如下

前言

特效展示

效果展示

js canvas实现圆形流水动画

代码展示

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();
  
})();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

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

延伸 · 阅读

精彩推荐
  • js教程js 执行上下文和作用域的相关总结

    js 执行上下文和作用域的相关总结

    这篇文章主要介绍了js 执行上下文和作用域的相关知识总结,帮助大家更好的理解和使用JavaScript,感兴趣的朋友可以了解下...

    前端Serendipity11292022-01-19
  • js教程js实现弹框效果

    js实现弹框效果

    这篇文章主要为大家详细介绍了js实现弹框效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    程序猿余某人3962022-02-20
  • js教程JS一分钟在github+Jekyll的博客中添加访问量功能的实现

    JS一分钟在github+Jekyll的博客中添加访问量功能的实现

    这篇文章主要介绍了JS一分钟在github+Jekyll的博客中添加访问量功能的实现,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借...

    董哥大鸟走四方6172022-02-22
  • js教程玩转 Mockjs,前端也能跑得很溜

    玩转 Mockjs,前端也能跑得很溜

    mockjs作用就是,生成随机模拟数据,拦截 ajax 请求,可以对数据进行增删改查。在生成数据时,我们就需要能够熟练使用 mock.js 的语法。...

    前端人4882022-01-05
  • js教程javascript实现倒计时关闭广告

    javascript实现倒计时关闭广告

    这篇文章主要为大家详细介绍了javascript实现倒计时关闭广告,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    爱前端的茂茂11612022-01-20
  • js教程JavaScript快速实现日历效果

    JavaScript快速实现日历效果

    这篇文章主要为大家详细介绍了JavaScript快速实现日历效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    云杰8了10872022-02-13
  • js教程原生JS实现pc端轮播图效果

    原生JS实现pc端轮播图效果

    这篇文章主要为大家详细介绍了原生JS实现pc端轮播图效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    qq_1519846510212021-12-15
  • js教程JS实现纸牌发牌动画

    JS实现纸牌发牌动画

    这篇文章主要为大家详细介绍了JS实现纸牌发牌动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    计算机的皇帝5432022-01-04