Clear Chart from HTML5 Canvas to Disable Hover Events



To clear a chart from canvas, delete the <canvas> element and then append a new <canvas> element to the parent container as in the below-given code −

var resetCanvas = function(){
   $('#results-graph').remove();  
   $('#graph-container').append('<canvas id = "results-graph"><canvas>');
   canvas = document.querySelector('#results-graph');

   ct = canvas.getContext('2d');
   ct.canvas.width = $('#graph').width(); // here we are resizing the new canvas element to parent width
   ct.canvas.height = $('#graph').height(); // here we are resizing the new canvas element to parent height
   var a = canvas.width/2;
   var b = canvas.height/2;

   ct.font = '12pt Calibri';
   ct.textAlign = 'Align Left';
   ct.fillText('left aligned text on the canvas', a, b);
};
Updated on: 2020-06-24T13:45:44+05:30

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements