I am trying to smooth the animation for a circular progress bar i have made using HTML canvas arc and javascript.
The progress bar is drawn using data pulled from an XML sheet (semic.cgx) which is updated dynamically by a PLC.
My idea is to check the XML sheet every 100ms for an update and between this add 1/10th of an increment every 10ms to the variable that controls the progress. Turning 1 increment into 10.
Ive not yet been able to achieve my desired affect. The below code has issues once the upper limit of progress bar has been reached.
I know there must be other ways of doing this as i have seen a lot of examples online of smooth animations but most contain no information on how the code is actually working and my knowledge is basic. I would appreciate any help.
var req
function reloadData() {
url = 'semic.cgx'
try
{
req = new XMLHttpRequest();
}
catch (e)
{
alert('No AJAX Support');
return;
}
req.onreadystatechange = myFunction;
req.open('GET', url, true);
req.send(null);
}
function myFunction() {
if (req.readyState == 4)
{
if (req.status == 200)
{
var x = req.responseXML;
var v1 = x.getElementsByTagName('text')[0].childNodes[1].innerHTML;
var angle = (0.75 +((v1 / 100)* 1.5));
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
setInterval(function () {
if (angle >= 2.25) {
ctx.clearRect(0,0,500,500);
}
if (angle < 2.25) {
angle = angle + 0.0015;
ctx.globalCompositeOperation = "source-over";
ctx.rotate(0.5*2*Math.PI);
ctx.lineWidth = 15;
ctx.imageSmoothingEnabled = true;
ctx.imageSmoothingQuality = "high";
ctx.beginPath();
ctx.arc(250, 250, 200, (0.75 * Math.PI), (angle * Math.PI));
ctx.strokeStyle = "#DE2700";
ctx.stroke();
}
console.log(angle);
}, 10);
timeoutID = setTimeout('reloadData()', 100);
}
}
}