-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
199 lines (176 loc) · 7.61 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lifetime to 24-hour Day Mapping</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 20px;
}
.container {
max-width: 70vw;
margin: 0 auto;
position: relative;
}
.slider {
width: 100%;
}
.buttons {
margin: 20px 0;
}
.sky {
bottom: 0;
position: absolute;
width: 100vw;
height: 300px;
background: linear-gradient(to top, #87CEEB, #ffffff);
overflow: hidden;
}
.sun {
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 50px;
height: 50px;
background: yellow;
border-radius: 50%;
}
</style>
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
</head>
<body>
<div class="container">
<h1>Map Lifetime to 24-hour Day</h1>
<div>
<label for="max-life">Max Life (Years): </label>
<input type="number" id="max-life" value="80" min="1" max="150" step="1">
</div>
<div class="buttons">
<button onclick="setMaxLife(70)">70 years</button>
<button onclick="setMaxLife(80)">80 years</button>
<button onclick="setMaxLife(90)">90 years</button>
</div>
<div>
<label for="current-age">Current Age: <span id="current-age-value">0</span></label>
<input type="range" id="current-age" class="slider" value="0" min="0" max="80">
</div>
<div>
<h2>Current Time: <span id="current-time">12:00 AM</span></h2>
</div>
</div>
<div class="sky">
<div class="sun" id="sun"></div>
</div>
<script>
const maxLifeInput = document.getElementById('max-life');
const currentAgeSlider = document.getElementById('current-age');
const currentAgeValue = document.getElementById('current-age-value');
const currentTimeDisplay = document.getElementById('current-time');
const sun = document.getElementById('sun');
const sunriseHour = 6; // 6 AM
const sunsetHour = 20.5; // 8:30 PM
const activeDayMinutes = (sunsetHour - sunriseHour) * 60; // Total active minutes in the day
function getGradientColors(timeInMinutes) {
const times = [
{ time: 0, colors: ["#000000", "#000033"] },
{ time: 240, colors: ["#000011", "#000044"] },
{ time: 500, colors: ["#FF4500", "#FFD700"] },
{ time: 680, colors: ["#87CEEB", "#FFFFFF"] },
{ time: 780, colors: ["#87CEEB", "#FFFFFF"] },
{ time: 960, colors: ["#87CEEB", "#FFFFFF"] },
{ time: 1170, colors: ["#FFD700", "#FF4500"] },
{ time: 1440, colors: ["#000000", "#000033"] },
];
function interpolateColor(color1, color2, factor) {
const hex = (color) => parseInt(color.slice(1), 16);
const r = (color) => (color >> 16) & 0xff;
const g = (color) => (color >> 8) & 0xff;
const b = (color) => color & 0xff;
const color1Hex = hex(color1);
const color2Hex = hex(color2);
const resultR = Math.round(r(color1Hex) + factor * (r(color2Hex) - r(color1Hex)));
const resultG = Math.round(g(color1Hex) + factor * (g(color2Hex) - g(color1Hex)));
const resultB = Math.round(b(color1Hex) + factor * (b(color2Hex) - b(color1Hex)));
return `#${((1 << 24) + (resultR << 16) + (resultG << 8) + resultB).toString(16).slice(1)}`;
}
function blendColors(time) {
let prev = times[0];
let next = times[times.length - 1];
for (let i = 0; i < times.length - 1; i++) {
if (time >= times[i].time && time <= times[i + 1].time) {
prev = times[i];
next = times[i + 1];
break;
}
}
const factor = Math.abs(time - prev.time) / (next.time - prev.time);
const color1 = interpolateColor(prev.colors[0], next.colors[0], factor);
const color2 = interpolateColor(prev.colors[1], next.colors[1], factor);
return [color1, color2];
}
return blendColors(timeInMinutes);
}
function updateSkyColor(timeInMinutes) {
const sky = document.querySelector('.sky');
getGradientColors(timeInMinutes);
const [color1, color2] = getGradientColors(timeInMinutes);
sky.style.background = `linear-gradient(to top, ${color1}, ${color2})`;
}
function updateCurrentTime() {
const maxLife = parseInt(maxLifeInput.value);
const currentAge = parseInt(currentAgeSlider.value);
// Map the current age to the 24-hour day period (0 to 1440 minutes)
const totalDayMinutes = 1440;
const sliderTimeInMinutes = (currentAge / maxLife) * totalDayMinutes;
// Map the slider time to the active day period (6 AM to 8:30 PM)
let activeTimeInMinutes;
if (sliderTimeInMinutes < sunriseHour * 60) {
activeTimeInMinutes = 0;
} else if (sliderTimeInMinutes > sunsetHour * 60) {
activeTimeInMinutes = activeDayMinutes;
} else {
activeTimeInMinutes = sliderTimeInMinutes - sunriseHour * 60;
}
const hours = Math.floor(sliderTimeInMinutes / 60);
const minutes = Math.floor(sliderTimeInMinutes % 60);
const ampm = hours >= 12 ? 'PM' : 'AM';
const displayHours = hours % 12 || 12;
const displayMinutes = minutes.toString().padStart(2, '0');
currentTimeDisplay.textContent = `${displayHours}:${displayMinutes} ${ampm}`;
updateSunPosition(activeTimeInMinutes);
updateSkyColor(sliderTimeInMinutes);
}
function updateSunPosition(activeTimeInMinutes) {
const percentageOfDay = activeTimeInMinutes / activeDayMinutes;
const sunAngle = percentageOfDay * Math.PI; // Angle in radians from 0 to Pi (half circle)
const sky = document.querySelector('.sky');
const centerX = sky.clientWidth / 2;
const centerY = sky.clientHeight; // Bottom of the sky container
const radius = sky.clientHeight; // Radius should be half of the sky height
const sunPositionX = centerX - radius * Math.cos(sunAngle);
const sunPositionY = centerY - radius * Math.sin(sunAngle);
sun.style.left = `${sunPositionX}px`;
sun.style.top = `${sunPositionY}px`;
}
function setMaxLife(years) {
maxLifeInput.value = years;
currentAgeSlider.max = years;
updateCurrentTime();
}
maxLifeInput.addEventListener('input', () => {
currentAgeSlider.max = maxLifeInput.value;
updateCurrentTime();
});
currentAgeSlider.addEventListener('input', () => {
currentAgeValue.textContent = currentAgeSlider.value;
updateCurrentTime();
});
// Initialize display
updateCurrentTime();
</script>
</body>
</html>