2

I have an image i am getting from a url. I want to display this image in a canvas but when it gives me error image not found but my image is present in a folder and I have given its path correclty. Tell me why it is saying image not found?

I have this in Html:

 <canvas #canvas></canvas>

Here is my component Code:

export class drawingTestFormComponent{
@Input() public width = 495;
@Input() public height = 445;

private cx: CanvasRenderingContext2D;

public ngAfterViewInit() {
    const canvasEl: HTMLCanvasElement = this.canvas.nativeElement;
    this.cx = canvasEl.getContext('2d')!;
    let image = new Image();


    canvasEl.width = this.width;
    canvasEl.height = this.height;

    this.cx.lineWidth = 3;
    this.cx.lineCap = 'round';
    this.cx.strokeStyle = '#000';
    image.onload = ()=> {
        this.cx.drawImage(image, 0, 0, this.width, this.height);
    }
    image.src = "../../../../wwwroot/dist/assets/blackBoards/NCAA_mhalfcourt_500x410.png";

}

}

1 Answer 1

2

Your code looks good the only issue I have found is that you have not defined the canvas variable.

@ViewChild("canvas") canvas;

WORKING DEMO

Sign up to request clarification or add additional context in comments.

4 Comments

I have defined canvas variable i-e { @ViewChild('canvas') public canvas: ElementRef } but this is not working in my case. And also this demo is not working as well.
@MuhammadNaeemAkhtar : I just verified demo is working.please verify again
I change the image source now its working fine. Thanks
Link broken: "ikes! The page you requested couldn't be found." :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.