I've a question about my code, where I'm trying to create a dynamic background using the webcam, which with help of .jquery gives a lightIntensity in a number 0-255, which I would like to use for the background, I am not able to get it to work.
$(document).ready(function() {
$.fn.webcamLightSensor({ refreshInterval: 100 }, function(lightIntensity) {
$('.container p').text(lightIntensity);
console.log(lightIntensity)
});
function decimalToHex(lightIntensity) {
var hex = Number(lightIntensity).toString(16);
hex = "000000".substr(0, 6 - hex.length) + hex;
return hex;
document.body.style.backgroundColor = hex;
}
});
'lightIntenisty' is a number between 0 and 255, which needs to be translated to a RGB or hex (I think).
Code uses jQuery library's:
- jquery-1.9.1.js
- jquery.image-brightness.js
- jquery.webcam-light-sensor.js
<script src="https://github.com/ErwynIzaaekJoziasse/js/blob/master/jquery.webcam-light-sensor.js"></script>
<script src="https://github.com/ErwynIzaaekJoziasse/js/blob/master/jquery.image-brightness.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Light -> colour</title>
<script src="js/jquery-1.9.1.js" ></script>
<script src="js/jquery.image-brightness.js" ></script>
<script src="js/jquery.webcam-light-sensor.js" ></script>
</head>
<body>
<div>
<div class="container">
<!-- in between the <p> the 'amount' of light is displayed (0/255) -->
<p></p>
</div>
</div>
<script>
$(document).ready(function() {
$.fn.webcamLightSensor({ refreshInterval: 100 }, function(lightIntensity) {
$('.container p').text(lightIntensity);
console.log(lightIntensity)
});
function decimalToHex(lightIntensity) {
var hex = Number(lightIntensity).toString(16);
hex = "000000".substr(0, 6 - hex.length) + hex;
document.body.style.backgroundColor = '#' + hex;
return hex;
}
});
</script>
</body>
</html>