draw basic tank

This commit is contained in:
ez_lcw 2023-07-05 22:04:06 +08:00
parent 29eaa5c16e
commit 543c82a8f4
2 changed files with 23 additions and 0 deletions

View File

Before

Width:  |  Height:  |  Size: 447 B

After

Width:  |  Height:  |  Size: 447 B

View File

@ -2,6 +2,29 @@ import { io } from "socket.io-client";
const socket = io(); const socket = io();
function loadImg(url) {
const img_url = url;
const img = new Image();
img.src = img_url;
return new Promise((resolve, reject) => {
img.onload = () => { resolve(img); };
img.onerror = (err) => { reject(err); };
});
}
const imgs = {};
async function loadAssets() {
imgs.basic = await loadImg(new URL('assets/basic.svg', import.meta.url));
}
await loadAssets();
const cvs = document.querySelector("#board");
const ctx = cvs.getContext("2d");
socket.on("update",(tank_info) => { socket.on("update",(tank_info) => {
document.querySelector("#raw-info").innerHTML = JSON.stringify(tank_info); document.querySelector("#raw-info").innerHTML = JSON.stringify(tank_info);
ctx.clearRect(0, 0, cvs.width, cvs.height);
ctx.drawImage(imgs.basic, tank_info.position.x, tank_info.position.y, 100, 100);
}); });