@pooder/kit 3.3.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/coordinate.ts CHANGED
@@ -1,106 +1,106 @@
1
- export interface Point {
2
- x: number;
3
- y: number;
4
- }
5
-
6
- export interface Size {
7
- width: number;
8
- height: number;
9
- }
10
-
11
- export type Unit = "px" | "mm" | "cm" | "in";
12
-
13
- export interface Layout {
14
- scale: number;
15
- offsetX: number;
16
- offsetY: number;
17
- width: number;
18
- height: number;
19
- }
20
-
21
- export class Coordinate {
22
- /**
23
- * Calculate layout to fit content within container while preserving aspect ratio.
24
- */
25
- static calculateLayout(
26
- container: Size,
27
- content: Size,
28
- padding: number = 0,
29
- ): Layout {
30
- const availableWidth = Math.max(0, container.width - padding * 2);
31
- const availableHeight = Math.max(0, container.height - padding * 2);
32
-
33
- if (content.width === 0 || content.height === 0) {
34
- return { scale: 1, offsetX: 0, offsetY: 0, width: 0, height: 0 };
35
- }
36
-
37
- const scaleX = availableWidth / content.width;
38
- const scaleY = availableHeight / content.height;
39
- const scale = Math.min(scaleX, scaleY);
40
-
41
- const width = content.width * scale;
42
- const height = content.height * scale;
43
-
44
- const offsetX = (container.width - width) / 2;
45
- const offsetY = (container.height - height) / 2;
46
-
47
- return { scale, offsetX, offsetY, width, height };
48
- }
49
-
50
- /**
51
- * Convert an absolute value to a normalized value (0-1).
52
- * @param value Absolute value (e.g., pixels)
53
- * @param total Total dimension size (e.g., canvas width)
54
- */
55
- static toNormalized(value: number, total: number): number {
56
- return total === 0 ? 0 : value / total;
57
- }
58
-
59
- /**
60
- * Convert a normalized value (0-1) to an absolute value.
61
- * @param normalized Normalized value (0-1)
62
- * @param total Total dimension size (e.g., canvas width)
63
- */
64
- static toAbsolute(normalized: number, total: number): number {
65
- return normalized * total;
66
- }
67
-
68
- /**
69
- * Normalize a point's coordinates.
70
- */
71
- static normalizePoint(point: Point, size: Size): Point {
72
- return {
73
- x: this.toNormalized(point.x, size.width),
74
- y: this.toNormalized(point.y, size.height),
75
- };
76
- }
77
-
78
- /**
79
- * Denormalize a point's coordinates to absolute pixels.
80
- */
81
- static denormalizePoint(point: Point, size: Size): Point {
82
- return {
83
- x: this.toAbsolute(point.x, size.width),
84
- y: this.toAbsolute(point.y, size.height),
85
- };
86
- }
87
-
88
- static convertUnit(value: number, from: Unit, to: Unit): number {
89
- if (from === to) return value;
90
-
91
- // Base unit: mm
92
- const toMM: Record<Unit, number> = {
93
- px: 0.264583, // 1px = 0.264583mm (96 DPI)
94
- mm: 1,
95
- cm: 10,
96
- in: 25.4
97
- };
98
-
99
- const mmValue = value * (from === 'px' ? toMM.px : toMM[from] || 1);
100
-
101
- if (to === 'px') {
102
- return mmValue / toMM.px;
103
- }
104
- return mmValue / (toMM[to] || 1);
105
- }
106
- }
1
+ export interface Point {
2
+ x: number;
3
+ y: number;
4
+ }
5
+
6
+ export interface Size {
7
+ width: number;
8
+ height: number;
9
+ }
10
+
11
+ export type Unit = "px" | "mm" | "cm" | "in";
12
+
13
+ export interface Layout {
14
+ scale: number;
15
+ offsetX: number;
16
+ offsetY: number;
17
+ width: number;
18
+ height: number;
19
+ }
20
+
21
+ export class Coordinate {
22
+ /**
23
+ * Calculate layout to fit content within container while preserving aspect ratio.
24
+ */
25
+ static calculateLayout(
26
+ container: Size,
27
+ content: Size,
28
+ padding: number = 0,
29
+ ): Layout {
30
+ const availableWidth = Math.max(0, container.width - padding * 2);
31
+ const availableHeight = Math.max(0, container.height - padding * 2);
32
+
33
+ if (content.width === 0 || content.height === 0) {
34
+ return { scale: 1, offsetX: 0, offsetY: 0, width: 0, height: 0 };
35
+ }
36
+
37
+ const scaleX = availableWidth / content.width;
38
+ const scaleY = availableHeight / content.height;
39
+ const scale = Math.min(scaleX, scaleY);
40
+
41
+ const width = content.width * scale;
42
+ const height = content.height * scale;
43
+
44
+ const offsetX = (container.width - width) / 2;
45
+ const offsetY = (container.height - height) / 2;
46
+
47
+ return { scale, offsetX, offsetY, width, height };
48
+ }
49
+
50
+ /**
51
+ * Convert an absolute value to a normalized value (0-1).
52
+ * @param value Absolute value (e.g., pixels)
53
+ * @param total Total dimension size (e.g., canvas width)
54
+ */
55
+ static toNormalized(value: number, total: number): number {
56
+ return total === 0 ? 0 : value / total;
57
+ }
58
+
59
+ /**
60
+ * Convert a normalized value (0-1) to an absolute value.
61
+ * @param normalized Normalized value (0-1)
62
+ * @param total Total dimension size (e.g., canvas width)
63
+ */
64
+ static toAbsolute(normalized: number, total: number): number {
65
+ return normalized * total;
66
+ }
67
+
68
+ /**
69
+ * Normalize a point's coordinates.
70
+ */
71
+ static normalizePoint(point: Point, size: Size): Point {
72
+ return {
73
+ x: this.toNormalized(point.x, size.width),
74
+ y: this.toNormalized(point.y, size.height),
75
+ };
76
+ }
77
+
78
+ /**
79
+ * Denormalize a point's coordinates to absolute pixels.
80
+ */
81
+ static denormalizePoint(point: Point, size: Size): Point {
82
+ return {
83
+ x: this.toAbsolute(point.x, size.width),
84
+ y: this.toAbsolute(point.y, size.height),
85
+ };
86
+ }
87
+
88
+ static convertUnit(value: number, from: Unit, to: Unit): number {
89
+ if (from === to) return value;
90
+
91
+ // Base unit: mm
92
+ const toMM: Record<Unit, number> = {
93
+ px: 0.264583, // 1px = 0.264583mm (96 DPI)
94
+ mm: 1,
95
+ cm: 10,
96
+ in: 25.4
97
+ };
98
+
99
+ const mmValue = value * (from === 'px' ? toMM.px : toMM[from] || 1);
100
+
101
+ if (to === 'px') {
102
+ return mmValue / toMM.px;
103
+ }
104
+ return mmValue / (toMM[to] || 1);
105
+ }
106
+ }