picture-it 0.2.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/zones.ts ADDED
@@ -0,0 +1,63 @@
1
+ import { ZONES, type ZoneName, type AnchorPosition } from "./types.ts";
2
+
3
+ export function resolvePosition(
4
+ zone: ZoneName | { x: number; y: number },
5
+ canvasWidth: number,
6
+ canvasHeight: number,
7
+ elementWidth: number,
8
+ elementHeight: number,
9
+ anchor: AnchorPosition = "center"
10
+ ): { x: number; y: number } {
11
+ let centerX: number;
12
+ let centerY: number;
13
+
14
+ if (typeof zone === "string") {
15
+ const z = ZONES[zone];
16
+ if (!z) throw new Error(`Unknown zone: ${zone}`);
17
+ centerX = (z.x / 100) * canvasWidth;
18
+ centerY = (z.y / 100) * canvasHeight;
19
+ } else {
20
+ // Raw percentages or pixels
21
+ centerX = zone.x <= 100 && zone.x >= 0 ? (zone.x / 100) * canvasWidth : zone.x;
22
+ centerY = zone.y <= 100 && zone.y >= 0 ? (zone.y / 100) * canvasHeight : zone.y;
23
+ }
24
+
25
+ // Adjust from center point to top-left based on anchor
26
+ switch (anchor) {
27
+ case "center":
28
+ return {
29
+ x: Math.round(centerX - elementWidth / 2),
30
+ y: Math.round(centerY - elementHeight / 2),
31
+ };
32
+ case "top-left":
33
+ return { x: Math.round(centerX), y: Math.round(centerY) };
34
+ case "top-right":
35
+ return {
36
+ x: Math.round(centerX - elementWidth),
37
+ y: Math.round(centerY),
38
+ };
39
+ case "bottom-left":
40
+ return {
41
+ x: Math.round(centerX),
42
+ y: Math.round(centerY - elementHeight),
43
+ };
44
+ case "bottom-right":
45
+ return {
46
+ x: Math.round(centerX - elementWidth),
47
+ y: Math.round(centerY - elementHeight),
48
+ };
49
+ }
50
+ }
51
+
52
+ export function resolveDimension(
53
+ value: number | string | undefined,
54
+ canvasSize: number,
55
+ fallback: number
56
+ ): number {
57
+ if (value === undefined) return fallback;
58
+ if (typeof value === "number") return value;
59
+ if (value.endsWith("%")) {
60
+ return Math.round((parseFloat(value) / 100) * canvasSize);
61
+ }
62
+ return parseInt(value, 10) || fallback;
63
+ }