-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava.js
More file actions
152 lines (126 loc) · 4.54 KB
/
Copy pathjava.js
File metadata and controls
152 lines (126 loc) · 4.54 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
// Enhanced Drum Kit JavaScript with modern features
// Initialize the drum kit
document.addEventListener('DOMContentLoaded', function() {
initializeDrumKit();
});
function initializeDrumKit() {
const drums = document.querySelectorAll('.drum');
// Add click event listeners
drums.forEach(drum => {
drum.addEventListener('click', function() {
const key = this.getAttribute('data-key');
playDrum(key);
animateButton(this);
});
});
// Add keyboard event listeners
document.addEventListener('keydown', function(event) {
const key = event.key.toLowerCase();
const drumElement = document.querySelector(`[data-key="${key}"]`);
if (drumElement) {
playDrum(key);
animateButton(drumElement);
}
});
// Add touch support for mobile
drums.forEach(drum => {
drum.addEventListener('touchstart', function(e) {
e.preventDefault();
const key = this.getAttribute('data-key');
playDrum(key);
animateButton(this);
});
});
}
function playDrum(key) {
const audioMap = {
'w': 'sounds/tom-1.mp3',
'a': 'sounds/tom-4.mp3',
'd': 'sounds/snare.mp3',
'k': 'sounds/tom-2.mp3',
'n': 'sounds/tom-3.mp3',
'm': 'sounds/crash.mp3'
};
const audioFile = audioMap[key];
if (audioFile) {
const audio = new Audio(audioFile);
audio.volume = 0.7; // Set a comfortable volume
audio.play().catch(error => {
console.log('Audio play failed:', error);
});
}
}
function animateButton(element) {
// Remove any existing animation class
element.classList.remove('pressed');
// Force reflow to ensure the class removal is processed
element.offsetHeight;
// Add the animation class
element.classList.add('pressed');
// Remove the class after animation completes
setTimeout(() => {
element.classList.remove('pressed');
}, 200);
}
// Add visual feedback for active keys
document.addEventListener('keydown', function(event) {
const key = event.key.toLowerCase();
const drumElement = document.querySelector(`[data-key="${key}"]`);
if (drumElement) {
drumElement.style.transform = 'translateY(-5px) scale(0.95)';
}
});
document.addEventListener('keyup', function(event) {
const key = event.key.toLowerCase();
const drumElement = document.querySelector(`[data-key="${key}"]`);
if (drumElement) {
drumElement.style.transform = '';
}
});
// Add some fun particle effects (optional enhancement)
function createParticleEffect(element) {
const rect = element.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
for (let i = 0; i < 6; i++) {
const particle = document.createElement('div');
particle.style.position = 'fixed';
particle.style.left = centerX + 'px';
particle.style.top = centerY + 'px';
particle.style.width = '4px';
particle.style.height = '4px';
particle.style.background = '#f39c12';
particle.style.borderRadius = '50%';
particle.style.pointerEvents = 'none';
particle.style.zIndex = '1000';
document.body.appendChild(particle);
const angle = (i * 60) * (Math.PI / 180);
const velocity = 50;
const vx = Math.cos(angle) * velocity;
const vy = Math.sin(angle) * velocity;
particle.animate([
{ transform: 'translate(0, 0) scale(1)', opacity: 1 },
{ transform: `translate(${vx}px, ${vy}px) scale(0)`, opacity: 0 }
], {
duration: 500,
easing: 'ease-out'
}).onfinish = () => {
document.body.removeChild(particle);
};
}
}
// Enhanced button animation with particle effect
function animateButton(element) {
// Remove any existing animation class
element.classList.remove('pressed');
// Force reflow to ensure the class removal is processed
element.offsetHeight;
// Add the animation class
element.classList.add('pressed');
// Create particle effect
createParticleEffect(element);
// Remove the class after animation completes
setTimeout(() => {
element.classList.remove('pressed');
}, 200);
}