All files / src/components/animation/RollingNumber helpers.ts

0% Statements 0/16
0% Branches 0/8
0% Functions 0/3
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                                                                     
export function getTextWidthFactory() {
  let canvas: HTMLCanvasElement | null = null;
  let context: CanvasRenderingContext2D | null = null;
 
  return function getTextWidth(
    text: string | number,
    fontSize: string | number,
    fontFamily: string,
  ) {
    if (typeof fontSize === "string") {
      fontSize = fontSize.replace("px", "");
    }
    if (!canvas) {
      canvas = document.createElement("canvas");
      context = canvas.getContext("2d");
    }
 
    if (context) {
      context.clearRect(0, 0, canvas.width, canvas.height);
      context.font = `${fontSize}px ${fontFamily}`;
 
      return context.measureText(typeof text === "number" ? String(text) : text)
        .width;
    }
 
    return 30;
  };
}
 
export function getRandomIntInclusive(min: number, max: number) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}