要在画布上进行双缓冲,请创建第二个canvas元素并绘制到该元素。之后,使用drawImage()
方法将图像绘制到第一个画布上,
// canvas element var canvas1 = document.getElementById('canvas'); var context1 = canvas1.getContext('2d'); //缓冲画布 var canvas2 = document.createElement('canvas'); canvas2.width = 250; canvas2.height =250; var context2 = canvas2.getContext('2d'); //在画布上创建 context2.beginPath(); context2.moveTo(10,10); context2.lineTo(10,30); context2.stroke(); //渲染缓冲的画布 context1.drawImage(canvas2, 0, 0);