Skip to content

Commit

Permalink
Replace the canvas usage with svg when rendering initials
Browse files Browse the repository at this point in the history
  • Loading branch information
Borislav Kulov committed Feb 9, 2018
1 parent c751542 commit b8979c6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
8 changes: 6 additions & 2 deletions src/avatar/avatar.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<div class="igx-avatar igx-avatar--{{size}}" aria-label="avatar" role="img">
<img #image *ngIf="src || initials" src="{{src}}" class="igx-avatar--image igx-avatar--{{size}}" [class.igx-avatar--rounded]="isRounded"
[style.backgroundColor]="bgColor" [attr.aria-roledescription]="roleDescription" />
<img #image *ngIf="src" src="{{src}}" class="igx-avatar--image igx-avatar--{{size}}" [class.igx-avatar--rounded]="isRounded"
[style.backgroundColor]="bgColor" [attr.aria-roledescription]="roleDescription" />
<svg #initialsImage *ngIf="initials && !src" xmlns="http://www.w3.org/2000/svg" [class.igx-avatar--rounded]="isRounded"
[style.backgroundColor]="bgColor" [attr.aria-roledescription]="roleDescription">
<text text-anchor="middle"/>
</svg>
<span *ngIf="!src && !initials" class="igx-avatar--{{size}} igx-avatar--icon" [class.igx-avatar--rounded]="isRounded" [style.backgroundColor]="bgColor"
[style.color]="color" [attr.aria-roledescription]="roleDescription">
<igx-icon fontSet="material" [name]="icon"></igx-icon>
Expand Down
49 changes: 24 additions & 25 deletions src/avatar/avatar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export enum Size {
templateUrl: "avatar.component.html"
})
export class IgxAvatarComponent implements AfterViewInit, AfterContentChecked {
@ViewChild("image") public image: ElementRef;
@ViewChild("image") public image: ElementRef;
@ViewChild("initialsImage") public initialsImage: ElementRef;
@Input() public initials: string;
@Input() public src: string;
@Input("roundShape") public roundShape = "false";
Expand Down Expand Up @@ -86,11 +87,12 @@ export class IgxAvatarComponent implements AfterViewInit, AfterContentChecked {
}

public ngAfterViewInit() {
if (this.initials && this.image) {
const src = this.generateInitials(
parseInt(this.image.nativeElement.width, 10)
);
this.image.nativeElement.src = src;
if (this.initials && this.initialsImage) {
// it seems the svg element is not yet fully initialized so give it some time
setTimeout(()=>{
var size = parseInt(this.initialsImage.nativeElement.width.baseVal.value, 10);
this.generateInitials(size);
}, 50);
}
}

Expand All @@ -109,26 +111,23 @@ export class IgxAvatarComponent implements AfterViewInit, AfterContentChecked {
}

private generateInitials(size) {
const canvas = document.createElement("canvas");
const fontSize = size / 2;
let ctx;

canvas.width = size;
canvas.height = size;

ctx = canvas.getContext("2d");
ctx.fillStyle = this.bgColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.textAlign = "center";
ctx.fillStyle = this.color;
ctx.font = fontSize + `px ${this.fontName}`;
ctx.fillText(
this.initials.toUpperCase(),
size / 2,
size - size / 2 + fontSize / 3
);

return canvas.toDataURL("image/png");

var svg = this.initialsImage.nativeElement;
svg.setAttribute('width', size);
svg.setAttribute('height', size);

var svgText = svg.children[0];
if (svgText) {
svgText.textContent = this.initials.toUpperCase();
svgText.setAttribute('font-size', fontSize);
svgText.setAttribute('font-family', this.fontName);

let x = fontSize;
svgText.setAttribute('x', x);
var y = size - size / 2 + fontSize / 3;
svgText.setAttribute('y', y);
}
}

private _addEventListeners(renderer: Renderer2) { }
Expand Down

0 comments on commit b8979c6

Please sign in to comment.