var ctx = document.querySelector("canvas").getContext("2d"),
x = 3.1, dx = 0.1;
ctx.imageSmoothingEnabled = ctx.mozImageSmoothingEnabled = ctx.webkitImageSmoothingEnabled = false;
ctx.font = "14px sans-serif";
(function loop() {
ctx.fillStyle = "#000";
ctx.clearRect(0,0,350,50);
ctx.fillRect(0,0, x, 1); // forces pixel-alignment for demo
ctx.drawImage(ctx.canvas, 0,0,4,1, 0, 0, 200, 50);
x += dx;
if (x <= 3 || x >= 4) dx = -dx;
info(x);
document.querySelector("div").innerHTML = "length: " + x.toFixed(2);
setTimeout(loop, 160)
})();
function info(x) {
ctx.fillStyle = "#f00";
ctx.fillText("Perceived length", 210, 19);
ctx.fillRect(0, 15, x * 50, 2);
ctx.fillRect(x*50-1, 12, 2, 7);
x = x === 3 ? 3 : 4;
ctx.fillText("Actual length", 210, 40);
ctx.fillRect(0, 35, x * 50, 2);
ctx.fillRect(x*50-1, 32, 2, 7);
}
div {font:bold 14px sans-serif}
<canvas width=500 height=50></canvas><br>
<div></div>