JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<div id="gameArea">
<canvas id="canvas"></canvas>
<div id="stat"></div>
</div>
</body>
CSS
#gameArea {
position: absolute;
left: 50%;
top: 50%;
/*border: 1px solid black;*/
margin: 0;
}
#canvas {
width: 100%;
height: 100%;
background-color: #ccc;
}
#stat {
position: absolute;
top: 0;
height: 8%;
width: 100%;
opacity: 0.8;
}
#speed {
}
JavaScript
// CONSTANTS
var CIRCLE_SIZE = 1 / 6,
RAD = Math.PI / 360,
FULL_CIRCLE = 2 * Math.PI,
KEY_CODE = {
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40
},
START_SPEED = RAD * 2,
SPEED_UP = 0.1;
// Глобальные переменные
var startAngle = 60 * RAD,
endAngle = 2 * Math.PI / 3 + startAngle,
circleRadius,
circleCentrX,
circleCentrY,
gameArea,
game,
canvas,
speed = START_SPEED;
window.onload = function () {
gameArea = document.getElementById('gameArea');
canvas = document.getElementById('canvas');
game = canvas.getContext("2d");
// ---------------*********** Start Game ************---------------------
resizeGame();
drawObject();
window.addEventListener('resize', resizeGame, false);
window.addEventListener('resize', drawObject, false);
window.addEventListener('keydown', pressKey, false);
window.addEventListener('keyup', function () {
speed = START_SPEED;
}, false);
// ---------------*********** End Game **************---------------------
}
// Перерисовка с учетом изменений
var update = function (dAngle) {
game.rotate(dAngle);
drawObject();
}
// Изменение экрана
var resizeGame = function () {
var widthToHeight = 16 / 9, // Желаемое отношение w/h
newWidth = window.innerWidth, // Новые значения ширины и высоты
newHeight = window.innerHeight - 25,
newWidthtoHeight = newWidth / newHeight; //Новое отношение w/h
if (newWidthtoHeight > widthToHeight) { // Ширина больше чем надо
newWidth = newHeight * widthToHeight;
} else { // Высота больше чем надо
newHeight = newWidth * widthToHeight;
}
//Изменяем на новые значения
gameArea.style.height = newHeight + 'px';
gameArea.style.width = newWidth + 'px';
//Центрируем и меняем шрифт
gameArea.style.marginTop = (-newHeight / 2) + 'px';
gameArea.style.marginLeft = (-newWidth / 2) + 'px';
gameArea.style.fontSize =...