-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
215 lines (210 loc) · 9.97 KB
/
index.html
File metadata and controls
215 lines (210 loc) · 9.97 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Canvas Snowflakes</title>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<meta name="description" content="Simple Snwflakes in a Canvas">
<link rel="icon" href="favicon.ico">
<link rel='shortcut icon' type='image/x-icon' href='favicon.ico' />
<link rel="shortcut icon" type="image/png" href="favicon.png"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
html, body {
margin: 0;
width: 100%;
height: 100%;
font-family: Verdana;
background-color: skyblue;
position: relative;
overflow: hidden;
background-repeat: no-repeat;
background-size: 100% 100%;
}
.snowAnimationCanvas {
position: absolute;
top: 0px;
left: 0px;
z-index: 1;
}
.scene_container {
width: 100%;
height: 100%;
position: absolute;
top: 0px;
left: 0px;
background-repeat: no-repeat;
background-size: 100% 100%;
}
@media screen and (orientation:portrait) {
.scene_container {
background-image: url("assets/cbc_p.jpg");
}
}
@media screen and (orientation:landscape) {
.scene_container {
background-image: url("assets/cbc_l.jpg");
}
}
</style>
</head>
<body>
<div id="animation_container" class="scene_container">
<canvas class="snowAnimationCanvas"></canvas>
</div>
<script>
// self calling closure
(function () {
class Flake {
constructor(containerWidth, containerHeight, id, maxWeight, maxSpeed) {
// Left to right random position
this.xPos = this.randomIntBetween(0, containerWidth);
// Top to bottom random position above visible region
this.yPos = this.randomIntBetween(0, containerHeight) * -1;
// Random horizontal travel
this.curveWidth = this.randomFloatBetween(0, 1);
// Random horizontal travel speed
this.frequency = this.randomFloatBetween(0, Math.PI);
// Weight determines Size, Speed and opacity
this.weight = this.randomIntBetween(1, maxWeight);
// Random opacity designed to simulate distance
this.opacity = (this.weight / maxWeight);
// random speed for downward movement
this.speed = (this.weight / maxWeight) * maxSpeed;
}
// called during update cycle
move() {
this.xPos += Math.cos(this.frequency) * this.curveWidth;
this.yPos += this.speed;
}
// flake fell out of view, reset back to top, we don't need to
// random height is because it's already random, random xPos
// just to mix it up
reset(xPosLimit) {
this.yPos = -1;
this.xPos = this.randomIntBetween(0, xPosLimit);
}
// standard random range method
randomIntBetween(min, max) {
let num = Math.random() * (max - min + 1) + min;
return Math.floor(num);
}
// and again
randomFloatBetween(min, max) {
return Math.random() * (max - min) + min;
}
}
class Snow {
constructor(containerSelector, numberOfSnowflakes, maxWeight, maxSpeed) {
// for callback use
this.scope = this;
// store containing div info
this.container = document.getElementById(containerSelector);
this.containerWidth = this.container.clientWidth;
this.containerHeight = this.container.clientHeight;
// set flake weight (size) for screen size
this.getFlakeWeight = () => 0.0020 * (this.containerWidth + this.containerHeight);
// snowflake params
this.numberOfSnowflakes = numberOfSnowflakes;
this.maxWeight = this.getFlakeWeight();
this.maxSpeed = maxSpeed;
// snowflake array
this.flakes = [];
// store canvas info
this.useCanvas = this.isCanvasSupported();
this.canvas = document.querySelector('.snowAnimationCanvas');
this.ctx = this.canvas.getContext('2d');
}
generateSnowFlakes() {
// reset array
this.flakes = [];
// Generate flakes
for (let i = 0; i < this.numberOfSnowflakes; i++) {
this.flakes.push(new Flake(this.containerWidth, this.containerHeight, 'flake' + i, this.maxWeight, this.maxSpeed));
}
}
// starting point for all things snow
init() {
// If we don't have canvas (unlikely)
if (!this.useCanvas) {
return false;
}
// Set size as soon as we get it
this.scaleCanvas();
// Let window resize nows what to do
// scope seems more clear than using bind
let scope = this;
window.addEventListener('resize', function () {
scope.scaleCanvas();
});
// Trigger loop for animation
this.canvasAnimationLoop(this);
}
// resize the canvas as needed
scaleCanvas() {
this.containerWidth = this.canvas.width = this.container.clientWidth;
this.containerHeight = this.canvas.height = this.container.clientHeight;
// modify snowflake size based on device width + height
this.maxWeight = this.getFlakeWeight();
// rebuild snowflakes on resize
this.generateSnowFlakes();
}
// quick check that we have canvas available
isCanvasSupported() {
let elem = document.createElement('canvas');
return !!(elem.getContext && elem.getContext('2d') && window.requestAnimationFrame);
}
// main animation loop
// scope is used because 'this' isn't available in requestAnimationFrame
canvasAnimationLoop(scope) {
// save the canvas context
this.ctx.save();
// clear it
this.ctx.clearRect(0, 0, this.containerWidth, this.containerHeight);
// restore the canvas context
this.ctx.restore();
// loop through the array, move and redraw each snowflake
for (let i = 0; i < this.flakes.length; i++) {
// get a convenient handle
let snowflake = this.flakes[i];
// call it's move method
snowflake.move();
// draw it on the canvas
this.ctx.beginPath();
this.ctx.arc(snowflake.xPos, snowflake.yPos, snowflake.weight, 0, 2 * Math.PI, false);
// using rgba for the opacity
this.ctx.fillStyle = 'rgba(255, 255, 255, ' + snowflake.opacity + ')';
this.ctx.fill();
// if it's reached the bottom, put it back to just
// above the top of the screen
if (snowflake.yPos >= this.containerHeight) {
snowflake.reset(this.containerWidth);
}
}
// loop entry
requestAnimationFrame(function () {
scope.canvasAnimationLoop(scope);
});
}
}
/*
* Canvas container ID
* @param {String} backgroundSelector
*
* How many snowflakes
* @param {Number} numberOfSnowflakes
*
* Weight determines Size, Speed and Opacity to simulate distance
* and is randomized
* @param {Number} maxWeight
*
* Effects speed for downward movement is randomized
* @param {Number} maxSpeed
*/
let snow = new Snow('animation_container', 900, 6, 2.5);
snow.init();
})();
</script>
</body>
</html>