Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing repeatedly updating images to the canvas (Safari blanks out image during loading)
    primarykey
    data
    text
    <p>I'm drawing an image onto the background of a canvas. This image refreshes every second, but the canvas needs to be redrawn more often than that, every 0.5 seconds. The problem I seem to be running into is that Safari flickers when updating the canvas because it appears to lose the image data from the bgImg variable. Firefox works as expected and no flicker occurs.</p> <p>Does anyone have any clue as to how to avoid this? Am I missing something really obvious?</p> <p>To test this code, simply paste the entire code below into an html file and load with your browser. What it should do is:<br/></p> <ul> <li>create a canvas</li> <li>load an image every second</li> <li>redraw the canvas every 500ms, using the image as a background.</li> </ul> <p>Here's the code:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; var bgImg = new Image(); var firstload = false; var cv; var ctx; var updateBackground = function() { bgImg.onload = function() { firstload = true; console.log("image loaded. Dimensions are " + bgImg.width + "x" + bgImg.height); } bgImg.src = "http://mopra-ops.atnf.csiro.au/TOAD/temp.php?" + Math.random() * 10000000; setTimeout(updateBackground, 1000); } var initGraphics = function() { cv = document.getElementById("canvas"); ctx = cv.getContext("2d"); cv.width = cv.clientWidth; cv.height = cv.clientHeight; cv.top = 0; cv.left = 0; } var drawStuff = function() { console.log("Draw called. firstload = " + firstload ); ctx.clearRect(0, 0, cv.width, cv.height); if ( firstload == true) { console.log("Drawing image. Dimensions are " + bgImg.width + "x" + bgImg.height); try { ctx.drawImage(bgImg, 0, 0); } catch(err) { console.log('Oops! Something bad happened: ' + err); } } setTimeout(drawStuff, 500); } window.onload = function() { initGraphics(); setTimeout(updateBackground, 1000); setTimeout(drawStuff, 500); } &lt;/script&gt; &lt;style&gt; body { background: #000000; font-family: Verdana, Arial, sans-serif; font-size: 10px; color: #cccccc; } .maindisplay { position:absolute; top: 3%; left: 1%; height: 96%; width: 96%; text-align: left; border: 1px solid #cccccc; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="myspan" style="width: 50%"&gt; &lt;canvas id="canvas" class="maindisplay"&gt;&lt;/canvas&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&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