Skip to content

Instantly share code, notes, and snippets.

@leecrossley
Created July 17, 2013 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leecrossley/6018599 to your computer and use it in GitHub Desktop.
Save leecrossley/6018599 to your computer and use it in GitHub Desktop.
A CodePen by Lee Crossley. Avatar Generator from Name - A name (first name and surname) is input and a canvas element is output using the initials from the name and a background colour (based on the first name first letter). The background colours are from from http://flatuicolors.com/
<canvas id="user-icon" width="256" height="256"></canvas>
var colours = ["#1abc9c", "#2ecc71", "#3498db", "#9b59b6", "#34495e", "#16a085", "#27ae60", "#2980b9", "#8e44ad", "#2c3e50", "#f1c40f", "#e67e22", "#e74c3c", "#ecf0f1", "#95a5a6", "#f39c12", "#d35400", "#c0392b", "#bdc3c7", "#7f8c8d"];
var name = "Lee Crossley",
nameSplit = name.split(" "),
initials = nameSplit[0].charAt(0).toUpperCase() + nameSplit[1].charAt(0).toUpperCase();
var charIndex = initials.charCodeAt(0) - 64,
colourIndex = charIndex % 20;
var canvas = document.getElementById("user-icon");
var context = canvas.getContext("2d");
context.fillStyle = colours[colourIndex - 1];
context.fillRect (0, 0, canvas.width, canvas.height);
context.font = "128px Arial";
context.textAlign = "center";
context.fillStyle = "#FFF";
context.fillText(initials, canvas.width / 2, canvas.height / 1.5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment