JavaScript rounding to 1 decimal place on inline style attribute -


I have a function that is generating random RGBA colors and if I log in to the console then it's decimal Rotates at the place (what I want to do), but if you open the console and view it as an inline style feature on the body element, can you see that it is moving in a decimal place? where is the problem?

  setInterval (function fu () {document.body.style.backgroundColor = ("RGBA (+ + Mathrade). () * 255) +" , "+ Math.round (Math.random (* * 255) +", "+ Math.round (Math.random (* * 255) +", + + Math.random (). Tofix (1)) + " ) ";}, 1000);  

JS

  setInterval (function foo () {console.log (("rgba ("+ Math.round (Math.random (* * 255) +", "+ Math.round (Math.random (* * 255) +", + Math.round (Math.random (* * 255) + "," + Math.random (. ToFixed (1)) + ")");}, 1000);   

If you compare these two fiddles then you see the difference Why are they different? It is a similar task and it should be equal, right?

Browser internally stores alpha values ​​as 1 byte, not in the form of a float. For example When you say that you need 0.5 alpha, then the browser converts it to 1 byte integer using:

  Math. Floor (255 * 0.5)   

When you say the result back, it will change the integer by dividing it by 0 to 0.

This will be the wrong alpha I tested it using: Color by RGBA (134, 78, 73,0,5) // javascript RGBA (134, 78, 73, 0.498039) Re>

0.498039 = Math.floor (255 * 0.5) / 255; (with some rounding at all)

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -