-
Notifications
You must be signed in to change notification settings - Fork 11
/
material-avatar.js
251 lines (194 loc) · 6.59 KB
/
material-avatar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
;(function(win, doc) {
/**
* Main function to create material avatars
* @param element - The element(s) to apply the material avatar look to
*/
function MaterialAvatar(elements, options) {
if (!elements) {
throw(new Error('No elements selected/found'));
}
var _this = 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';
extend(_this.options, options);
this.elements = elements;
if (this.elements[0]) {
//Turn our HTMLCollection into an array so we can iterate through it.
this.elements = [].slice.call(this.elements);
this.elements.forEach(function(element){
element.avatar = new Avatar(element, _this.options);
});
} else {
this.elements.avatar = new Avatar(elements, _this.options);
}
}
MaterialAvatar.prototype.updateOptions = function(options) {
var _this = this;
if (options) {
this.options = options;
}
this.elements.forEach(function(element){
element.avatar.options = _this.options;
});
};
function Avatar(element, options) {
if (!element) {
throw(new Error('No element selected/found'));
}
var _this = this;
this.element = element;
this.options = options;
this.canvas = doc.createElement('canvas');
//Push our reflows to a new animation frame.
requestAnimationFrame(function() {
return _this.init();
});
}
Avatar.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();
};
Avatar.prototype.render = function() {
this.backgroundColor = this.generateColor(this.initials.charCodeAt(0) - 65);
this.context = this.canvas.getContext('2d');
//Create our font styles
this.context.font = this.fontSize + 'px/0px ' + this.options.fontFamily;
this.context.textAlign = 'center';
//Decide what type of shape we should draw for the background
if (this.options) {
if(this.options.shape === 'circle') {
this._drawCircle();
} else {
this._drawSquare();
}
} else {
this._drawSquare();
}
//Create the color and add our initials
this.context.fillStyle = this.getTextColor();
this.context.fillText(
this.initials,
this.width/2,
(this.height / 2) + ((this.fontSize*0.68)/2)
);
//Remove the inner text and swap in the canvas elemnt
this.element.innerHTML = '';
this.element.appendChild(this.canvas);
};
//Creates circle background area
Avatar.prototype._drawCircle = function() {
var centerX = this.width / 2;
var centerY = this.height / 2;
var radius = this.width / 2;
this.context.beginPath();
this.context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
this.context.fillStyle = this.backgroundColor;
this.context.fill();
};
//Creates square background area
Avatar.prototype._drawSquare = function() {
this.context.fillStyle = this.backgroundColor;
this.context.fillRect(0, 0, this.width, this.height);
};
Avatar.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 _nameSplit = this.name.split(' ');
var _initials;
this.element.setAttribute('data-name', this.name);
//Get initials from name
if (_nameSplit.length > 1) {
_initials = _nameSplit[0].charAt(0).toUpperCase() + _nameSplit[1].charAt(0).toUpperCase();
} else {
_initials = _nameSplit[0].charAt(0).toUpperCase();
}
return _initials;
};
Avatar.prototype.getFontSize = function () {
if (this.options.fontSize) {
if(typeof this.options.fontSize === 'function') {
return this.options.fontSize(this.height, this.initials.length);
}
return this.options.fontSize;
}
var _fontSize = this.height/((this.initials.length*0.5) + 1);
return _fontSize;
};
Avatar.prototype.getTextColor = function () {
//Override generated text color with a custom one
if (this.options.textColor) {
return this.options.textColor;
}
var _hexColor = this._hexToRgb(this.backgroundColor);
//Optional fallback incase our function returns null
if (!_hexColor) return '#222';
var _colorValue = (_hexColor.r * 299) + (_hexColor.g * 587) + (_hexColor.b * 114);
return (Math.round(_colorValue/1000) > 125) ? '#222' : '#fff';
};
Avatar.prototype._hexToRgb = function (hex) {
var _result;
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
hex = hex.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, function(m, r, g, b) {
return r + r + g + g + b + b;
});
_result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
if (_result) {
return {
r: parseInt(_result[1], 16),
g: parseInt(_result[2], 16),
b: parseInt(_result[3], 16)
};
}
return null;
};
Avatar.prototype.generateColor = function (index) {
if (this.options.backgroundColor) {
return this.options.backgroundColor;
}
//Uses the randomColor generator - https://github.com/davidmerfield/randomColor
if (typeof randomColor !== undefined) {
if (this.options && this.options.randomColor) {
return randomColor(this.options.randomColor);
} else if (!this.options) {
return randomColor();
}
}
return this.options.colorPalette[Math.abs(index) % this.options.colorPalette.length];
};
// export
win.MaterialAvatar = MaterialAvatar;
if (typeof jQuery !== 'undefined' && typeof jQuery.fn !== 'undefined') {
jQuery.fn.materialAvatar = function(options) {
return this.each(function() {
if (!jQuery.data(this, 'plugin_materialAvatar')) {
jQuery.data(this, 'plugin_materialAvatar', new MaterialAvatar(this, options));
}
});
};
}
function extend(_this, obj) {
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
_this[i] = obj[i];
}
}
}
})(window, document);