canvas 雪花效果

canvas 画雪花效果

示例

example.html

如图:

源码

注意源码中引用了一个图片,链接在此:snowflake1.png

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
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<title>canvas 雪花效果</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<style type="text/css">
body {
margin: 0;
padding: 0;
background: #000;
}

canvas {
display: block;
width: 100%;
height: 100%;
}
</style>
</head>

<body>
<canvas id="canvas"></canvas>
<script>
//canvas init
var snowflake = {
number: 200,
particles: [],
angle: 0,
timer: null,
canvas: document.getElementById("canvas"),
ctx: document.getElementById("canvas").getContext("2d"),
loadLength: 0, //当前图片加载了多少个
image: [
'./snowflake1.png',
],
winWidth: window.innerWidth,
winHeight: window.innerHeight,
minSize: 5,
maxSize: 12,
init: function () {
var __this = this;
__this.canvas.width = __this.winWidth;
__this.canvas.height = __this.winHeight;
__this.createParticles();
__this.loadImage();
},
loadImage: function () {
var __this = this;
for (var i = 0; i < __this.image.length; i++) {
(function (i) {
var img = new Image();
img.src = __this.image[i];
img.onload = function () {
__this.image[i] = img;
__this.loadLength++;
if (__this.loadLength == __this.image.length) {
__this.loopDraw();
}
}
})(i)
}
},
loopDraw: function () {
var __this = this;
__this.timer = setInterval(function () {
__this.draw();
}, 33);
},
createParticles: function () {
var __this = this;
var width = __this.winWidth;
var height = __this.winHeight;
for (var i = 0; i < __this.number; i++) {
__this.particles.push({
x: __this.rand(10, __this.winWidth - 10),
y: __this.rand(0, __this.winHeight),
imgIndex: __this.rand(0, __this.image.length - 1),
size: __this.rand(__this.minSize, __this.maxSize),
d: Math.random() * __this.number,
dir: __this.rand(0, 1) == 0 ? -1 : 1
});
}
},
draw: function () {
var __this = this;
var width = __this.winWidth;
var height = __this.winHeight;
__this.ctx.clearRect(0, 0, width, height);
for (var i = 0; i < this.number; i++) {
var p = __this.particles[i];
var img = __this.image[p.imgIndex];
try {
__this.ctx.drawImage(img, p.x, p.y, p.size, p.size);
} catch (e) {
console.log(e, p)
clearInterval(__this.timer);
}
}
__this.update();
},
update: function () {
var __this = this;
var width = __this.winWidth;
var height = __this.winHeight;
__this.angle += 0.01;
for (var i = 0; i < this.number; i++) {
var p = __this.particles[i];
p.y += (Math.cos(__this.angle + p.d) + 1 + p.size) / 10;
p.x += (Math.sin(__this.angle) + 1) / 10 * p.dir;

if (p.x > width + __this.maxSize || p.x < -__this.maxSize || p.y > height) {
if (i % 3 > 0) {
__this.particles[i] = {
x: Math.random() * width,
y: -10,
size: p.size,
imgIndex: __this.rand(0, __this.image.length - 1),
d: p.d,
dir: __this.rand(0, 1) == 0 ? -1 : 1
};
} else {
if (Math.sin(__this.angle) > 0) {
__this.particles[i] = {
x: -__this.maxSize,
y: Math.random() * height,
size: p.size,
imgIndex: __this.rand(0, __this.image.length - 1),
d: p.d,
dir: __this.rand(0, 1) == 0 ? -1 : 1
};
} else {
__this.particles[i] = {
x: width + __this.maxSize,
y: Math.random() * height,
size: p.size,
imgIndex: __this.rand(0, __this.image.length - 1),
d: p.d,
dir: __this.rand(0, 1) == 0 ? -1 : 1
};
}
}
}
}
},
rand: function (min, max) {
return parseInt(Math.random() * (max - min + 1) + min);
},
};

snowflake.init();
</script>
</body>

</html>
本文由 linx(544819896@qq.com) 创作,采用 CC BY 4.0 CN协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。本文链接为: https://blog.jijian.link/2020-03-21/canvas-snowflake/

如果您觉得文章不错,可以点击文章中的广告支持一下!