JSFiddle - React, Tailwind, and code Playground
HTML
<html>
<head>
<title></title>
</head>
<body>
</body>
JavaScript
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
canvas.width = 500;
canvas.height = 500;
document.body.appendChild(canvas);
keysdown = {}
addEventListener("keydown",function(){
keysdown[e.keycode] = true;
});
addEventListener("keyup", function(){
delete keysdown[e.keycode];
});
gamestate = {
}
vector = {
make:function(object){
return [object.x + object.width/2,object.y + object.height/2]
},
normalize:function(v){
var length = Math.sqrt(v[0] * v[0] + v[1] * v[1])
v[0] = v[0]/length
v[1] = v[1]/length
return v
},
dot:function(v1,v2){
return v1[0] * v2[0] + v1[1] * v2[1]
}
}
ball = {
width: 15,
height: 15,
speed: 200,
x: 314,
y: 347.5,
bouncey: 1,
bouncex: 1
}
paddle = {
width:15,
height:70,
speed:200
}
all_objects = [ball]
paddle1 = Object.create(paddle);
paddle1.x = 50;
paddle1.y = 50;
all_objects.push(paddle1);
paddle2 = Object.create(paddle);
paddle2.x = 300;
paddle2.y = 320;
all_objects.push(paddle2);
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function intersects(ob1,ob2){
var x1, x2, y1, y2, w1, w2, h1, h2;
x1 = ob1.x;
x2 = ob2.x;
y1 = ob1.y;
y2 = ob2.y;
w1 = ob1.width;
w2 = ob2.width;
h1 = ob1.height;
h2 = ob2.height;
//console.log(x1, x2, y1, y2, w1, w2, h1, h2);
w1 += x1;
w2 += x2;
if(w2 < x1 || w1 < x2){
return false;
}
h1 += y1;
h2 += y2;
if(h2 < y1 || h1 < y2){
return false;
}
...