Sometimes you need to clone exisitng canvas elements to the new place, keeping its content. Solution for this problem is simple, but for IE additional workaround should be implemented, as always:)
/**
* Copy canvas element image content.
* @param oldCanvas old canvas element
* @param newCanvas new canvas element
* @param document current window document
*/
copyCanvas: function (oldCanvas, newCanvas, document) {
if (oldCanvas) {
try {
// For IE browser replace canvas element with img with
// source of old canvas, as drawImage causes
// TypeMismatchError
var agent = window.navigator.userAgent;
// Check for both, IE11 and older versions
if ((agent.indexOf('MSIE ') > -1) || (agent.indexOf('Trident/') > -1)) {
var oldImage = oldCanvas.toDataURL();
var img = document.createElement("img");
img.src = oldImage;
$(newCanvas).replaceWith(img);
} else {
var context = newCanvas.getContext('2d');
context.drawImage(oldCanvas, 0, 0);
}
} catch (e) {
console.log(e.name);
}
}
}