File size: 8,882 Bytes
9a962ce 0c262e2 9a962ce 0c262e2 9a962ce 0c262e2 9a962ce 0c262e2 9a962ce 4bcb726 9a962ce 4bcb726 9a962ce | 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | ---
// TrackioWrapper.astro
import Trackio from './trackio/Trackio.svelte';
---
<!-- Ensure Roboto Mono is loaded for Oblivion theme -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;600;700&display=swap" rel="stylesheet">
<div class="trackio-wrapper">
<div class="trackio-controls">
<div class="controls-left">
<div class="theme-selector">
<label for="theme-select">Theme</label>
<select id="theme-select" class="theme-select">
<option value="classic">Classic</option>
<option value="oblivion">Oblivion</option>
</select>
</div>
<div class="scale-controls">
<label>
<input type="checkbox" id="log-scale-x" checked>
Log Scale X
</label>
<label>
<input type="checkbox" id="smooth-data" checked>
Smooth
</label>
</div>
</div>
<button class="button button--ghost" type="button" id="randomize-btn">
Randomize Data
</button>
</div>
<div class="trackio-container">
<Trackio client:load variant="classic" logScaleX={true} smoothing={true} />
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', async () => {
const themeSelect = document.getElementById('theme-select');
const randomizeBtn = document.getElementById('randomize-btn');
const logScaleXCheckbox = document.getElementById('log-scale-x');
const smoothDataCheckbox = document.getElementById('smooth-data');
const trackioContainer = document.querySelector('.trackio-container');
if (!themeSelect || !randomizeBtn || !logScaleXCheckbox || !smoothDataCheckbox || !trackioContainer) return;
// Import the store function
const { triggerJitter } = await import('./trackio/store.js');
// Theme change handler
themeSelect.addEventListener('change', (e) => {
const target = e.target;
if (!target || !('value' in target)) return;
const newVariant = target.value;
console.log(`Theme changed to: ${newVariant}`); // Debug log
// Find the trackio element and call setTheme on the Svelte instance
const trackioEl = trackioContainer.querySelector('.trackio');
if (trackioEl && trackioEl.__trackioInstance) {
console.log('Calling setTheme on Trackio instance'); // Debug log
trackioEl.__trackioInstance.setTheme(newVariant);
} else {
// Fallback: just update CSS classes
console.log('No Trackio instance found, updating CSS classes only'); // Debug log
if (trackioEl) {
trackioEl.classList.remove('theme--classic', 'theme--oblivion');
trackioEl.classList.add(`theme--${newVariant}`);
}
}
});
// Log scale X change handler
logScaleXCheckbox.addEventListener('change', (e) => {
const target = e.target;
if (!target || !('checked' in target)) return;
const isLogScale = target.checked;
console.log(`Log scale X changed to: ${isLogScale}`); // Debug log
// Find the trackio element and call setLogScaleX on the Svelte instance
const trackioEl = trackioContainer.querySelector('.trackio');
if (trackioEl && trackioEl.__trackioInstance) {
console.log('Calling setLogScaleX on Trackio instance'); // Debug log
trackioEl.__trackioInstance.setLogScaleX(isLogScale);
} else {
console.log('Trackio instance not found for log scale change');
}
});
// Smooth data change handler
smoothDataCheckbox.addEventListener('change', (e) => {
const target = e.target;
if (!target || !('checked' in target)) return;
const isSmooth = target.checked;
console.log(`Smooth data changed to: ${isSmooth}`); // Debug log
// Find the trackio element and call setSmoothing on the Svelte instance
const trackioEl = trackioContainer.querySelector('.trackio');
if (trackioEl && trackioEl.__trackioInstance) {
console.log('Calling setSmoothing on Trackio instance'); // Debug log
trackioEl.__trackioInstance.setSmoothing(isSmooth);
} else {
console.log('Trackio instance not found for smooth change');
}
});
// Initialize with default checked states
setTimeout(() => {
if (logScaleXCheckbox.checked) {
const trackioEl = trackioContainer.querySelector('.trackio');
if (trackioEl && trackioEl.__trackioInstance) {
console.log('Initializing with log scale X enabled');
trackioEl.__trackioInstance.setLogScaleX(true);
}
}
if (smoothDataCheckbox.checked) {
const trackioEl = trackioContainer.querySelector('.trackio');
if (trackioEl && trackioEl.__trackioInstance) {
console.log('Initializing with smoothing enabled');
trackioEl.__trackioInstance.setSmoothing(true);
}
}
}, 100); // Small delay to ensure Trackio is fully loaded
// Randomize data handler - now uses the store
randomizeBtn.addEventListener('click', () => {
console.log('Randomize button clicked - triggering jitter via store'); // Debug log
// Add vibration animation
randomizeBtn.classList.add('vibrating');
setTimeout(() => {
randomizeBtn.classList.remove('vibrating');
}, 600);
// Test direct window approach as well
if (window.trackioInstance && typeof window.trackioInstance.jitterData === 'function') {
console.log('Found window.trackioInstance, calling jitterData directly'); // Debug log
window.trackioInstance.jitterData();
} else {
console.log('No window.trackioInstance found, using store trigger'); // Debug log
triggerJitter();
}
});
});
</script>
<style>
.trackio-wrapper {
width: 100%;
margin: 0px 0 20px 0;
}
.trackio-controls {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
padding: 12px 0px;
/* border-bottom: 1px solid var(--border-color); */
gap: 16px;
flex-wrap: nowrap;
}
.controls-left {
display: flex;
align-items: center;
gap: 24px;
flex-wrap: wrap;
}
.btn-randomize {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 8px 16px;
background: var(--accent-color, #007acc);
color: white;
border: none;
border-radius: 6px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
transition: all 0.15s ease;
}
.btn-randomize:hover {
background: var(--accent-hover, #005a9e);
transform: translateY(-1px);
}
.btn-randomize:active {
transform: translateY(0);
}
.theme-selector {
display: flex;
align-items: center;
gap: 8px;
font-size: 14px;
flex-shrink: 0;
white-space: nowrap;
}
.theme-selector label {
font-weight: 500;
color: var(--text-color);
}
.theme-select {
padding: 6px 12px;
border: 1px solid var(--border-color);
border-radius: 4px;
background: var(--input-bg, var(--surface-bg));
color: var(--text-color);
font-size: 14px;
cursor: pointer;
transition: border-color 0.15s ease;
}
.theme-select:focus {
outline: none;
border-color: var(--accent-color, #007acc);
}
.scale-controls {
display: flex;
align-items: center;
gap: 16px;
flex-shrink: 0;
white-space: nowrap;
}
/* Animation de vibration pour le bouton */
@keyframes vibrate {
0% { transform: translateX(0); }
10% { transform: translateX(-2px) rotate(-1deg); }
20% { transform: translateX(2px) rotate(1deg); }
30% { transform: translateX(-2px) rotate(-1deg); }
40% { transform: translateX(2px) rotate(1deg); }
50% { transform: translateX(-1px) rotate(-0.5deg); }
60% { transform: translateX(1px) rotate(0.5deg); }
70% { transform: translateX(-1px) rotate(-0.5deg); }
80% { transform: translateX(1px) rotate(0.5deg); }
90% { transform: translateX(-0.5px) rotate(-0.25deg); }
100% { transform: translateX(0) rotate(0); }
}
.button.vibrating {
animation: vibrate 0.6s ease-in-out;
}
.trackio-container {
width: 100%;
margin-top: 10px;
border: 1px solid var(--border-color);
padding: 24px 12px;
}
@media (max-width: 768px) {
.trackio-controls {
flex-direction: column;
align-items: stretch;
gap: 12px;
}
.controls-left {
flex-direction: column;
align-items: stretch;
gap: 12px;
}
.theme-selector {
justify-content: space-between;
}
.scale-controls {
justify-content: space-between;
}
}
</style> |