You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

letteravatar.js 2.9 kB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * LetterAvatar
  3. *
  4. * Artur Heinze
  5. * Create Letter avatar based on Initials
  6. * based on https://gist.github.com/leecrossley/6027780
  7. */
  8. (function(w, d){
  9. function LetterAvatar (name, size, color) {
  10. name = name || '';
  11. size = size || 60;
  12. var colours = [
  13. "#1abc9c", "#2ecc71", "#3498db", "#9b59b6", "#34495e", "#16a085", "#27ae60", "#2980b9", "#8e44ad", "#2c3e50",
  14. "#f1c40f", "#e67e22", "#e74c3c", "#00bcd4", "#95a5a6", "#f39c12", "#d35400", "#c0392b", "#bdc3c7", "#7f8c8d"
  15. ],
  16. nameSplit = String(name).split(' '),
  17. initials, charIndex, colourIndex, canvas, context, dataURI;
  18. if (nameSplit.length == 1) {
  19. initials = nameSplit[0] ? nameSplit[0].charAt(0):'?';
  20. } else {
  21. initials = nameSplit[0].charAt(0) + nameSplit[1].charAt(0);
  22. }
  23. if (w.devicePixelRatio) {
  24. size = (size * w.devicePixelRatio);
  25. }
  26. charIndex = (initials == '?' ? 72 : initials.charCodeAt(0)) - 64;
  27. colourIndex = charIndex % 20;
  28. canvas = d.createElement('canvas');
  29. canvas.width = size;
  30. canvas.height = size;
  31. context = canvas.getContext("2d");
  32. context.fillStyle = color ? color : colours[colourIndex - 1];
  33. context.fillRect (0, 0, canvas.width, canvas.height);
  34. context.font = Math.round(canvas.width/2)+"px 'Microsoft Yahei'";
  35. context.textAlign = "center";
  36. context.fillStyle = "#FFF";
  37. context.fillText(initials, size / 2, size / 1.5);
  38. dataURI = canvas.toDataURL();
  39. canvas = null;
  40. return dataURI;
  41. }
  42. LetterAvatar.transform = function() {
  43. Array.prototype.forEach.call(d.querySelectorAll('img[avatar]'), function(img, name, color) {
  44. name = img.getAttribute('avatar');
  45. color = img.getAttribute('color');
  46. img.src = LetterAvatar(name, img.getAttribute('width'), color);
  47. img.removeAttribute('avatar');
  48. img.setAttribute('alt', name);
  49. });
  50. };
  51. // AMD support
  52. if (typeof define === 'function' && define.amd) {
  53. define(function () { return LetterAvatar; });
  54. // CommonJS and Node.js module support.
  55. } else if (typeof exports !== 'undefined') {
  56. // Support Node.js specific `module.exports` (which can be a function)
  57. if (typeof module != 'undefined' && module.exports) {
  58. exports = module.exports = LetterAvatar;
  59. }
  60. // But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
  61. exports.LetterAvatar = LetterAvatar;
  62. } else {
  63. window.LetterAvatar = LetterAvatar;
  64. d.addEventListener('DOMContentLoaded', function(event) {
  65. LetterAvatar.transform();
  66. });
  67. }
  68. })(window, document);