Friday, November 24, 2006

Controling Opacity

I have found this very helpful in doing "flash-like" java scripts driven by user input. Takes 1 minute to create transition effect lib with this function.

Enjoy!


P.S.: Oh!
The only thing left for you to implement isIE()!!!!!



function setObjectOpacity(obj, opacity)
{
//IE
if (isIE())
obj.style.filter = "alpha(opacity:"+opacity+")";
else
{
opacity = (opacity == 100)?99.999:opacity;//99.9 for Firefox flicker bug

// Konqueror, Safari
obj.style.KHTMLOpacity = opacity / 100;
// Old Mozilla and Firefox
obj.style.MozOpacity = opacity / 100;
// CSS3, Safari , new Firefox (Gecko)
obj.style.opacity = opacity / 100;
}//else
}

Java Script multi-threading

We all know that it is not possible,...but... using timers along with path-through routines that don't block might sound like a cool alternative. I have tried it and it works - picture this:

function doLongTask(x,y,z, callback)
{
this.X = x;
this.Y = y;
this.Z = z;
this.doneCallback=callback;
}

doLongTask.prototype.threadSpin = _longtask_threadspin;

function _longtask_threadspin()
{
//instead of long loops save your intermediate state in this.{vars}

this.X++;
if (this.X%100==0) this.Z = this.Y - 100; //some logic that would have been in a tight loop

if (this.X>1000) this.doneCallback();

}


And now we can easily:

var task = new doLongTask(1,2,3,doneLongTask);
var tmr = setInterval(task.threadSpin, 10/*time slice!!!*/);

function doneLongTask()
{
task.Z // result of task
clearInterval(tmr);// kill 'thread'
}