this repo has no description
1"use strict";
2
3// Pure math and canvas utilities shared across modules.
4const TLUtils = (() => {
5 const clamp = (x, a, b) => Math.max(a, Math.min(b, x));
6 const fmt = (x, d = 3) => Number.isFinite(x) ? x.toFixed(d) : "NaN";
7 const sign = (x) => (x > 0) - (x < 0);
8
9 function getDPR() {
10 return Math.max(1, Math.floor(window.devicePixelRatio || 1));
11 }
12
13 // Scale canvas internal buffer to match CSS size × device pixel ratio,
14 // returning a logical-pixel coordinate system via ctx.setTransform.
15 function resizeCanvasToCSS(canvas) {
16 const dpr = getDPR();
17 const cssW = canvas.clientWidth;
18 const cssH = Math.round(cssW * (canvas.height / canvas.width));
19 canvas.width = Math.round(cssW * dpr);
20 canvas.height = Math.round(cssH * dpr);
21 const ctx = canvas.getContext("2d");
22 ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
23 return { ctx, w: cssW, h: cssH, dpr };
24 }
25
26 return { clamp, fmt, sign, getDPR, resizeCanvasToCSS };
27})();