javascript 根据文字生成字母头像

Javascript 根据文字生成字母头像代码如下:

<html>
  <head>
    <title>
      前端根据文字生成字母头像
    </title>
  </head>
  
  <body>
    <img width="156" height="156" avatar="T" color="#000000">
    <script>
      /**
		 * LetterAvatar
		 * 
		 * Artur Heinze
		 * Create Letter avatar based on Initials
		 * based on https://gist.github.com/leecrossley/6027780
		 */
      (function(w, d) {
        function LetterAvatar(name, size, color) {
          name = name || '';
          size = size || 60;
          var colours = ["#1abc9c", "#2ecc71", "#3498db", "#9b59b6", "#34495e", "#16a085", "#27ae60", "#2980b9", "#8e44ad", "#2c3e50", "#f1c40f", "#e67e22", "#e74c3c", "#00bcd4", "#95a5a6", "#f39c12", "#d35400", "#c0392b", "#bdc3c7", "#7f8c8d"],
          nameSplit = String(name).split(' '),
          initials,
          charIndex,
          colourIndex,
          canvas,
          context,
          dataURI;
          if (nameSplit.length == 1) {
            initials = nameSplit[0] ? nameSplit[0].charAt(0) : '?';
          } else {
            initials = nameSplit[0].charAt(0) + nameSplit[1].charAt(0);
          }
          if (w.devicePixelRatio) {
            size = (size * w.devicePixelRatio);
          }

          charIndex = (initials == '?' ? 72 : initials.charCodeAt(0)) - 64;
          colourIndex = charIndex % 20;
          canvas = d.createElement('canvas');
          canvas.width = size;
          canvas.height = size;
          context = canvas.getContext("2d");

          context.fillStyle = color ? color: colours[colourIndex - 1];
          context.fillRect(0, 0, canvas.width, canvas.height);
          context.font = Math.round(canvas.width / 2) + "px 'Microsoft Yahei'";
          context.textAlign = "center";
          context.fillStyle = "#FFF";
          context.fillText(initials, size / 2, size / 1.5);
          dataURI = canvas.toDataURL();
          canvas = null;
          return dataURI;
        }
        LetterAvatar.transform = function() {
          Array.prototype.forEach.call(d.querySelectorAll('img[avatar]'),
          function(img, name, color) {
            name = img.getAttribute('avatar');
            color = img.getAttribute('color');
            img.src = LetterAvatar(name, img.getAttribute('width'), color);
            img.removeAttribute('avatar');
            img.setAttribute('alt', name);
          });
        };
        // AMD support
        if (typeof define === 'function' && define.amd) {

          define(function() {
            return LetterAvatar;
          });

          // CommonJS and Node.js module support.
        } else if (typeof exports !== 'undefined') {

          // Support Node.js specific `module.exports` (which can be a function)
          if (typeof module != 'undefined' && module.exports) {
            exports = module.exports = LetterAvatar;
          }
          // But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
          exports.LetterAvatar = LetterAvatar;
        } else {

          window.LetterAvatar = LetterAvatar;
          d.addEventListener('DOMContentLoaded',
          function(event) {
            LetterAvatar.transform();
          });
        }
      })(window, document);
    </script>
  </body>

</html>

参考博客:https://mkblog.cn/1886/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值