-
Notifications
You must be signed in to change notification settings - Fork 11
/
material-avatar.min.js
1 lines (1 loc) · 3.72 KB
/
material-avatar.min.js
1
!function(t,i){function e(t,i){if(!t)throw new Error("No elements selected/found");var e=this;this.options={colorPalette:["#1abc9c","#2ecc71","#3498db","#9b59b6","#34495e","#16a085","#27ae60","#2980b9","#8e44ad","#2c3e50","#f1c40f","#e67e22","#e74c3c","#95a5a6","#f39c12","#d35400","#c0392b","#bdc3c7","#7f8c8d"],fontFamily:"Arial"},this.name="MaterialAvatar",o(e.options,i),this.elements=t,this.elements[0]?(this.elements=[].slice.call(this.elements),this.elements.forEach(function(t){t.avatar=new n(t,e.options)})):this.elements.avatar=new n(t,e.options)}function n(t,e){if(!t)throw new Error("No element selected/found");var n=this;this.element=t,this.options=e,this.canvas=i.createElement("canvas"),requestAnimationFrame(function(){return n.init()})}function o(t,i){for(var e in i)i.hasOwnProperty(e)&&(t[e]=i[e])}e.prototype.updateOptions=function(t){var i=this;t&&(this.options=t),this.elements.forEach(function(t){t.avatar.options=i.options})},n.prototype.init=function(){this.width=parseInt(this.element.offsetWidth,10),this.height=parseInt(this.element.offsetHeight,10),this.canvas.setAttribute("width",this.width),this.canvas.setAttribute("height",this.width),this.initials=this.getInitials(),this.fontSize=this.getFontSize(),this.render()},n.prototype.render=function(){this.backgroundColor=this.generateColor(this.initials.charCodeAt(0)-65),this.context=this.canvas.getContext("2d"),this.context.font=this.fontSize+"px/0px "+this.options.fontFamily,this.context.textAlign="center",this.options&&"circle"===this.options.shape?this._drawCircle():this._drawSquare(),this.context.fillStyle=this.getTextColor(),this.context.fillText(this.initials,this.width/2,this.height/2+.68*this.fontSize/2),this.element.innerHTML="",this.element.appendChild(this.canvas)},n.prototype._drawCircle=function(){var t=this.width/2,i=this.height/2,e=this.width/2;this.context.beginPath(),this.context.arc(t,i,e,0,2*Math.PI,!1),this.context.fillStyle=this.backgroundColor,this.context.fill()},n.prototype._drawSquare=function(){this.context.fillStyle=this.backgroundColor,this.context.fillRect(0,0,this.width,this.height)},n.prototype.getInitials=function(){if(this.options.initials)return this.options.initials;this.name=this.options.name||this.element.getAttribute("data-name")||this.element.innerHTML.trim();var t,i=this.name.split(" ");return this.element.setAttribute("data-name",this.name),t=i.length>1?i[0].charAt(0).toUpperCase()+i[1].charAt(0).toUpperCase():i[0].charAt(0).toUpperCase()},n.prototype.getFontSize=function(){if(this.options.fontSize)return"function"==typeof this.options.fontSize?this.options.fontSize(this.height,this.initials.length):this.options.fontSize;var t=this.height/(.5*this.initials.length+1);return t},n.prototype.getTextColor=function(){if(this.options.textColor)return this.options.textColor;var t=this._hexToRgb(this.backgroundColor);if(!t)return"#222";var i=299*t.r+587*t.g+114*t.b;return Math.round(i/1e3)>125?"#222":"#fff"},n.prototype._hexToRgb=function(t){var i;return t=t.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(t,i,e,n){return i+i+e+e+n+n}),i=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t),i?{r:parseInt(i[1],16),g:parseInt(i[2],16),b:parseInt(i[3],16)}:null},n.prototype.generateColor=function(t){if(this.options.backgroundColor)return this.options.backgroundColor;if(void 0!==typeof randomColor){if(this.options&&this.options.randomColor)return randomColor(this.options.randomColor);if(!this.options)return randomColor()}return this.options.colorPalette[t%this.options.colorPalette.length]},t.MaterialAvatar=e,"undefined"!=typeof jQuery&&"undefined"!=typeof jQuery.fn&&(jQuery.fn.materialAvatar=function(t){return this.each(function(){jQuery.data(this,"plugin_materialAvatar")||jQuery.data(this,"plugin_materialAvatar",new e(this,t))})})}(window,document);