Here's the sample code to create a custom avatar builder using Vuetify 2 and Avataaars.io:
App.vue:
<template>
<v-app>
<div class="container">
<v-avatar size="200">
<img :src="avatarUrl" alt="Avatar" />
</v-avatar>
<v-tabs>
<v-tab v-for="(category, index) in avataaarsCategories" :key="index">
{{ category }}
</v-tab>
<v-tab-item v-for="(category, index) in avataaarsCategories" :key="index">
<v-container fluid>
<v-item-group v-model="selectedOptions[category]" row>
<v-item
v-for="option in avataaarsOptions[category]"
:key="option"
:value="option"
>
<v-card shaped width="80" height="80" class="mx-auto">
<v-avatar width="100%">
<img
:src="getOptionImageUrl(category, option)"
:alt="option"
/>
</v-avatar>
</v-card>
</v-item>
</v-item-group>
</v-container>
</v-tab-item>
</v-tabs>
</div>
</v-app>
</template>
<script>
export default {
data() {
return {
avataaarsCategories: ['topTypes', 'facialHairTypes', 'eyeTypes'],
avataaarsOptions: {
topTypes: ['NoHair', 'Hat', 'Hijab'],
facialHairTypes: ['Blank', 'BeardMedium', 'BeardLight'],
eyeTypes: ['Default', 'Dilate', 'EyeRoll'],
},
selectedOptions: {
topTypes: 'NoHair',
facialHairTypes: 'Blank',
eyeTypes: 'Default',
},
}
},
created() {
this.loadFromLocalStorage()
},
computed: {
avatarUrl() {
let baseUrl = 'https://avataaars.io/png?'
let params = []
for (let category in this.selectedOptions) {
params.push(`${category}=${this.selectedOptions[category]}`)
}
return baseUrl + params.join('&')
},
},
methods: {
getOptionImageUrl(category, option) {
const baseOptions = { ...this.selectedOptions }
baseOptions[category] = option
let baseUrl = 'https://avataaars.io/png?'
let params = []
for (let category in baseOptions) {
params.push(`${category}=${baseOptions[category]}`)
}
return baseUrl + params.join('&')
},
loadFromLocalStorage() {
const storedAvatar = JSON.parse(localStorage.getItem('avatar'))
if (storedAvatar) {
this.selectedOptions = storedAvatar
}
},
},
watch: {
selectedOptions: {
deep: true,
handler(newVal) {
localStorage.setItem('avatar', JSON.stringify(newVal))
},
},
},
}
</script>
<style scoped>
.container {
display: flex;
flex-direction: column;
align-items: center;
}
</style>
Replace the data's avataaarsCategories array and avataaarsOptions object with your own categories and options. When the user changes an option, the main avatar image will be updated, and the avatar will be stored in local storage. When the app is restarted, the corresponding item-group elements will be preselected according to the stored avatar's options, and the user will be able to see how each option would change the avatar using computed images. The getOptionImageUrl() method is used to create the URLs for the option images, while the avatarUrl computed property is used for the main avatar image.