JSFiddle - React, Tailwind, and code Playground

by hustlerinc

HTML

<!DOCTYPE HTML>
<html>
<body onload="init();">
<div id="gameArea">
<canvas id="viewport" width="800" height="400"></canvas>
</div>
</body>
</html>

CSS

html,body{width:100%;height:100%;margin:0px;}

#gameArea{width:800px;height:400px;margin:0px auto;}
#viewport{border:1px solid #000;margin:5px 0px 0px;}

JavaScript

var viewport = document.getElementById('viewport');
var ctx = viewport.getContext('2d');
console.log(ctx);
var fps = 30;

function init(){
    setInterval(update, 1000 / fps);
}

function Player(){
    this.width = 10;
    this.height = 50;
    this.X = 10;
    this.Y = 10;
    this.draw = function() {
        ctx.fillStyle = '#0000ff';
        ctx.fillRect(this.X, this.Y, this.width, this.height);
    };
}
var player = new Player();

function Ball(){
    this.radius = 5;
    this.Y = 20;
    this.X = 25;
    this.draw = function() {
        ctx.arc(this.X, this.Y, this.radius, 0, Math.PI*2, true);
        ctx.fillStyle = '#00ff00';
        ctx.fill();
    };
}

var ball = new Ball();

function draw(){
    player.draw();
    ball.draw();
}

function update(){
    viewport.width = viewport.width;
    draw();
    ball.X++;
}