-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
78 lines (71 loc) · 3.29 KB
/
script.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
document.addEventListener('DOMContentLoaded', function () {
const themeToggle = document.getElementById('theme-toggle');
const currentTheme = localStorage.getItem('theme');
// Set initial theme based on localStorage or system preference
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
themeToggle.checked = currentTheme === 'dark';
} else {
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
document.documentElement.setAttribute('data-theme', systemTheme);
localStorage.setItem('theme', systemTheme);
themeToggle.checked = systemTheme === 'dark';
}
// Function to toggle theme
function toggleTheme() {
const theme = themeToggle.checked ? 'dark' : 'light';
document.documentElement.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
}
// Event listener for theme toggle
themeToggle.addEventListener('change', toggleTheme);
document.getElementById('start-button').addEventListener('click', () => {
document.getElementById('welcome-box').classList.add('hidden');
document.getElementById('customization-box').classList.remove('hidden');
});
document.getElementById('generate-button').addEventListener('click', () => {
const style = document.getElementById('style').value;
const seed = document.getElementById('seed').value;
const fileType = document.querySelector('input[name="file-type"]:checked').value;
const flip = document.querySelector('input[name="flip"]:checked').value;
const apiUrl = `https://api.dicebear.com/8.x/${style}/${fileType}?seed=${seed}&flip=${flip}`;
fetch(apiUrl)
.then(response => {
if (fileType === 'svg') {
return response.text();
} else {
return response.blob();
}
})
.then(data => {
const img = document.getElementById('avatar-image');
const downloadButton = document.getElementById('download-button');
if (fileType === 'svg') {
const blob = new Blob([data], { type: 'image/svg+xml' });
const url = URL.createObjectURL(blob);
img.src = url;
img.dataset.downloadUrl = url;
downloadButton.disabled = false;
} else {
const url = URL.createObjectURL(data);
img.src = url;
img.dataset.downloadUrl = url;
downloadButton.disabled = false;
}
})
.catch(error => console.error('Error generating avatar:', error));
});
// Add event listener for download button
document.getElementById('download-button').addEventListener('click', () => {
const img = document.getElementById('avatar-image');
const downloadUrl = img.dataset.downloadUrl;
if (downloadUrl) {
const a = document.createElement('a');
a.href = downloadUrl;
a.download = 'avatar';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
});
});