-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalib.js
174 lines (140 loc) · 6.56 KB
/
calib.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
const { set } = require("firebase/database");
import { signInWithGoogle, signOutGoogle, auth, onAuthStateChanged, db, getDoc, setDoc, doc } from './src/firebase';
document.getElementById('google-signin-button').addEventListener('click', () => {
signInWithGoogle();
});
document.getElementById('google-signout-button').addEventListener('click', () => {
signOutGoogle();
});
let signed_in_user = null;
onAuthStateChanged(auth, (user) => {
if (user) {
signed_in_user = user;
document.getElementById('google-signin-button').style.display = 'none';
document.getElementById('google-signout-button').style.display = 'block';
document.getElementById('user-name').innerText = `Welcome, ${user.displayName}`;
} else {
document.getElementById('google-signin-button').style.display = 'block';
document.getElementById('google-signout-button').style.display = 'none';
document.getElementById('user-name').innerText = '';
signed_in_user = null;
}
});
const grid = document.querySelector('.grid');
const minFrequencySlider = document.getElementById('min-frequency-slider');
const minFrequencyValue = document.getElementById('min-frequency-value');
const frequencyDiffSlider = document.getElementById('frequency-diff-slider');
const frequencyDiffValue = document.getElementById('frequency-diff-value');
const circleSizeSlider = document.getElementById('circle-size-slider');
const circleSizeValue = document.getElementById('circle-size-value');
const fpsSelect = document.getElementById('fps-select');
const sliderContainer = document.querySelector('.slider-container');
const toggleSlidersButton = document.getElementById('toggle-sliders-button');
const fullscreenButton = document.getElementById('fullscreen-button');
const circleSpacingSlider = document.getElementById('circle-spacing-slider');
// Function to create the grid of circles
function createGrid() {
grid.innerHTML = '';
const minFrequency = parseFloat(minFrequencySlider.value);
const frequencyDiff = parseFloat(frequencyDiffSlider.value);
const circleSize = parseInt(circleSizeSlider.value);
// <div>
// <label for="circle-spacing-slider">Circle Spacing: <span id="circle-spacing-value">50</span> px</label>
// <input type="range" id="circle-spacing-slider" min="10" max="100" step="1" value="50">
// </div>
const circleSpacing = parseInt(circleSpacingSlider.value);
for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
const cercle = document.createElement('div');
cercle.classList.add('cercle');
const frequency = minFrequency + (i * 4 + j) * frequencyDiff;
console.log("circle #", i * 4 + j, "frequency: ", frequency)
const period = 1 / frequency;
cercle.style.animationDuration = `${period}s`;
cercle.style.width = `${circleSize}px`;
cercle.style.height = `${circleSize}px`;
cercle.style.margin = `${circleSpacing}px`;
grid.appendChild(cercle);
}
}
}
// Update slider values
function updateValues() {
minFrequencyValue.textContent = minFrequencySlider.value;
frequencyDiffValue.textContent = frequencyDiffSlider.value;
circleSizeValue.textContent = circleSizeSlider.value;
const minFrequency = parseFloat(minFrequencySlider.value);
const totalTime = 3 * 1000 / minFrequency;
const totalTimeDisplay = document.getElementById('total-time');
totalTimeDisplay.textContent = (totalTime / 1000).toFixed(2) + ' s';
const fps = parseInt(fpsSelect.value);
const imageCount = Math.ceil(totalTime / 1000 * fps);
const imageCountDisplay = document.getElementById('image-count');
imageCountDisplay.textContent = imageCount;
const frequencyDiff = parseFloat(frequencyDiffSlider.value);
const circleSize = parseInt(circleSizeSlider.value);
let nCircles = 4 * 4;
const lastDotIndex = nCircles - 1;
const lastDotFrequency = minFrequency + lastDotIndex * frequencyDiff;
const blinkCount = Math.floor(totalTime / (1 / lastDotFrequency) / 1000);
const blinkCountDisplay = document.getElementById('blink-count');
const blinkDuration = 1 / lastDotFrequency;
blinkCountDisplay.textContent = blinkDuration.toFixed(2) + ' s';
const frameTime = 1000 / fps;
const frameTimeDisplay = document.getElementById('frame-time-text');
frameTimeDisplay.textContent = frameTime.toFixed(2) + ' ms';
createGrid();
// logCircleCenters(); // Log circle centers after updating the grid
}
// <button id="save-button">Save</button>
document.getElementById('save-button').addEventListener('click', logCircleCenters);
// Function to log the center of each circle along with the frequency
async function logCircleCenters() {
const circles = document.querySelectorAll('.cercle');
const centers = [];
const pixelRatio = window.devicePixelRatio || 1;
const minFrequency = parseFloat(minFrequencySlider.value);
const frequencyDiff = parseFloat(frequencyDiffSlider.value);
circles.forEach((cercle, index) => {
const rect = cercle.getBoundingClientRect();
const centerX = (rect.left + rect.width / 2) * pixelRatio;
const centerY = (rect.top + rect.height / 2) * pixelRatio;
const frequency = minFrequency + index * frequencyDiff;
centers.push({ x: centerX, y: centerY, frequency: frequency });
});
await setDoc(doc(db, 'users', signed_in_user.uid), { blinkingCircles: centers }, { merge: true });
console.log(centers);
// For demonstration, displaying the centers in the console
}
// Function to toggle the visibility of the sliders
function toggleSliders() {
if (sliderContainer.style.display === 'none') {
sliderContainer.style.display = 'flex';
} else {
sliderContainer.style.display = 'none';
}
}
// Function to make the browser fullscreen
function makeFullscreen() {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen();
// setTimeout(() => {
// logCircleCenters();
// }, 1000);
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
}
}
}
minFrequencySlider.addEventListener('input', updateValues);
frequencyDiffSlider.addEventListener('input', updateValues);
circleSizeSlider.addEventListener('input', updateValues);
fpsSelect.addEventListener('change', updateValues);
toggleSlidersButton.addEventListener('click', toggleSliders);
fullscreenButton.addEventListener('click', makeFullscreen);
circleSpacingSlider.addEventListener("input", updateValues);
// Initialize the grid when the document is loaded
document.addEventListener('DOMContentLoaded', () => {
createGrid();
});