-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
95 lines (77 loc) · 3.68 KB
/
script.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
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const playNote = (frequency, duration = 0.3, startTime = 0) => {
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.type = 'sine';
oscillator.frequency.setValueAtTime(frequency, startTime);
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
gainNode.gain.setValueAtTime(1, startTime);
gainNode.gain.exponentialRampToValueAtTime(0.001, startTime + duration);
oscillator.start(startTime);
oscillator.stop(startTime + duration);
};
const noteFrequencies = {
'C4': 261.63,
'D4': 293.66,
'E4': 329.63,
'F4': 349.23,
'G4': 392.00,
'A4': 440.00,
'B4': 493.88,
'C5': 523.25
};
const jingleBells = [
{ note: 'E4', duration: 0.3 }, { note: 'E4', duration: 0.3 }, { note: 'E4', duration: 0.6 },
{ note: 'E4', duration: 0.3 }, { note: 'E4', duration: 0.3 }, { note: 'E4', duration: 0.6 },
{ note: 'E4', duration: 0.3 }, { note: 'G4', duration: 0.3 }, { note: 'C4', duration: 0.3 },
{ note: 'D4', duration: 0.3 }, { note: 'E4', duration: 0.6 },
{ note: 'F4', duration: 0.3 }, { note: 'F4', duration: 0.3 }, { note: 'F4', duration: 0.45 },
{ note: 'F4', duration: 0.15 }, { note: 'F4', duration: 0.3 }, { note: 'E4', duration: 0.3 },
{ note: 'E4', duration: 0.3 }, { note: 'E4', duration: 0.45 }, { note: 'E4', duration: 0.15 },
{ note: 'E4', duration: 0.3 }, { note: 'D4', duration: 0.3 }, { note: 'D4', duration: 0.3 },
{ note: 'E4', duration: 0.3 }, { note: 'D4', duration: 0.3 }, { note: 'G4', duration: 0.6 },
{ note: 'E4', duration: 0.3 }, { note: 'E4', duration: 0.3 }, { note: 'E4', duration: 0.6 },
{ note: 'E4', duration: 0.3 }, { note: 'E4', duration: 0.3 }, { note: 'E4', duration: 0.6 },
{ note: 'E4', duration: 0.3 }, { note: 'G4', duration: 0.3 }, { note: 'C4', duration: 0.3 },
{ note: 'D4', duration: 0.3 }, { note: 'E4', duration: 0.6 },
{ note: 'F4', duration: 0.3 }, { note: 'F4', duration: 0.3 }, { note: 'F4', duration: 0.45 },
{ note: 'F4', duration: 0.15 }, { note: 'F4', duration: 0.3 }, { note: 'E4', duration: 0.3 },
{ note: 'E4', duration: 0.3 }, { note: 'E4', duration: 0.15 }, { note: 'E4', duration: 0.15 },
{ note: 'G4', duration: 0.3 }, { note: 'G4', duration: 0.3 }, { note: 'F4', duration: 0.3 },
{ note: 'D4', duration: 0.3 }, { note: 'C4', duration: 0.6 }
];
const playJingleBells = () => {
const playButton = document.querySelector('#play-song');
playButton.disabled = true;
let startTime = audioContext.currentTime;
jingleBells.forEach(({ note, duration }) => {
playNote(noteFrequencies[note], duration, startTime);
startTime += duration + 0.1;
});
setTimeout(() => {
playButton.disabled = false;
}, (startTime - audioContext.currentTime) * 1000);
};
document.querySelector('#play-song').addEventListener('click', () => playJingleBells());
function createSnowflake() {
const snowflake = document.createElement('div');
snowflake.classList.add('snowflake');
snowflake.style.left = Math.random() * window.innerWidth + 'px';
snowflake.style.fontSize = (Math.random() * 1 + 0.5) + 'em';
snowflake.textContent = '❄';
document.body.appendChild(snowflake);
const fallDuration = Math.random() * 3 + 2;
snowflake.animate([
{ transform: 'translateY(0)' },
{ transform: `translateY(${window.innerHeight}px)` }
], {
duration: fallDuration * 1000,
easing: 'linear',
fill: 'forwards'
});
setTimeout(() => {
snowflake.remove();
}, fallDuration * 1000);
}
setInterval(createSnowflake, 300);