I've been working on a little program that generates a finished image out of several layers. I need it to all be on one canvas so that I can save the final product as a single image. However, when I run my code, it draws the layers in a random order. If someone could please explain to me how to get the layers to render in order, I would really appreciate it!
function addimage(image, layer) {
var canvas = document.getElementById('paper'),
context = canvas.getContext('2d');
var img = new Image();
img.src = "Images/" + image;
img.onload = function(){
if (image == "Khundii_Agouti_Basic.png"){
context.globalCompositeOperation = "multiply";
} else if (image == "Khundii_Agouti_Cinnamon.png"){
context.globalCompositeOperation = "overlay";
} else if (layer == "pattern1"){
context.globalCompositeOperation = "overlay";
} else {
context.globalCompositeOperation = "source-over";
}
context.drawImage(img, 0, 0, 500, 500);
}
}
Above is the code that adds the image to the canvas
addimage(baseImage);
if (pattern1 == true)
addimage(pattern1Image, "pattern1");
if (pattern2 == true)
addimage(pattern2Image);
if (tint1 == true)
addimage(tint1Image);
if (tint2 == true)
addimage(tint2Image);
if (agouti == true)
addimage(agoutiImage);
addimage(maneImage);
addimage(eyeImage);
addimage("Khundii_Line_Black.png");
And here is where it is called.
All of the code can be found here if you want to see more.