From 20256792aa01e9c1c2aef0b5e58b04e80e537003 Mon Sep 17 00:00:00 2001 From: onlypuppy7 <35199523+onlypuppy7@users.noreply.github.com> Date: Wed, 11 Dec 2024 18:14:40 +0000 Subject: [PATCH] i hate canvas hahaha --- package.json | 2 +- server-game/src/rooms.js | 3 +++ src/shell/stringWidth.js | 16 ++++++++++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index a0e0edec..ce6051f1 100644 --- a/package.json +++ b/package.json @@ -18,10 +18,10 @@ "cn": "lsof -t -i :13371 | xargs kill -9 && lsof -t -i :13370 | xargs kill -9 && lsof -t -i :13372 | xargs kill -9" }, "dependencies": { + "@napi-rs/canvas": "^0.1.65", "@vuepress/theme-default": "^2.0.0-rc.60", "babylonjs": "3.3.0", "bcrypt": "^5.1.1", - "canvas": "^3.0.0-rc2", "chalk": "^5.3.0", "cross-spawn": "^7.0.3", "express": "^4.21.0", diff --git a/server-game/src/rooms.js b/server-game/src/rooms.js index f794d3d2..b7f37a81 100644 --- a/server-game/src/rooms.js +++ b/server-game/src/rooms.js @@ -13,6 +13,7 @@ import { ItemManagerConstructor } from '#itemManager'; import { PermissionsConstructor } from '#permissions'; import BABYLON from "babylonjs"; import { censor } from "#censor"; +import { removeCanvasResources } from '#stringWidth'; //legacyshell: logging import log from '#coloured-logging'; //legacyshell: plugins @@ -358,6 +359,8 @@ export class newRoom { this.metaLoopObject.stop(); } catch (error) { } //possible that some wasnt initted, or something + removeCanvasResources(); + parentPort.postMessage([Comm.Worker.terminate, this.gameId]); // parentPort.close(); diff --git a/src/shell/stringWidth.js b/src/shell/stringWidth.js index 9fdadd98..35f319a1 100644 --- a/src/shell/stringWidth.js +++ b/src/shell/stringWidth.js @@ -1,18 +1,22 @@ //legacyshell: string width import { isClient } from '#constants'; -import { createCanvas, registerFont } from 'canvas'; +import canvas from '@napi-rs/canvas'; // +let nameTestFont = "Nunito"; + //(server-only-start) -registerFont('./src/Nunito-Bold.ttf', { family: 'Nunito' }); +const { createCanvas } = canvas; + +nameTestFont = "./src/Nunito-Bold.ttf"; -const nameTestCanvas = createCanvas(200, 50); +let nameTestCanvas = createCanvas(200, 50); //(server-only-end) export function getStringWidth(str) { const context = nameTestCanvas.getContext('2d'); - context.font = 'bold 1em Nunito, sans-serif'; // same font definition on both environments + context.font = `bold 1em ${nameTestFont}, sans-serif`; // same font definition on both environments return context.measureText(str).width / (isClient ? 1 : 2); //dont ask abt the division }; @@ -39,6 +43,10 @@ export function fixStringWidth(str, len = 80) { return str; }; +export function removeCanvasResources() { + nameTestCanvas = null; +}; + // console.log("STRING WIDTH\n\n\n\nn\n\n\n\n\n\n\n", fixStringWidth("298h398fh23oi23fhf23328hf20aw3g"), getStringWidth("298h398fh23oi23fhf23328hf20aw3g")); // console.log("STRING HEIGHT\n\n\n\nn\n\n\n\n\n\n\n", getStringHeight("298h398fh23oi23fhf23328hf20aw3g")); // console.log("STRING HEIGHT\n\n\n\nn\n\n\n\n\n\n\n", getStringHeight("⎠"));