Give both images time to load:
var loadCount;
var image1,image2;
function compare(){
// variable to count how many images have fully loaded
loadCount=0;
// convert canvas1 to an image
// call startComparing when done
image1=new Image();
image1.onload=startComparing;
image1.src=canvas1.toDataURL();
// convert canvas2 to an image
// call startComparing when done
image2=new Image();
image2.onload=startComparing;
image2.src=canvas2.toDataURL();
}
function startComparing(image1,image2){
// do the comparison only if we've loaded both images (loadCount==2)
if(++loadCount==2);
resemble(image1).compareTo(image2).onComplete(function(data){
alert(data);
});
}
}