Recently I started working with Angular and encountered an issue while trying to access a variable inside a function. Below is the code snippet that's causing me trouble:
mergeImages() {
var imgurl;
var canvas: HTMLCanvasElement = this.canvas.nativeElement;
var context = canvas.getContext('2d');
let img1 = new Image();
let img2 = new Image();
img1.onload = function() {
canvas.width = img1.width;
canvas.height = img1.height;
context.globalAlpha = 1.0;
img2.src = '../assets/sun.jpg';
};
img2.onload = function() {
context.globalAlpha = 1;
context.drawImage(img1, 0, 0);
context.globalAlpha = 0.5; //Remove if pngs have alpha
context.drawImage(img2, 0, 0);
imgurl = canvas.toDataURL("image/jpg");
//console.log(imgurl)
};
img1.src = '../assets/moon.jpg';
}
Now I'm looking for a way to access the "imgurl" from another method.
printvalue(){
// Need to access imgurl here
}
Update - The problem lies in Angular not being able to find var a within the printvalue() function as it currently only works inside the function something(). Any suggestions?