JSFiddle - React, Tailwind, and code Playground
HTML
<canvas width="200" height="200" id="canvas1">
JavaScript
var ctx = document.getElementById('canvas1').getContext('2d');
ctx.strokeStyle = 'black';
ctx.lineWidth = .25;
var right = true;
var x = 20;
function go()
{
ctx.clearRect(0, 0, 500, 500);
x += right? +0.1:-0.1;
if(right && x == 100)
right = false;
else if(!right && x == 20)
right = true;
ctx.strokeRect(x, 5.5, 10, 20);
requestAnimationFrame(go);
}
go();