i am new to angular 2 and i work on image cropping plugin and i want to display the image on canvas. here my html code
<div class="row">
<div class="col-sm-6">
<canvas id="layout" width="400" height="300">
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
and here is my typescript file
import { Component, OnInit, ViewChild } from '@angular/core';
@Component({
selector: 'app-four',
templateUrl: './four.component.html',
styleUrls: ['./four.component.css']
})
export class FourComponent implements OnInit {
image: HTMLImageElement;
profPic: any;
context: CanvasRenderingContext2D;
constructor() { }
@ViewChild('layout') canvas;
@ViewChild('photo') photo;
ngOnInit() {
const _canvas = this.canvas.nativeElement;
//let photo = this.photo.nativeElement;
this.context = (<HTMLCanvasElement>_canvas).getContext('2d');
this.image = new Image();
this.image.src = '../../assets/images/1.jpg';
this.context.drawImage(this.image,20,20,500,260);
}
}
and i have error
ERROR TypeError: Cannot read property 'nativeElement' of undefined
please help me to fix this
thank you
Make layout local template variable
<canvas #layout id="layout" width="400" height="300">
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
WORKING DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With