DEV Community

Cover image for Create Custom Profile Initial Avatar with Live Preview
Avioflagos
Avioflagos

Posted on

Create Custom Profile Initial Avatar with Live Preview

Username-to-Avatar Transformer: See Your Initials Come Alive

This little experiment of mine shows how simple it can be to create personalized avatars just from someone's username. Type anything in the box and watch as it instantly transforms into a sleek profile picture with perfectly matched colors.

I built this because I was tired of boring default avatars that all look the same. Why show a generic silhouette when you can display something that feels personalized to each user - even before they upload their own photo?

Image description

The Magic Behind It

The core of this avatar generator is surprisingly simple. Here's the function that extracts initials from a username:

// Get initials from username (up to 2 characters)
function getInitials(username) {
  if (!username) return "??";
  return username.length > 1 
    ? username.substring(0, 2).toUpperCase() 
    : username.toUpperCase();
}
Enter fullscreen mode Exit fullscreen mode

And here's how it generates consistent colors for each username:

// Generate color based on username
function generateUserColor(username) {
  if (!username) return "#CCCCCC";

  // Create a simple hash from the username
  let hash = 0;
  for (let i = 0; i < username.length; i++) {
    hash = username.charCodeAt(i) + ((hash << 5) - hash);
  }

  // Map hash to one of our predefined colors
  const index = Math.abs(hash) % colorPalette.length;
  return colorPalette[index];
}
Enter fullscreen mode Exit fullscreen mode

This ensures the same username always gets the same color, creating a consistent visual identity.

Key Features

  • 1. - Live Preview: See your avatar changes instantly as you type
  • 2. - Smart Color Generation: Usernames automatically get consistent, personalized colors
  • 3. - Initial Extraction: Automatically displays the first two letters of any username
  • 4. - Custom Photo Option: Already have a profile picture? Upload it with one click
  • 5. - Exportable Results: Save your finished avatar as PNG or JPEG
  • 6. - Accessibility Built-in: High-contrast mode and color-blind friendly design

Perfect For

  • Adding user avatars to your web app without requiring photo uploads
  • Creating consistent visual identities across platforms
  • Generating placeholder avatars that still feel personalized
  • Quickly creating profile images for new users or team members

Simple Yet Powerful

The avatar generator uses a clever hash algorithm to ensure consistent color assignments while maintaining a clean, modern aesthetic. No complex libraries or dependencies - just efficient, elegant code you can easily incorporate into your projects.

Try It Out!

Type your name and watch how quickly it generates a unique, personalized avatar. Perfect for implementing dashboards, comment sections, or user profiles!

Questions? Well.. you have AI, and can most definitely be able to solve or adapt to your usecase.

Top comments (0)