Skip to content

Commit

Permalink
i hate canvas hahaha
Browse files Browse the repository at this point in the history
  • Loading branch information
onlypuppy7 committed Dec 11, 2024
1 parent 79068a7 commit 2025679
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions server-game/src/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 12 additions & 4 deletions src/shell/stringWidth.js
Original file line number Diff line number Diff line change
@@ -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
};
Expand All @@ -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("⎠"));
Expand Down

0 comments on commit 2025679

Please sign in to comment.