PHP前端开发

如何清除HTML5画布上的图表,以防止触发悬停事件?

百变鹏仔 3个月前 (09-22) #HTML
文章标签 画布

要清除画布上的图表,请删除元素,然后将一个新的元素附加到父容器中,如下所示的代码:

var resetCanvas = function(){   $(&#39;#results-graph&#39;).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);};