mcottondesign

Loving Open-Souce One Anonymous Function at a Time.

JavaScript Journal - Updating an image without flickering

This is how I'm approaching the problem of trying to simulate live video from a series of preview images.

  • After initial page setup, we start the polling service
  • After each polling response we look for new preview images ('pre')
  • For each preview response, we ask each camera to update its image
  • As an optimization we only update the cameras that are currently on screen
  • When the new image has loaded, we swap it with the one being displayed (this eliminates flickering)
  • If the image has taken longer than two seconds to load than display a 'Loading…' message
  • Update the time display in the corner with the timestamp of the image

I am made an updateImage method on my Cameras model. When the new image is loaded into a dummy space, I copy it to the actual model's image and the view re-renders.

To eliminate the flickering, bind to the load event on the new image and wait unti it is fired before changing the src on the image.

I also added some convenience methods to the Collection. I filter down the models based and return it based on status. As long as I return an array of models everything else works.