JSFiddle - React, Tailwind, and code Playground
CSS
canvas{
width: 100%;
height: 300px;
}
#content {
position: absolute;
left: 0;
top: 0;
width: 500px;
height: 300px;
border: 1px red solid;
}
#endinfo {
position: absolute;
width: 40%;
height: 20px;
left: 0;
top: 150px;
}
#gameinfo {
position: absolute;
width: 15%;
height: 40px;
left: 85%;
top: 250px;
}
#center {
position: relative;
width: 120px;
left: 190px;
top: 133px;
}
JavaScript
/**
* Created by mati on 2015-01-18.
*/
//create canvas
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
//create image
var image = new Image();
//global variables
var Cnv = {
w: canvas.width,
h: canvas.height,
};
var Hero = {
h: 50,
w: 0.2 * canvas.width
};
var Block = {
h: 0.25 * Cnv.h,
w: 10,
//p - position
p: 9999,
y: 0
};
var Pos = {
x: Cnv.w / 2 - Hero.w,
y: Cnv.h - Hero.h,
destination: 0
};
//0 - in air, 1 - on floor, 2 - upside-down
var status;
//(angle / 180) -> deegrees
var angle = 0;
//game statistic
// !unused lvl
var lvl = 0;
var score = 0;
//click arrows event handler
var arrows = function () {
'use strict';
$(document).keydown(function (key) {
if (status != 0) {
//arrow up
if (key.which === 38 && Pos.y === Cnv.h - Hero.h) {
status = 0;
Move.up();
}
//arrow down
if (key.which === 40 && Pos.y === 0) {
status = 0;
Move.down();
}
}
//dev
else {console.log("Status = 0")}
});
};
//link to img1
image.src = "/ajax/img/running-icon-on-transparent-background-hi.png";
//and draw this img
image.onload = function () {CreateGame.drawplayer(); };
//Elements:
var CreateGame = {
drawplayer: function () {
'use strict';
Move.animate();
if (Pos.y === 0) {
//player y = 0 -> stop rotate
status = 2;
}
if (Pos.y === Cnv.h - Hero.h) {
//player is on a floor -> stop rotate
status = 1;
}
},
drawbackground: function () {
'use strict';
//clear all
ctx.clearRect(0, 0, Cnv.w, Cnv.h);
//background doesn't exist now :(
},
drawblocks: function () {
'use strict';
var x = Cnv.w - Block.p;
var y = Block.y;
var w = Block.w;
...