class AvatarCustomizer extends HTMLElement {
constructor() {
super();
this.avatarData = {
gender: 'female',
skinTone: '#f8d9b4',
hairStyle: 'long',
hairColor: '#2c140c',
eyeColor: '#3a74a8',
facialFeature: 'none',
clothing: 'casual',
accessories: 'none'
};
this.options = {
gender: ['male', 'female'],
skinTone: ['#f8d9b4', '#e0b080', '#c68650', '#a0522d', '#6b3e1e'],
hairStyle: ['short', 'medium', 'long', 'curly', 'ponytail', 'bun'],
hairColor: ['#2c140c', '#4a2c14', '#8b4513', '#d2b48c', '#000000', '#808080', '#ff0000', '#ffa500'],
eyeColor: ['#3a74a8', '#5e8c31', '#8d67a8', '#8b4513', '#000000'],
facialFeature: ['none', 'freckles', 'mole', 'scar'],
clothing: ['casual', 'formal', 'fantasy', 'punk', 'sporty'],
accessories: ['none', 'glasses', 'hat', 'earrings', 'necklace']
};
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.render();
this.setupEventListeners();
}
render() {
this.shadowRoot.innerHTML = `
${Object.keys(this.options).map(category => `
${this.capitalizeFirstLetter(category)}
${this.renderOptions(category)}
`).join('')}
`;
this.updateAvatarPreview();
}
renderOptions(category) {
if (category === 'skinTone' || category === 'hairColor' || category === 'eyeColor') {
return this.options[category].map(color => `
`).join('');
}
return this.options[category].map(option => `
${this.capitalizeFirstLetter(option)}
`).join('');
}
setupEventListeners() {
// Option selection
this.shadowRoot.querySelectorAll('.option-item').forEach(item => {
item.addEventListener('click', (e) => {
const category = e.target.dataset.category;
const value = e.target.dataset.value;
this.selectOption(category, value);
});
});
// Action buttons
this.shadowRoot.getElementById('save-avatar').addEventListener('click', () => {
this.saveAvatar();
});
this.shadowRoot.getElementById('randomize').addEventListener('click', () => {
this.randomizeAvatar();
});
this.shadowRoot.getElementById('next-step').addEventListener('click', () => {
this.dispatchEvent(new CustomEvent('step-change', { detail: { step: 2 } }));
});
}
selectOption(category, value) {
this.avatarData[category] = value;
this.render();
}
updateAvatarPreview() {
const preview = this.shadowRoot.querySelector('.avatar-container');
if (!preview) return;
// Update body
const body = preview.querySelector('.body');
body.style.backgroundColor = this.avatarData.skinTone;
body.style.width = '100px';
body.style.height = '120px';
body.style.top = '180px';
body.style.left = '50px';
body.style.borderRadius = '50% 50% 0 0';
// Update head
const head = preview.querySelector('.head');
head.style.backgroundColor = this.avatarData.skinTone;
head.style.width = '80px';
head.style.height = '80px';
head.style.top = '100px';
head.style.left = '60px';
head.style.borderRadius = '50%';
// Update hair
const hair = preview.querySelector('.hair');
hair.style.backgroundColor = this.avatarData.hairColor;
switch(this.avatarData.hairStyle) {
case 'short':
hair.style.width = '90px';
hair.style.height = '40px';
hair.style.top = '90px';
hair.style.left = '55px';
hair.style.borderRadius = '50% 50% 0 0';
break;
case 'long':
hair.style.width = '90px';
hair.style.height = '120px';
hair.style.top = '70px';
hair.style.left = '55px';
hair.style.borderRadius = '50% 50% 0 0';
break;
case 'ponytail':
hair.style.width = '40px';
hair.style.height = '100px';
hair.style.top = '70px';
hair.style.left = '95px';
hair.style.borderRadius = '50% 50% 0 0';
break;
}
// Update eyes
const eyes = preview.querySelector('.eyes');
eyes.style.backgroundColor = this.avatarData.eyeColor;
eyes.style.width = '10px';
eyes.style.height = '10px';
eyes.style.top = '130px';
eyes.style.left = '80px';
eyes.style.borderRadius = '50%';
eyes.style.boxShadow = '15px 0 0 ' + this.avatarData.eyeColor;
}
saveAvatar() {
// Simulate coin deduction
const coinElement = this.shadowRoot.getElementById('coin-count');
let coins = parseInt(coinElement.textContent);
if (coins >= 50) {
coins -= 50;
coinElement.textContent = `${coins} Coins`;
// Show success message
alert('Avatar saved successfully!');
} else {
alert('Not enough coins! Complete more chats to earn coins.');
}
}
randomizeAvatar() {
Object.keys(this.options).forEach(category => {
const options = this.options[category];
const randomIndex = Math.floor(Math.random() * options.length);
this.avatarData[category] = options[randomIndex];
});
this.render();
}
capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
}
customElements.define('avatar-customizer', AvatarCustomizer);