Does HTML5 Canvas support Double Buffering?


For double buffering on the canvas, create a 2nd canvas element and draw to it. After that draw the image to the first canvas using the drawImage() method,

// canvas element
var canvas1 = document.getElementById('canvas');
var context1 = canvas1.getContext('2d');

// buffer canvas
var canvas2 = document.createElement('canvas');
canvas2.width = 250;
canvas2.height =250;
var context2 = canvas2.getContext('2d');

// create on the canvas
context2.beginPath();
context2.moveTo(10,10);
context2.lineTo(10,30);
context2.stroke();

//render the buffered canvas
context1.drawImage(canvas2, 0, 0);

Updated on: 24-Jan-2020

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements