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
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BP Sand</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
display: flex;
flex-direction: column;
height: 100%;
}
canvas {
display: block;
width: 100%;
background-color: beige;
}
.controls {
display: flex;
justify-content: space-around;
padding: 10px;
background-color: white;
box-shadow: 0 -2px 5px rgba(0,0,0,0.1);
}
input, button {
font-size: 1.5rem;
}
</style>
</head>
<body>
<canvas id="sandCanvas"></canvas>
<div class="controls">
<input type="number" id="bpmInput" value="60" min="10" max="300">
<div>
<button id="clearSand">Clear</button>
<button id="toggleButton">Start</button>
</div>
</div>
<script>
const canvas = document.getElementById('sandCanvas');
const ctx = canvas.getContext('2d');
const bpmInput = document.getElementById('bpmInput');
const toggleButton = document.getElementById('toggleButton');
const clearSandButton = document.getElementById('clearSand');
const controls = document.querySelector('.controls');
let bpm = 60;
let isRunning = false;
let sandParticles = [];
let intervalId;
// Set canvas size to avoid overlapping with controls
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight - controls.offsetHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
function playTone() {
const oscillator = audioCtx.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.setValueAtTime(440, audioCtx.currentTime); // 440 Hz tone
oscillator.connect(audioCtx.destination);
oscillator.start();
oscillator.stop(audioCtx.currentTime + 0.1); // Short beep
}
function createSandParticle() {
return {
x: Math.random() * canvas.width,
y: 0,
size: 5 + Math.random() * 5, // Vary size a bit
velocityY: 0,
atRest: false // Track if the particle is at rest
};
}
// Update sand particle positions
function updateSand() {
for (let particle of sandParticles) {
if (!particle.atRest) {
particle.velocityY += 0.1; // Accelerate! Gravity!
particle.y += particle.velocityY;
// Check if sand particle has hit the bottom
if (particle.y + particle.size >= canvas.height) {
particle.y = canvas.height - particle.size;
particle.atRest = true; // Mark the particle as at rest
}
// Check if sand particle has landed on another particle
for (let otherParticle of sandParticles) {
if (otherParticle !== particle && otherParticle.atRest) {
let distY = otherParticle.y - (particle.y + particle.size);
let distX = Math.abs(otherParticle.x - particle.x);
if (distY <= 0 && distX < particle.size) {
particle.y = otherParticle.y - particle.size;
particle.atRest = true;
break;
}
}
}
}
}
}
// Draw sand particles on the canvas
function drawSand() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'teal';
for (let particle of sandParticles) {
ctx.beginPath();
ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
ctx.fill();
}
}
// Main loop
function gameLoop() {
updateSand();
drawSand();
}
// Control the BPM and start/stop the loop
function toggleBPM() {
isRunning = !isRunning;
if (isRunning) {
let interval = (60 / bpm) * 1000; // Convert BPM to interval in milliseconds
intervalId = setInterval(() => {
sandParticles.push(createSandParticle()); // Generate new sand
playTone(); // Play the tone
}, interval);
toggleButton.textContent = 'Stop';
} else {
clearInterval(intervalId);
toggleButton.textContent = 'Start';
}
}
// Event Listeners
toggleButton.addEventListener('click', toggleBPM);
clearSandButton.addEventListener('click', () => {
sandParticles = [];
});
bpmInput.addEventListener('input', (e) => {
bpm = parseInt(e.target.value);
if (isRunning) {
clearInterval(intervalId);
toggleBPM();
}
});
function animate() {
gameLoop();
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
|