I'm trying to load sprites from a tile set, and load them into a HTML5 canvas.
The problem is that the the coordinates aren't equal to the of the size of the images (It's getting stretched)
All my images are 64x64 pixels. (Preview on 1080p resolution: ) As you can see, the canvas is stretching those 64x64 images.
All of the code: HTML:
<html>
<head>
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/board.css" />
<script src="js/ext/jquery.js"></script>
<script src="js/game.js"></script>
</head>
<body>
<canvas id="GameBoard"></canvas>
</body>
</html>
CSS:
#GameBoard {
width: 100vw;
height: 100vh;
border: 1px solid white;
}
JavaScript:
$(document).ready(function()
{
createWorld();
});
function createWorld()
{
var TileSets = loadTiles();
drawWorld(TileSets);
}
var chunks =
[
[[0],[0],[0],[0],[0]],
[[0],[1],[0],[1],[0]],
[[0],[1],[0],[1],[0]],
[[0],[1],[0],[1],[0]],
[[0],[0],[0],[0],[0]]
];
var tiles =
[
{ tileid: 0, tileset: 1, tilerow: 0, tilecolumn: 0},
{ tileid: 1, tileset: 1, tilerow: 0, tilecolumn: 1}
]
var tilesets =
[
{ tilesetid: 0, tilesrc: "static/tiles/tileset0.png", description:"OLD" },
{ tilesetid: 1, tilesrc: "static/tiles/tileset1.png", description:"New TEST" }
]
function loadTiles()
{
var tiless = [];
for(var i = 0; i < tilesets.length; i++)
{
var img = new Image();
img.src = tilesets[i].tilesrc;
tiless.push(img);
}
return tiless;
}
function drawWorld(TileSets)
{
var WorldData = chunks;
var canvas = document.getElementById("GameBoard");
var counter = 0;
for(var i = 0; i < WorldData.length; i++)
{
for(var j = 0; j < WorldData[i].length; j++)
{
counter++;
var ctx = canvas.getContext("2d");
var tileid = WorldData[i][j];
var tilerow = tiles[tileid].tilerow;
var tilecolumn = tiles[tileid].tilecolumn;
console.log("DRAWN: " + counter + " FROM SET: " + tilerow + "x" + tilecolumn + " AT: " + i + "x" + j);
ctx.drawImage(TileSets[1], 64 * tilecolumn , 64 * tilerow, 64, 64, 64 * j, 64 * i, 64, 64);
}
}
}
So is there any way, JavaScript, CSS, or something else to make the size of the grid of the canvas 64 by 64 so that the images are placed on their own scale next to eachother.