-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.js
182 lines (152 loc) · 6.04 KB
/
car.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* CAR */
//Move the brush and 'paint' with scroll
let brush = document.getElementById("brush"),
paint = document.getElementById("paint"),
height = document.body.scrollHeight;
window.addEventListener('scroll', function () {
var value = window.scrollY;
if (value <= height) {
//Multiply value by a factor that increases/decreases speed
brush.style.left = value * 0.58 + 50 + 'px';
brush.style.top = value * 1.58 + 50 + 'px';
//paint.style.height = value * 1.18 + 'px';
}
});
//Flip the brush around based on scroll direction
let previousScrollPosition = 0;
const isScrollingDown = () => {
let goingDown = false;
let scrollPosition = window.pageYOffset;
if (scrollPosition > previousScrollPosition) {
goingDown = true;
}
previousScrollPosition = scrollPosition;
return goingDown;
};
const handleScroll = () => {
if (isScrollingDown() || window.scrollY == 0) {
//brush.style.transform = 'rotate(180deg) translate(50%, 0)';
} else {
//brush.style.transform = 'translate(-50%, -110px)';
}
};
window.addEventListener("scroll", handleScroll);
/* LIGHTENING */
const width = 1300;
const h = 600;
const maxTimeBetweenLightning = 5;
const maxLightningPaths = 200;
const maxLightningThickness = 5;
const startingDistance = 30;
const maxBranches = 7;
function makeLightning(ctx, startingX, startingY, branches) {
ctx.beginPath();
const amntOfPaths = getRandomInt(maxLightningPaths);
let lightningThickness = maxLightningThickness;
let distance = startingDistance;
let timeout = 80;
let speed = timeout;
let totalTime = 0;
for (let i = 0; i < amntOfPaths; i++) {
ctx.strokeStyle = `rgb(255,255,255)`;
ctx.lineWidth = getRandomInt(lightningThickness);
lightningThickness /= 1.2;
setTimeout(() => {
ctx.moveTo(startingX, startingY);
let endingX = getRandomInt(distance) * negOrPos() + startingX;
let endingY = startingY + getRandomInt(distance * 2);
distance /= 1.1;
ctx.lineTo(endingX, endingY);
startingX = endingX;
startingY = endingY;
ctx.stroke();
if (branches < maxBranches && getRandomInt(maxLightningPaths / 6) == 1) {
let time = makeLightning(ctx, startingX, startingY, branches + 1);
totalTime += time;
}
}, timeout);
speed /= 1.4;
timeout += speed;
}
return timeout + totalTime;
}
function negOrPos() {
return Math.round(Math.random()) == 0 ? -1 : 1;
}
function getRandomInt(max) {
return Math.ceil(Math.random() * max);
}
let prevHighestId = 0;
function createCanvasAndLightning() {
const canvas = document.createElement('canvas');
const body = document.getElementById("weatherAnimation");
canvas.setAttribute('width', '1450px');
canvas.setAttribute('height', '800px');
canvas.className = 'myCanvas';
ctx = canvas.getContext("2d");
body.appendChild(canvas);
const time = makeLightning(ctx, getRandomInt(width), getRandomInt(h / 3), 0);
canvas.style.animationName = 'flash';
canvas.style.animationDuration = time + "ms";
setTimeout(() => {
canvas.style.animationName = 'fadeOut';
}, time);
setTimeout(() => {
canvas.remove();
const highestId = window.setTimeout(() => {
for (let i = highestId; i >= prevHighestId; i--) {
window.clearTimeout(i);
}
prevHighestId = highestId;
setTimeout(createCanvasAndLightning, 1000);
}, 0);
}, time);
}
createCanvasAndLightning();
// number of drops created.
var nbDrop = 858;
// function to generate a random number range.
function randRange(minNum, maxNum) {
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
var makeItRain = function () {
//clear out everything
$('.rain').empty();
var increment = 0;
var drops = "";
var backDrops = "";
while (increment < 100) {
//couple random numbers to use for various randomizations
//random number between 98 and 1
var randoHundo = (Math.floor(Math.random() * (98 - 1 + 1) + 1));
//random number between 5 and 2
var randoFiver = (Math.floor(Math.random() * (5 - 2 + 1) + 2));
//increment
increment += randoFiver;
//add in a new raindrop with various randomizations to certain CSS properties
drops += '<div class="drop" style="left: ' + increment + '%; bottom: ' + (randoFiver + randoFiver - 1 + 100) + '%; animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"><div class="stem" style="animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"></div><div class="splat" style="animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"></div></div>';
backDrops += '<div class="drop" style="right: ' + increment + '%; bottom: ' + (randoFiver + randoFiver - 1 + 100) + '%; animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"><div class="stem" style="animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"></div><div class="splat" style="animation-delay: 0.' + randoHundo + 's; animation-duration: 0.5' + randoHundo + 's;"></div></div>';
}
$('.rain.front-row').append(drops);
$('.rain.back-row').append(backDrops);
}
$('.splat-toggle.toggle').on('click', function () {
$('body').toggleClass('splat-toggle');
$('.splat-toggle.toggle').toggleClass('active');
makeItRain();
});
$('.back-row-toggle.toggle').on('click', function () {
$('body').toggleClass('back-row-toggle');
$('.back-row-toggle.toggle').toggleClass('active');
makeItRain();
});
$('.single-toggle.toggle').on('click', function () {
$('body').toggleClass('single-toggle');
$('.single-toggle.toggle').toggleClass('active');
makeItRain();
});
makeItRain();
document.body.addEventListener("mousemove", function () {
var audio = new Audio('audio/thunder.mp3');
audio.play();
})