mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-15 11:40:29 +03:00
Create icons for users without an avatar in desktop notifications (#305)
* Add notifications icon for users without an avatar * Render icon at higher resolution * Use scale to render at higher resolution
This commit is contained in:
parent
fe674ef2ea
commit
8d3f0a9f4d
5 changed files with 88 additions and 15 deletions
|
|
@ -8,6 +8,7 @@ import Text from '../text/Text';
|
|||
import RawIcon from '../system-icons/RawIcon';
|
||||
|
||||
import ImageBrokenSVG from '../../../../public/res/svg/image-broken.svg';
|
||||
import { avatarInitials } from '../../../util/common';
|
||||
|
||||
function Avatar({
|
||||
text, bgColor, iconSrc, iconColor, imageSrc, size,
|
||||
|
|
@ -40,7 +41,7 @@ function Avatar({
|
|||
? <RawIcon size={size} src={iconSrc} color={iconColor} />
|
||||
: text !== null && (
|
||||
<Text variant={textSize} primary>
|
||||
{twemojify([...text][0])}
|
||||
{twemojify(avatarInitials(text))}
|
||||
</Text>
|
||||
)
|
||||
}
|
||||
|
|
|
|||
61
src/app/atoms/avatar/render.js
Normal file
61
src/app/atoms/avatar/render.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import { avatarInitials } from '../../../util/common';
|
||||
|
||||
function cssVar(name) {
|
||||
return getComputedStyle(document.body).getPropertyValue(name);
|
||||
}
|
||||
|
||||
// renders the avatar and returns it as an URL
|
||||
export default async function renderAvatar({
|
||||
text, bgColor, imageSrc, size, borderRadius, scale,
|
||||
}) {
|
||||
try {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = size * scale;
|
||||
canvas.height = size * scale;
|
||||
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
ctx.scale(scale, scale);
|
||||
|
||||
// rounded corners
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(size, size);
|
||||
ctx.arcTo(0, size, 0, 0, borderRadius);
|
||||
ctx.arcTo(0, 0, size, 0, borderRadius);
|
||||
ctx.arcTo(size, 0, size, size, borderRadius);
|
||||
ctx.arcTo(size, size, 0, size, borderRadius);
|
||||
|
||||
if (imageSrc) {
|
||||
// clip corners of image
|
||||
ctx.closePath();
|
||||
ctx.clip();
|
||||
|
||||
const img = new Image();
|
||||
img.crossOrigin = 'anonymous';
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
img.onerror = reject;
|
||||
img.onload = resolve;
|
||||
});
|
||||
img.src = imageSrc;
|
||||
await promise;
|
||||
|
||||
ctx.drawImage(img, 0, 0, size, size);
|
||||
} else {
|
||||
// colored background
|
||||
ctx.fillStyle = cssVar(bgColor);
|
||||
ctx.fill();
|
||||
|
||||
// centered letter
|
||||
ctx.fillStyle = '#fff';
|
||||
ctx.font = `${cssVar('--fs-s1')} ${cssVar('--font-primary')}`;
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.fillText(avatarInitials(text), size / 2, size / 2);
|
||||
}
|
||||
|
||||
return canvas.toDataURL();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return imageSrc;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue