MultiplayerMinesweeper/client/www/scripts/display/tileRenderer.js

37 lines
983 B
JavaScript
Raw Normal View History

2022-06-21 14:46:34 +00:00
import {game} from "/scripts/game/game.js"
export const tileArray = loadSprites('/img/mine.png', game.tileSize);
var counter = 0;
2022-06-19 21:54:34 +00:00
export function loadSprites(path, tileSize) {
let tiles = [];
let spriteJank = document.createElement("canvas");
spriteJank.id = `spriteJank${counter}`
spriteJank.style = "display: none;"
spriteJank.width = tileSize;
spriteJank.height = tileSize;
document.body.appendChild(spriteJank);
let ctxj = spriteJank.getContext('2d');
let spriteSheet = new Image();
spriteSheet.src = path
2022-06-19 21:54:34 +00:00
spriteSheet.onload = function() {
for (let y = 0; y < spriteSheet.height/tileSize; y++) {
for (let x = 0; x < spriteSheet.width/tileSize; x++) {
2022-06-21 14:46:34 +00:00
ctxj.drawImage(spriteSheet, -x*tileSize,-y*tileSize)
2022-06-19 21:54:34 +00:00
var tmp = new Image();
tmp.src = spriteJank.toDataURL();
2022-06-21 14:46:34 +00:00
ctxj.clearRect(0, 0, tileSize, tileSize);
2022-06-19 21:54:34 +00:00
tiles.push(tmp)
}
}
}
spriteJank.remove();
2022-06-19 21:54:34 +00:00
return tiles;
}
export const menuArray = loadSprites('/img/modes.png', 32)