Created
May 27, 2017 08:38
-
-
Save agrublev/465c46aceadcfdda7176052f81435865 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//I set to the window event to make sure it stays available. This is handy in many applications that may need to wait. | |
window.image1 = document.getElementById('img1'); | |
window.image2 = document.getElementById('img2'); | |
window.canv1 = document.getElementById('canv1'); | |
window.canv2 = document.getElementById('canv2'); | |
window.ctx1 = window.canv1.getContext('2d'); | |
window.ctx2 = window.canv2.getContext('2d'); | |
window.imagesLoaded = 0; | |
//load the images | |
window.image1.onload = function(){ | |
window.imagesLoaded++; | |
} | |
window.image2.onload = function(){ | |
window.imagesLoaded++; | |
} | |
//function to handle them all loading | |
function loaded(){ | |
if(window.imagesLoaded==2){ | |
//draw initial images | |
ctx1.drawImage(image1,0,0); | |
ctx2.drawImage(image2,0,0); | |
//at this point you should have 2 canvas's with 2 different images, | |
//now as I understand it you want to save them both to one canvas. | |
//So first grab a new canvas element. | |
var newImg1 = window.canv1.toDataURL('image/png'); | |
var newImg2 = window.canv2.toDataURL('image/png'); | |
window.canv3 = document.getElementById('canv3'); | |
window.ctx3 = window.canv3.getContext('2d'); | |
//this is the tricky part, side by side, | |
//overlapping or top/bottom by manipulating the x,y. | |
//I just make a general example here. | |
ctx3.drawImage(newImg1,0,0); | |
ctx3.drawImage(newImg2,10,10); | |
//finally create the new image as an image, | |
//you can do whatever you need with this | |
var newImg3 = window.canv3.toDataURL('image/png'); | |
}else{ | |
//set a timeout to retry this function if the images were not ready | |
setTimeout(function(){ | |
loaded(); | |
},100); | |
} | |
} | |
loaded(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment