Topic: How I Did An Austin Traffic Cam Widget...
I have been hacking (more like flailing wildly) the sample cam widget to create an Austin Tx Traffic Cam widget. While the first one I did worked on one of the tours (as TXDOT calls it) I decided to get more involved in activescript. So I added buttons on the left and right side of the screen to move through the tours and then fancied it up by displaying the title of the tours as on the TXDOT web site.
Here is the code for those interesested. As usual its a mess...
url = "http://ausits.dot.state.tx.us/snapshots/aus-cam";// link to the cam feed
delay = 1000;// one second refresh rate
_global.tourNo = 2;
_global.tour = new Array();
tour[0] = "tour!";
tour[1] = "US 183 Tour - between McNeil Road and Loop 1 (MoPac)";
tour[2] = "Loop 1 Tour - between Loop 360 North and Lake Austin Boulevard";
tour[3] = "Loop 1 Tour - between Town Lake and Barton Creek";
tour[4] = "US 290 Tour - between Banister to Congress";
tour[5] = "US 183 Tour - between Loop 1 (MoPac) and IH 35";
tour[6] = "IH 35 Tour - between 51st Street and Braker Lane";
tour[7] = "IH 35 near 6th Street";
//-----------------------------------
//
// this handles simple JPEG-based webcams
//
// set 'url' to URL of JPEG image feed
// set 'delay' to frame rate in ms (best to avoid faster than 1s)
// create library item with linkage "proxy" to hold image
//
// Note that the site hosting the cam must allow Flash access
// from arbitrary domains, typically by using a "crossdomain.xml" file
//
// This movie also counts on the images being exactly 320x240 - you may bee to get fancier
// if they're not.
//
//-----------------------------------
//
// The strategy here is to have three "proxy" movie clips that
// each load and rotate to front. If you have just one clip, then
// it will go blank during the load. Having three also allows
// some latency in the response for the image load.
//
createEmptyMovieClip("image0",0);// create some initial empty images
createEmptyMovieClip("image1",1);
createEmptyMovieClip("image2",2);
index = 0;
//
// onEnterFrame-based loading
//
// We aren't using setInterval/clearInterval to avoid
// potential memory/timer leaks in the Flash player
//
then = 0;// long, long ago....
this.onEnterFrame = function() {
now = (new Date()).getTime();// get current time in ms from the epoch
if (now-then>delay) {// enough time elapsed since last image loaded?
doLoadImage();// yup, load image
then = now;// and reset the last load time
}
};
//
// rotate the images, load the next one
//
function doLoadImage() {
var name0 = 'image'+(index%3);
var name1 = 'image'+((index+1)%3);
var name2 = 'image'+((index+2)%3);
var m0 = eval(name0);
var m1 = eval(name1);
var m2 = eval(name2);
_level0.infoField._visible = true;
_level0.infoField.text = _global.tourNo+" "+_global.tour[_global.tourNo];
m0.swapDepths(0);// rotate the clips
m1.swapDepths(1);
m2.removeMovieClip();// throw away the oldest one
createEmptyMovieClip(name2,2);// make a new one
m2 = eval(name2);
m2.loadMovie(url+"0"+_global.tourNo+".jpg"+"?"+Math.random());// load image into it
index = (index+1)%3;
}