Я уверен, что это было решено 1000 раз раньше: я получил холст размером 960 * 560 и комнату размером 5000 * 3000, из которых всегда нужно рисовать только 960 * 560, в зависимости от того, где игрок есть. Игрок должен быть всегда посередине, но когда он близок к границам - тогда должен быть рассчитан лучший вид). Игрок может свободно перемещаться с помощью WASD или клавиш со стрелками. И все объекты должны двигаться сами - вместо этого я перемещаю все остальное, кроме игрока, чтобы создать иллюзию, что игрок движется.
Теперь я нашел эти два quesitons:
HTML5 - Создание видового экрана для холста работает, но только для этого типа игры я не могу воспроизвести код для моего.
Изменение вида "center" холста html5, кажется, более перспективны и перфоманты, но я понимаю это только для правильного рисования всех других объектов относительно игрока, а не для того, чтобы прокрутить видовой экран холста относительно игрока, который я хочу достичь сначала, конечно.
Мой код (упрощенный - логика игры отдельно):
var canvas = document.getElementById("game");
canvas.tabIndex = 0;
canvas.focus();
var cc = canvas.getContext("2d");
// Define viewports for scrolling inside the canvas
/* Viewport x position */ view_xview = 0;
/* Viewport y position */ view_yview = 0;
/* Viewport width */ view_wview = 960;
/* Viewport height */ view_hview = 560;
/* Sector width */ room_width = 5000;
/* Sector height */ room_height = 3000;
canvas.width = view_wview;
canvas.height = view_hview;
function draw()
{
clear();
requestAnimFrame(draw);
// World end and viewport
if (player.x < 20) player.x = 20;
if (player.y < 20) player.y = 20;
if (player.x > room_width-20) player.x = room_width-20;
if (player.y > room_height-20) player.y = room_height-20;
if (player.x > view_wview/2) ... ?
if (player.y > view_hview/2) ... ?
}
То, как я пытаюсь заставить его работать, чувствует себя совершенно неправильно, и я даже не знаю, как я его пытаюсь... Любые идеи? Что вы думаете об объекте context.transform?
Надеюсь, вы понимаете мое описание и у кого-то есть идея. С уважением