You can draw one rectangle over another.
HTML:
<svg id="mask-layer" width="200" height="50" >
<rect id="masked" width="200" height="50" style="fill: blue"/>
<rect id='mask' width="0" height="50" style="fill: red" />
</svg>
<br />
<span id='progress'></span>
JavaScript:
(function() {
var mask = document.getElementById('mask');
var progressSpan = document.getElementById('progress');
var fullWidth = 200;
var curWidth = mask.getAttribute('width');
var calculateProgress = function() {
var progress = (curWidth*100)/fullWidth;
progressSpan.innerHTML = ['Progress: ', Math.floor(progress), '%'].join('');
}
var interval = setInterval(function() {
mask.setAttribute('width', ++curWidth);
calculateProgress();
if(curWidth > fullWidth) {
clearInterval(interval);
progressSpan.innerHTML = 'Finished!';
}
}, 50);
})();
Here you have a working example: https://jsfiddle.net/L1vy2t3o/
source
share