I need to reproduce an animation-like effect on the browser (mobile and desktop), this effect is just a procedural changes on the background color from black to white.
The timming is important, since a black background for a short time means "0" and a longer time means "1" (is like a binary Morse code). This information is interpreted by a light sensor in front of the screen. The sensor is very fast, and if we use a lower frequency we wont have problems with the screen (assuming a maximum refresh rate of 50Hz).
This is the script im using:
var i = 0;
var b0t = 100;
var b1t = 3.5 * b0t;
var wl = 100;
function pulses (){
bytes = "1001101010110101001000100";
bit = bytes[i];
i += 1;
document.body.style.backgroundColor = 'white';
document.body.style.color = 'white';
setTimeout(toblack, wl);
if(i <= bytes.length) {
if (bit == 1)
setTimeout(pulses, b1t + wl);
else
setTimeout(pulses, b0t + wl);
}
}
function toblack() {
document.body.style.backgroundColor = 'black';
document.body.style.color = 'black';
}
pulses ();
And a working demo (Do not open if you suffer any epileptic syndrome)
The problem is, 1 out of 20 the browser mess the timing, of course in the sensor side i have a tolerance range, a very tolerant range, but again the browser some times is out by several milliseconds, and this is no surprise, if your computer of cellphone is doing something in the background the performance of the browser is severely affected.
Is there a software solution for this? I was thinking using flash to do the flickering with more precision, but i really like my app to be accessible also from IOS. Or i can make a native app that for sure will be more precise than the browser, but then again, i will like to know if i can make it work on the browser.