Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem with using ctx.clearRect(0,0,canvas.width,canvas.height) is that if you have modified the transformation matrix you likely will not be clearing the canvas properly.</p> <p>The solution? Reset the transformation matrix prior to clearing the canvas:</p> <p>// Store the current transformation matrix ctx.save();</p> <p>// Use the identity matrix while clearing the canvas ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, canvas.width, canvas.height);</p> <p>// Restore the transform ctx.restore();</p> <p><strong>Edit:</strong></p> <p>Chrome responds well to: context.clearRect ( x , y , w , h ); but IE9 seems to completely ignore this instruction. IE9 seems to respond to: canvas.width = canvas.width; but it doesn't clear lines, just shapes, pictures and other objects.</p> <p>So if you have a canvas and context created like this:</p> <p>var canvas = document.getElementById('my-canvas'); var context = canvas.getContext('2d');</p> <p>You can use a method like this:</p> <p>function clearCanvas(context, canvas) { context.clearRect(0, 0, canvas.width, canvas.height); var w = canvas.width; canvas.width = 1; canvas.width = w; }</p> <p><strong>Edit2:</strong></p> <p>use this code to check if the browser supports it:</p> <pre><code>function checkSupported() { canvas = document.getElementById('canvas'); if (canvas.getContext){ ctx = canvas.getContext('2d'); // Canvas is supported } else { // Canvas is not supported alert("We're sorry, but your browser does not support the canvas tag. Please use any web browser other than Internet Explorer."); } } </code></pre> <p>And to make this code execute when the web page loads, adjust the body tag so it reads like this:</p> <pre><code>&lt;body onload="checkSupported();"&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload