pulse-whoop / components /vital-circle.js
CSNET's picture
ช่วยแก้ JS และกราฟ ไม่ให้เพี้ยนที
9539581 verified
Raw History Blame Contribute Delete
8.67 kB
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);