Spaces:
Running
Running
File size: 8,668 Bytes
4a7cb62 9539581 4a7cb62 9539581 4a7cb62 9539581 4a7cb62 9539581 4a7cb62 9539581 4a7cb62 9539581 4a7cb62 9539581 4a7cb62 | 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 | class VitalCircle extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
connectedCallback() {
this.render();
// Delay animation to ensure DOM is ready
setTimeout(() => {
this.animateValue();
}, 100);
}
render() {
const type = this.getAttribute('type') || 'recovery';
const value = this.getAttribute('value') || '0';
const label = this.getAttribute('label') || 'การวัด';
const color = this.getAttribute('color') || 'from-green-400 to-emerald-500';
const max = parseInt(this.getAttribute('max') || '100');
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
position: relative;
}
.vital-container {
position: relative;
width: 100%;
aspect-ratio: 1;
border-radius: 50%;
background: rgba(31, 41, 55, 0.4);
backdrop-filter: blur(10px);
border: 1px solid rgba(75, 85, 99, 0.3);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
transition: all 0.4s ease;
cursor: pointer;
overflow: hidden;
}
.vital-container::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(135deg, ${this.getColorValues(color)});
opacity: 0.15;
border-radius: 50%;
transition: opacity 0.3s ease;
}
.vital-container:hover {
transform: translateY(-5px);
border-color: rgba(52, 211, 153, 0.5);
box-shadow: 0 8px 25px rgba(52, 211, 153, 0.15);
}
.vital-container:hover::before {
opacity: 0.25;
}
.vital-value {
font-size: 1.5rem;
font-weight: 700;
background: linear-gradient(135deg, ${this.getColorValues(color)});
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
margin-bottom: 0.25rem;
transition: all 0.5s ease;
}
.vital-label {
font-size: 0.75rem;
color: #9ca3af;
font-weight: 500;
text-align: center;
padding: 0 0.5rem;
}
.vital-icon {
position: absolute;
top: 0.5rem;
right: 0.5rem;
width: 12px;
height: 12px;
border-radius: 50%;
background: linear-gradient(135deg, ${this.getColorValues(color)});
box-shadow: 0 0 8px currentColor;
}
.progress-ring {
position: absolute;
top: -2px;
left: -2px;
right: -2px;
bottom: -2px;
border-radius: 50%;
border: 2px solid transparent;
border-top-color: ${this.getPrimaryColor(color)};
animation: spin 2s linear infinite;
opacity: 0;
transition: opacity 0.3s ease;
}
.vital-container.loading .progress-ring {
opacity: 1;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.trend-indicator {
position: absolute;
bottom: 0.5rem;
left: 50%;
transform: translateX(-50%);
font-size: 0.6rem;
color: #34d399;
font-weight: 600;
}
.trend-indicator.down {
color: #f87171;
}
@media (max-width: 640px) {
.vital-value {
font-size: 1.25rem;
}
.vital-label {
font-size: 0.65rem;
}
}
</style>
<div class="vital-container" id="vitalContainer">
<div class="progress-ring"></div>
<div class="vital-icon"></div>
<div class="vital-value" id="vitalValue">${value}</div>
<div class="vital-label">${label}</div>
<div class="trend-indicator" id="trendIndicator"></div>
</div>
`;
}
getColorValues(colorClass) {
const colorMap = {
'from-green-400 to-emerald-500': '#34d399, #10b981',
'from-orange-400 to-red-500': '#fb923c, #ef4444',
'from-blue-400 to-indigo-500': '#60a5fa, #6366f1',
'from-purple-500 to-pink-600': '#a855f7, #db2777',
'from-yellow-400 to-orange-500': '#fbbf24, #f97316'
};
return colorMap[colorClass] || colorMap['from-green-400 to-emerald-500'];
}
getPrimaryColor(colorClass) {
const colorMap = {
'from-green-400 to-emerald-500': '#34d399',
'from-orange-400 to-red-500': '#fb923c',
'from-blue-400 to-indigo-500': '#60a5fa',
'from-purple-500 to-pink-600': '#a855f7',
'from-yellow-400 to-orange-500': '#fbbf24'
};
return colorMap[colorClass] || '#34d399';
}
animateValue() {
try {
const valueElement = this.shadowRoot.getElementById('vitalValue');
if (!valueElement) return;
const endValue = parseFloat(this.getAttribute('value') || '0');
const duration = 1500;
const startTime = performance.now();
const animate = (currentTime) => {
try {
const elapsed = currentTime - startTime;
const progress = Math.min(elapsed / duration, 1);
const easeOutQuart = 1 - Math.pow(1 - progress, 4);
const currentValue = (endValue * easeOutQuart).toFixed(1);
valueElement.textContent = currentValue % 1 === 0 ? currentValue.split('.')[0] : currentValue;
if (progress < 1) {
requestAnimationFrame(animate);
} else {
this.addTrendIndicator();
}
} catch (innerError) {
console.warn('Animation frame error:', innerError);
}
};
requestAnimationFrame(animate);
} catch (error) {
console.error('Animate value error:', error);
}
}
addTrendIndicator() {
try {
const trendElement = this.shadowRoot.getElementById('trendIndicator');
if (!trendElement) return;
const trends = ['↑ 5%', '↓ 3%', '→ เท่าเดิม', '↑ 12%', '↓ 8%'];
const randomTrend = trends[Math.floor(Math.random() * trends.length)];
trendElement.textContent = randomTrend;
trendElement.className = 'trend-indicator';
if (randomTrend.includes('↓')) {
trendElement.classList.add('down');
}
} catch (error) {
console.error('Trend indicator error:', error);
}
}
showLoading() {
const container = this.shadowRoot.getElementById('vitalContainer');
container.classList.add('loading');
}
hideLoading() {
const container = this.shadowRoot.getElementById('vitalContainer');
container.classList.remove('loading');
}
updateValue(newValue) {
this.setAttribute('value', newValue);
this.animateValue();
}
}
customElements.define('vital-circle', VitalCircle); |