signal-desktop/js/views/identicon_svg_view.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

/* global Whisper, loadImage */
// eslint-disable-next-line func-names
2018-04-27 21:25:04 +00:00
(function() {
'use strict';
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +00:00
/*
* Render an avatar identicon to an svg for use in a notification.
*/
2018-04-27 21:25:04 +00:00
Whisper.IdenticonSVGView = Whisper.View.extend({
templateName: 'identicon-svg',
initialize(options) {
2018-04-27 21:25:04 +00:00
this.render_attributes = options;
this.render_attributes.color = COLORS[this.render_attributes.color];
},
getSVGUrl() {
const html = this.render().$el.html();
const svg = new Blob([html], { type: 'image/svg+xml;charset=utf-8' });
2018-04-27 21:25:04 +00:00
return URL.createObjectURL(svg);
},
getDataUrl() {
const svgurl = this.getSVGUrl();
return new Promise(resolve => {
const img = document.createElement('img');
img.onload = () => {
const canvas = loadImage.scale(img, {
2018-04-27 21:25:04 +00:00
canvas: true,
maxWidth: 100,
maxHeight: 100,
});
const ctx = canvas.getContext('2d');
2018-04-27 21:25:04 +00:00
ctx.drawImage(img, 0, 0);
URL.revokeObjectURL(svgurl);
resolve(canvas.toDataURL('image/png'));
};
2018-04-27 21:25:04 +00:00
img.src = svgurl;
});
},
});
2016-08-30 00:06:55 +00:00
const COLORS = {
2018-04-27 21:25:04 +00:00
red: '#EF5350',
pink: '#EC407A',
purple: '#AB47BC',
deep_purple: '#7E57C2',
indigo: '#5C6BC0',
blue: '#2196F3',
light_blue: '#03A9F4',
cyan: '#00BCD4',
teal: '#009688',
green: '#4CAF50',
light_green: '#7CB342',
orange: '#FF9800',
deep_orange: '#FF5722',
amber: '#FFB300',
blue_grey: '#607D8B',
};
})();