JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<canvas>Your browser does not support canvas. Update your browser.</canvas>
</body>
JavaScript
// Check if canvas exists and get the context of the canvas
var canvas = document.getElementsByTagName('canvas')[0];
var ctx = canvas.getContext('2d');
// Add width and height of the canvas
var width = 640, height = 480;
// Make the canvas width and height as the variables
canvas.width = width, canvas.height = height;
rect = {
x: 0,
y: 0,
width: 100,
height: 100
}
ctx.fillStyle = 'red';
ctx.fillRect(rect.x,rect.y,rect.width,rect.height);
function animate(){
if(rect.x <= 200){
rect.x += 5;
}
requestID = requestAnimationFrame(animate);
}
requestAnimationFrame(animate);