html5 viewport,javascript - HTML5 - Creating a viewport for canvas - Stack Overflow

Here I've made a rudimentary viewport for you:

You have to keep track of the viewport X and Y (vX, vY) that represent where you are in the world at large. I also kept the viewport width and height as vars instead of hard coding them in.

You can use the arrow keys to change the viewport X and Y which will scroll the map, drawing only the correct tiles.

Hope that helps! Let me know if you have any questions!

EDIT: example of viewport moving with player: http://jsfiddle.net/kmHZt/10/

var canvas, context, board, imageObj, tiles, board, display;

var NUM_OF_TILES = 2;

// viewport

var vX = 0,

vY = 0,

vWidth = 15,

vHeight = 10;

var playerX = 0,

playerY = 0;

var worldWidth = 29,

worldHeight = 19;

function loadMap(map) {

if (map == 1) {

return [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 0], [0, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 0], [0, 1, 1, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 0], [0, 1, 1, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 0], [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1,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0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]];

}

}

$(document).ready(function() {

canvas = document.getElementById("canvas");

context = canvas.getContext("2d");

canvas.tabIndex = 0;

canvas.focus();

canvas.addEventListener('keydown', function(e) {

console.log(e);

var key = null;

switch (e.which) {

case 37:

// Left

if (playerX > 0) playerX--;

break;

case 38:

// Up

if (playerY > 0) playerY--;

break;

case 39:

// Right

if (playerX < worldWidth) playerX++;

break;

case 40:

// Down

if (playerY < worldHeight) playerY++;

break;

}

// Okay! The player is done moving, now we have to determine the "best" viewport.

// Ideally the viewport centers the player,

// but if its too close to an edge we'll have to deal with that edge

vX = playerX - Math.floor(0.5 * vWidth);

if (vX < 0) vX = 0;

if (vX+vWidth > worldWidth) vX = worldWidth - vWidth;

vY = playerY - Math.floor(0.5 * vHeight);

if (vY < 0) vY = 0;

if (vY+vHeight > worldHeight) vY = worldHeight - vHeight;

draw();

}, false);

var board = [];

canvas.width = 512;

canvas.height = 352;

board = loadMap(1);

imageObj = new Image();

tiles = [];

var loadedImagesCount = 0;

for (x = 0; x <= NUM_OF_TILES; x++) {

var imageObj = new Image(); // new instance for each image

imageObj.src = "http://mystikrpg.com/canvas/img/tiles/t" + x + ".png";

imageObj.onload = function() {

// console.log("Added tile ... "+loadedImagesCount);

loadedImagesCount++;

if (loadedImagesCount == NUM_OF_TILES) {

// Onces all tiles are loaded ...

// We paint the map

draw();

}

};

tiles.push(imageObj);

}

function draw() {

context.clearRect(0,0,canvas.width, canvas.height);

for (y = 0; y <= vHeight; y++) {

for (x = 0; x <= vWidth; x++) {

theX = x * 32;

theY = y * 32;

context.drawImage(tiles[board[y+vY][x+vX]], theX, theY, 32, 32);

}

}

context.fillStyle = 'red';

context.fillRect((playerX-vX)*32, (playerY-vY)*32, 32, 32);

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值