Add simple tree graph mode (#4)
All checks were successful
continuous-integration/drone/push Build is passing

Add a new kind of family tree: simple tree

Reviewed-on: #4
This commit is contained in:
2023-08-26 13:59:58 +00:00
parent 635fb667e1
commit 8086c1b4c9
9 changed files with 499 additions and 4 deletions

View File

@ -0,0 +1,30 @@
let canvas: HTMLCanvasElement = document.createElement("canvas");
let charLen: Map<string, Map<string, number>> = new Map();
/**
* Uses canvas.measureText to compute and return the width of the given text of given font in pixels.
*
* @param {String} text The text to be rendered.
* @param {String} font The css font descriptor that text is to be rendered with (e.g. "bold 14px verdana").
*
* @see https://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393
*/
function computeTextWidth(text: string, font: string) {
// re-use canvas object for better performance
const context = canvas.getContext("2d")!;
context.font = font;
const metrics = context.measureText(text);
return metrics.width;
}
export function getTextWidth(text: string, font: string) {
if (!charLen.has(font)) charLen.set(font, new Map());
const ref = charLen.get(font)!;
let size = 0;
for (const c of text) {
if (!ref.has(c)) ref.set(c, computeTextWidth(c, font));
size += ref.get(c)!;
}
return size;
}