signal-desktop/js/views/identicon_svg_view.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-02-23 16:00:18 +00:00
// Copyright 2015-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
/* global Whisper, loadImage */
// eslint-disable-next-line func-names
(function () {
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +00:00
/*
2020-01-08 17:44:54 +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);
},
2021-02-23 16:00:18 +00:00
getDataUrl() /* : Promise<string> */ {
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'));
};
2021-02-23 16:00:18 +00:00
img.onerror = () => {
URL.revokeObjectURL(svgurl);
// If this fails for some reason, we'd rather continue on than reject.
resolve(undefined);
};
2018-04-27 21:25:04 +00:00
img.src = svgurl;
});
},
});
2016-08-30 00:06:55 +00:00
const COLORS = {
red: '#cc163d',
deep_orange: '#c73800',
brown: '#746c53',
pink: '#a23474',
purple: '#862caf',
indigo: '#5951c8',
blue: '#336ba3',
teal: '#067589',
green: '#3b7845',
light_green: '#1c8260',
blue_grey: '#895d66',
grey: '#6b6b78',
ultramarine: '#2c6bed',
2018-04-27 21:25:04 +00:00
};
})();