时间:2010-07-23 13:31:42

标签: javascript html5 canvas clone

2 个答案:

答案 0 :(得分:102)

实际上,复制画布数据的正确方法是将旧画布传递给新的空白画布。试试这个功能。

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;
}

使用getImageData用于像素数据访问,而不是用于复制画布。在浏览器上复制它非常慢且很难。应该避免。

答案 1 :(得分:11)