-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetTrippy.html
173 lines (149 loc) · 6.2 KB
/
GetTrippy.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get Trippy with Mandalas (Triggered and Timed)</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
canvas {
border: 1px solid #000000;
}
</style>
</head>
<body>
<label for="numberOfTrips">Number of Trips:</label>
<select id="numberOfTrips" name="numberOfTrips">
<option value="5" selected>5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
</select>
<button id="playButton">Get Trippy</button>
<canvas id="mandalaCanvas" width="600" height="600"></canvas>
<script>
// Add Event Listener to Buttom Clicks
document.getElementById('playButton').addEventListener('click', getTrippy);
// Add an event listener for the 'change' event
document.getElementById('numberOfTrips').addEventListener('change', function() {
// Grab the selected value
numTrips = this.value;
randomArrayPetalCount = createRandomArray(numTrips, minPetalCount, maxPetalCount);
});
// Setup
const canvas = document.getElementById('mandalaCanvas');
const ctx = canvas.getContext('2d');
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const radius = 550;
const layers = 500;
const minPetalCount = 10;
const maxPetalCount = 100;
const petalCount = 240;
var numTrips = 5;
var randomArrayPetalCount = createRandomArray(numTrips, minPetalCount, maxPetalCount);
// On Start, Load Starter Mandala
window.onload = function() {
drawMandala(ctx, centerX, centerY, radius, layers, petalCount);
};
// Get Trippy!
function getTrippy () {
// Play Trippy Music
playTrippyMusic();
// Repeat Mandala Drawing
repeatFunctionWithInterval(() => {
let count = 0;
let randomNumberPetalCount = randomArrayPetalCount[count];
drawMandala(ctx, centerX, centerY, radius, layers, randomNumberPetalCount);
count++;
}, numTrips, 1000); // Calls the function X times with a 1000ms (1 second) interval
}
function repeatFunctionWithInterval(fn, times, interval) {
let count = 0;
const intervalId = setInterval(() => {
if (count < times) {
fn();
count++;
} else {
clearInterval(intervalId);
}
}, interval);
}
function createRandomArray(length, min, max) {
const randomArray = [];
for (let i = 0; i < length; i++) {
randomArray.push(getRandomNumber(min, max));
}
return randomArray;
}
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function playTrippyMusic() {
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// Create oscillators
const oscillators = [];
for (let i = 0; i < 5; i++) {
const oscillator = audioCtx.createOscillator();
oscillator.type = 'sine'; // sine, square, sawtooth, triangle, custom
oscillator.frequency.setValueAtTime(Math.random() * 1000, audioCtx.currentTime);
oscillator.start();
oscillators.push(oscillator);
}
// Create gain nodes
const gainNodes = [];
for (let i = 0; i < 5; i++) {
const gainNode = audioCtx.createGain();
gainNode.gain.setValueAtTime(0.1, audioCtx.currentTime);
gainNodes.push(gainNode);
}
// Connect oscillators to gain nodes and gain nodes to destination
oscillators.forEach((oscillator, index) => {
oscillator.connect(gainNodes[index]);
gainNodes[index].connect(audioCtx.destination);
// Set random frequency modulation
setInterval(() => {
oscillator.frequency.setValueAtTime(Math.random() * 1000, audioCtx.currentTime);
}, 1000);
});
// Stop oscillators after X seconds
setTimeout(() => {
oscillators.forEach(oscillator => oscillator.stop());
console.log('Trippy music stopped.');
}, numTrips * 1000);
}
function drawMandala(ctx, centerX, centerY, radius, layers, petalCount) {
for (let layer = 0; layer < layers; layer++) {
const currentRadius = (radius / layers) * (layer + 1);
for (let i = 0; i < petalCount; i++) {
const angle = (i / petalCount) * 2 * Math.PI;
const petalLength = currentRadius / 2;
const startX = centerX + currentRadius * Math.cos(angle);
const startY = centerY + currentRadius * Math.sin(angle);
const endX = centerX + (currentRadius + petalLength) * Math.cos(angle);
const endY = centerY + (currentRadius + petalLength) * Math.sin(angle);
drawPetal(ctx, startX, startY, endX, endY, petalLength / 2);
}
}
}
function drawPetal(ctx, startX, startY, endX, endY, controlPointOffset) {
ctx.beginPath();
ctx.moveTo(startX, startY);
const cp1X = startX + controlPointOffset * Math.cos(Math.atan2(endY - startY, endX - startX) - Math.PI / 2);
const cp1Y = startY + controlPointOffset * Math.sin(Math.atan2(endY - startY, endX - startX) - Math.PI / 2);
const cp2X = endX + controlPointOffset * Math.cos(Math.atan2(endY - startY, endX - startX) + Math.PI / 2);
const cp2Y = endY + controlPointOffset * Math.sin(Math.atan2(endY - startY, endX - startX) + Math.PI / 2);
ctx.bezierCurveTo(cp1X, cp1Y, cp2X, cp2Y, endX, endY);
ctx.closePath();
ctx.fillStyle = `hsl(${(Math.random() * 360).toFixed()}, 100%, 50%)`;
ctx.fill();
}
</script>
</body>
</html>