Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  9] [ 2]  / answers: 1 / hits: 62841  / 14 Years ago, fri, july 23, 2010, 12:00:00

Is there any way to create a deep copy of a canvas element with all drawn content?


More From » html

 Answers
77

Actually the correct way to copy the canvas data is to pass the old canvas to the new blank canvas. Try this function.



function cloneCanvas(oldCanvas) {

//create a new canvas
var newCanvas = document.createElement('canvas');
var context = newCanvas.getContext('2d');

//set dimensions
newCanvas.width = oldCanvas.width;
newCanvas.height = oldCanvas.height;

//apply the old canvas to the new one
context.drawImage(oldCanvas, 0, 0);

//return the new canvas
return newCanvas;
}


Using getImageData is for pixel data access, not for copying canvases. Copying with it is very slow and hard on the browser. It should be avoided.


[#96132] Tuesday, July 20, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isham

Total Points: 69
Total Questions: 86
Total Answers: 86

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
;