Home  >  Article  >  Web Front-end  >  How to clear a chart on HTML5 canvas to prevent hover events from being triggered?

How to clear a chart on HTML5 canvas to prevent hover events from being triggered?

PHPz
PHPzforward
2023-08-20 20:41:191395browse

How to clear a chart on HTML5 canvas to prevent hover events from being triggered?

To clear the chart from the canvas, delete the element and append a new element to the parent container, as shown below Code:

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

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

   ct.font = &#39;12pt Calibri&#39;;
   ct.textAlign = &#39;Align Left&#39;;
   ct.fillText(&#39;left aligned text on the canvas&#39;, a, b);
};

The above is the detailed content of How to clear a chart on HTML5 canvas to prevent hover events from being triggered?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete