我正在尝试在我正在开发的游戏中渲染瓷砖地图,但是在渲染时它会垂直和水平翻转。我正在使用 JavaScript 在 HTML 5 Canvas 中处理它。我已经尝试了我能想到的一切,但我似乎无法摆脱这个问题。
索引 JS:
import Map from './MapClass.js';
export var miniMapScale = .5;
var MiniMap = document.createElement("canvas");
document.body.appendChild(MiniMap);
MiniMap.width = miniMapScale * 640;
MiniMap.height = miniMapScale * 640;
MiniMap.id = "TopDownView";
var ctx = MiniMap.getContext("2d");
var TestMap = new Map([
[1,1,1,1,1,1,1,1,1,1],
[1,0,1,0,0,0,0,0,0,1],
[1,0,1,0,0,0,0,0,0,1],
[1,0,1,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,1,1,1,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1]
]);
function drawTopDownMap() {
ctx.clearRect(0, 0, MiniMap.width, MiniMap.height);
TestMap.draw();
requestAnimationFrame(drawTopDownMap);
};
requestAnimationFrame(drawTopDownMap);
地图类 JS:
import { miniMapScale } from './index.js';
export default class Map{
constructor(mapData) {
this.data = mapData;
this.tileSize = miniMapScale * 64;
this.width = this.data[0].length;
this.height = this.data.length;
this.ctx = document.getElementById("TopDownView").getContext("2d");
};
draw() {
for(var y = 0; y < this.height; y++) {
for(var x = 0; x < this.width; x++) {
var wall = this.data[x][y];
if(wall == 0) {
this.ctx.fillStyle = "#ffe4c4";
this.ctx.fillRect(
x * this.tileSize,
y * this.tileSize,
this.tileSize, this.tileSize
);
}else if(wall == 1) {
this.ctx.fillStyle = "#000000";
this.ctx.fillRect(
x * this.tileSize,
y * this.tileSize,
this.tileSize, this.tileSize
);
};
};
}
}
};
任何帮助将不胜感激!