Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Observer canvas background src dinamically generated

when first-time load canvas, background.src is empty. In dialog (dialog is in the second component) I upload IMG but after the success, background.src is still empty, I need to refresh app to get background.src = this.src;.

How to set observable to change background.src after upload to call again this.src = this.srcService.getImgURL(this.id) // returned string 'https://something.com/image/1234', because canvas is already loaded?

Or maybe something else instead of observable?

I tried to set timeout with 1 second on background src and this is working, but setTimeout is not an option to use in our app.

Can not use background.onload = () => {...} because I need src before this.

Thnx

First component

export class FirstComponent implements AfterViewInit {
  @ViewChild('canvas', { static: true }) canvas: ElementRef<HTMLCanvasElement>;

  ngAfterViewInit() {
  this.src = this.srcService.getImgURL(this.id) // returned string 'https://something.com/image/1234'
  const background = new Image();
  background.src = this.src;
}
}

Second component The second component is a dialog where I choose and upload the image

 export class SecondComponent implements {
       constructor( @Inject(MAT_DIALOG_DATA) public data: IInterface,
                    public dialogRef: MatDialogRef<SettingsComponent>) 
    ( ... )
      close() {
         this.dialogRef.close(null);
       }
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After the image loads, you should set the background to the canvas as well using the 2d context:

ngAfterViewInit() {
  this.src = this.srcService.getImgURL(this.id);
  const background = new Image();
  background.src = this.src;
  background.onload = () => {
    const ctx = this.canvas.getContext("2d");
    ctx.drawImage(background, 0, 0);
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!