有什么办法克隆HTML5 canvas元素及其内容?

有什么方法可以创建包含所有绘制内容的canvas元素的深层副本?



慕侠2389804
浏览 2104回答 3
3回答

拉风的咖菲猫

实际上,复制画布数据的正确方法是将旧画布传递到新的空白画布。试试这个功能。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用于像素数据访问,而不用于复制画布。使用它进行复制非常缓慢,并且很难在浏览器上进行。应该避免。
打开App,查看更多内容
随时随地看视频慕课网APP