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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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",
  14. "#2ecc71",
  15. "#3498db",
  16. "#9b59b6",
  17. "#34495e",
  18. "#16a085",
  19. "#27ae60",
  20. "#2980b9",
  21. "#8e44ad",
  22. "#2c3e50",
  23. "#f1c40f",
  24. "#e67e22",
  25. "#e74c3c",
  26. "#00bcd4",
  27. "#95a5a6",
  28. "#f39c12",
  29. "#d35400",
  30. "#c0392b",
  31. "#bdc3c7",
  32. "#7f8c8d",
  33. ],
  34. nameSplit = String(name).split(" "),
  35. initials,
  36. charIndex,
  37. colourIndex,
  38. canvas,
  39. context,
  40. dataURI;
  41. if (nameSplit.length == 1) {
  42. initials = nameSplit[0] ? nameSplit[0].charAt(0) : "?";
  43. } else {
  44. initials = nameSplit[0].charAt(0) + nameSplit[1].charAt(0);
  45. }
  46. let initials1 = initials.toUpperCase();
  47. if (w.devicePixelRatio) {
  48. size = size * w.devicePixelRatio;
  49. }
  50. charIndex = (initials == "?" ? 72 : initials.charCodeAt(0)) - 64;
  51. colourIndex = charIndex % 20;
  52. canvas = d.createElement("canvas");
  53. canvas.width = size;
  54. canvas.height = size;
  55. context = canvas.getContext("2d");
  56. context.fillStyle = color ? color : colours[colourIndex - 1];
  57. context.fillRect(0, 0, canvas.width, canvas.height);
  58. context.font = Math.round(canvas.width / 2) + "px 'Microsoft Yahei'";
  59. context.textAlign = "center";
  60. context.fillStyle = "#FFF";
  61. context.fillText(initials1, size / 2, size / 1.5);
  62. dataURI = canvas.toDataURL();
  63. canvas = null;
  64. return dataURI;
  65. }
  66. LetterAvatar.transform = function () {
  67. Array.prototype.forEach.call(
  68. d.querySelectorAll("img[avatar]"),
  69. function (img, name, color) {
  70. name = img.getAttribute("avatar");
  71. color = img.getAttribute("color");
  72. img.src = LetterAvatar(name, img.getAttribute("width"), color);
  73. img.removeAttribute("avatar");
  74. img.setAttribute("alt", name);
  75. }
  76. );
  77. };
  78. // AMD support
  79. if (typeof define === "function" && define.amd) {
  80. define(function () {
  81. return LetterAvatar;
  82. });
  83. // CommonJS and Node.js module support.
  84. } else if (typeof exports !== "undefined") {
  85. // Support Node.js specific `module.exports` (which can be a function)
  86. if (typeof module != "undefined" && module.exports) {
  87. exports = module.exports = LetterAvatar;
  88. }
  89. // But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
  90. exports.LetterAvatar = LetterAvatar;
  91. } else {
  92. window.LetterAvatar = LetterAvatar;
  93. d.addEventListener("DOMContentLoaded", function (event) {
  94. LetterAvatar.transform();
  95. });
  96. }
  97. })(window, document);