diff --git a/src/app/api/orders/[id]/design-spec/route.ts b/src/app/api/orders/[id]/design-spec/route.ts index 092080c..558d3d3 100644 --- a/src/app/api/orders/[id]/design-spec/route.ts +++ b/src/app/api/orders/[id]/design-spec/route.ts @@ -40,21 +40,31 @@ export async function GET(req: Request, { params }: { params: { id: string } }) hPct: number; }; + const printAreaStartX = DISPLAY * (printArea.xPct / 100); + const printAreaStartY = DISPLAY * (printArea.yPct / 100); const printAreaWidthPx = DISPLAY * (printArea.wPct / 100); const printAreaHeightPx = DISPLAY * (printArea.hPct / 100); - // Scale: how many pixels per millimeter - const pxPerMm = printAreaWidthPx / printAreaWidthMm; + // Scale: how many millimeters per pixel + const mmPerPx = printAreaWidthMm / printAreaWidthPx; - // Convert pixel measurements to CM (divide by pxPerMm to get mm, then divide by 10 for cm) - const convertToCm = (px: number) => { - const mm = px / pxPerMm; - return (mm / 10).toFixed(1); + // Convert pixel measurements to CM + // First: subtract the print area offset to get position within print area + // Then: multiply by mmPerPx to get millimeters + // Finally: divide by 10 to get centimeters + const convertToCm = (px: number, elementPx: number | null = null) => { + if (elementPx !== null) { + // For element positions: subtract offset and scale + const relativeToStart = elementPx - px; + const mm = Math.max(0, relativeToStart * mmPerPx); + return (mm / 10).toFixed(1); + } else { + // For dimensions: just scale + const mm = px * mmPerPx; + return (mm / 10).toFixed(1); + } }; - const printAreaStartX = DISPLAY * (printArea.xPct / 100); - const printAreaStartY = DISPLAY * (printArea.yPct / 100); - const generateDesignSection = (designElements: any[], side: 'Front' | 'Back', placement: string | null, designPreview: string | null) => { if (!designElements || designElements.length === 0) return ''; @@ -68,31 +78,38 @@ export async function GET(req: Request, { params }: { params: { id: string } }) ${placement ? `
Design on product (reference only) — with measurement guides
-