I am making an app, where I'm showing the image on the canvas, But the image src is coming from the parent page.
Everything is working file, but when I'm changing the imageSrc on button click, It's showing a blank canvas. After that When I'm clicking outside of the div new image showed fine.
preview.page.html
<ion-content no-padding>
<img #img src="{{imgSrc}}" style='display: none;' />
<canvas #imageCanvas (touchstart)="startDrawing($event)"></canvas>
</ion-content>
and preview.page.ts
export class PreviewBoxPage implements OnInit, AfterViewInit, AfterViewChecked {
@ViewChild('previewImg', {static: false}) waterMarkImage: ElementRef;
@ViewChild('imageCanvas', {static: false}) canvas: ElementRef;
@ViewChild('img', {static: false}) img: ElementRef;
@ViewChild(Platform , {static: false}) content: Platform;
canvasElement: HTMLCanvasElement;
private element: HTMLImageElement;
private ctx: CanvasRenderingContext2D;
@Input()
imgSrc;
ngAfterViewInit() {
this.canvasElement = this.canvas.nativeElement;
this.element = this.img.nativeElement;
this.canvasElement.width = this.plt.width();
this.canvasElement.height = 270;
this.ctx = this.canvasElement.getContext('2d');
}
ngAfterViewChecked() {
this.ctx.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height);
this.ctx.drawImage(this.element, 0, 0);
}}
Someone, please explain to me this. It's been a week, I couldn't find a solution.