File size: 9,666 Bytes
a2d572d
 
 
 
 
 
 
 
 
 
 
 
e56fe87
a2d572d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e56fe87
 
 
 
 
 
 
 
 
 
a2d572d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Freehand polygon selection tool
// User taps to place points, then adjusts nodes before confirming.

const CLOSE_THRESHOLD = 30; // px distance to first point to auto-close
const NODE_RADIUS = 14;     // px, visual + hit area

/**
 * Show the polygon selection overlay on an image.
 * @param {string} imageURL - object URL of the image to select from
 * @returns {Promise<{polygon: {x,y}[], imageBlob: Blob} | null>}
 *   polygon coords are normalized 0–1. Returns null if cancelled.
 */
export function showPolygonSelector(imageURL, initialPoints) {
  return new Promise((resolve) => {
    const overlay = document.createElement('div');
    overlay.className = 'poly-overlay';
    overlay.innerHTML = `
      <div class="poly-toolbar">
        <button class="pill poly-undo-btn" disabled>Undo</button>
        <span class="poly-hint" id="poly-hint">Tap around the item</span>
        <button class="pill poly-close-btn" style="display:none">Close shape</button>
      </div>
      <div class="poly-canvas-wrap">
        <img src="${imageURL}" class="poly-img" id="poly-img" draggable="false"/>
        <canvas id="poly-canvas" class="poly-canvas"></canvas>
      </div>
      <div class="poly-actions" id="poly-actions" style="display:none">
        <button class="big-button" id="poly-confirm">\u2705 Use this selection</button>
        <button class="pill" id="poly-reset">Start over</button>
        <button class="pill" id="poly-cancel">Cancel</button>
      </div>
      <div class="poly-actions" id="poly-drawing-actions">
        <button class="pill" id="poly-cancel2">Cancel</button>
      </div>
    `;
    document.body.appendChild(overlay);

    const img = document.getElementById('poly-img');
    const canvas = document.getElementById('poly-canvas');
    const ctx = canvas.getContext('2d');
    const hint = document.getElementById('poly-hint');
    const undoBtn = overlay.querySelector('.poly-undo-btn');
    const closeBtn = overlay.querySelector('.poly-close-btn');

    let points = [];     // {x, y} in canvas pixel coords
    let closed = false;
    let draggingIdx = -1;
    let imgRect = null;

    function cleanup() {
      overlay.remove();
    }

    // Wait for image to load to size canvas
    img.onload = () => {
      imgRect = img.getBoundingClientRect();
      canvas.width = imgRect.width;
      canvas.height = imgRect.height;
      canvas.style.width = imgRect.width + 'px';
      canvas.style.height = imgRect.height + 'px';

      // Pre-populate with initial points if provided
      if (initialPoints && initialPoints.length >= 3) {
        points = initialPoints.map(p => ({
          x: p.x * canvas.width,
          y: p.y * canvas.height,
        }));
        closePolygon();
      }

      draw();
    };

    // If already loaded (cached)
    if (img.complete && img.naturalWidth) {
      setTimeout(() => img.onload(), 0);
    }

    function draw() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      if (points.length === 0) return;

      // Draw filled polygon (semi-transparent)
      if (closed && points.length >= 3) {
        ctx.beginPath();
        ctx.moveTo(points[0].x, points[0].y);
        for (let i = 1; i < points.length; i++) {
          ctx.lineTo(points[i].x, points[i].y);
        }
        ctx.closePath();
        ctx.fillStyle = 'rgba(240, 180, 41, 0.15)';
        ctx.fill();
      }

      // Draw lines
      ctx.beginPath();
      ctx.moveTo(points[0].x, points[0].y);
      for (let i = 1; i < points.length; i++) {
        ctx.lineTo(points[i].x, points[i].y);
      }
      if (closed) ctx.closePath();
      ctx.strokeStyle = '#f0b429';
      ctx.lineWidth = 3;
      ctx.stroke();

      // Draw nodes
      points.forEach((p, i) => {
        ctx.beginPath();
        ctx.arc(p.x, p.y, NODE_RADIUS, 0, Math.PI * 2);
        ctx.fillStyle = i === 0 ? '#e8738a' : '#f0b429';
        ctx.fill();
        ctx.strokeStyle = '#2d1b4e';
        ctx.lineWidth = 2;
        ctx.stroke();
      });
    }

    function canvasCoords(e) {
      const rect = canvas.getBoundingClientRect();
      return {
        x: e.clientX - rect.left,
        y: e.clientY - rect.top,
      };
    }

    function distPt(a, b) {
      return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
    }

    function findNodeAt(pos) {
      for (let i = points.length - 1; i >= 0; i--) {
        if (distPt(pos, points[i]) < NODE_RADIUS + 10) return i;
      }
      return -1;
    }

    function closePolygon() {
      if (points.length < 3) return;
      closed = true;
      hint.textContent = 'Drag nodes to adjust';
      closeBtn.style.display = 'none';
      document.getElementById('poly-actions').style.display = '';
      document.getElementById('poly-drawing-actions').style.display = 'none';
      draw();
    }

    // Pointer events on canvas
    let pointerDownPos = null;
    let pointerMoved = false;

    canvas.addEventListener('pointerdown', (e) => {
      e.preventDefault();
      const pos = canvasCoords(e);
      pointerDownPos = pos;
      pointerMoved = false;

      if (closed) {
        // Try to drag a node
        draggingIdx = findNodeAt(pos);
        if (draggingIdx >= 0) {
          canvas.setPointerCapture(e.pointerId);
        }
        return;
      }

      // Check if near first point -> close
      if (points.length >= 3 && distPt(pos, points[0]) < CLOSE_THRESHOLD) {
        closePolygon();
        return;
      }

      draggingIdx = -1;
    });

    canvas.addEventListener('pointermove', (e) => {
      e.preventDefault();
      const pos = canvasCoords(e);

      if (pointerDownPos && distPt(pos, pointerDownPos) > 5) {
        pointerMoved = true;
      }

      if (closed && draggingIdx >= 0) {
        // Clamp to canvas
        points[draggingIdx].x = Math.max(0, Math.min(canvas.width, pos.x));
        points[draggingIdx].y = Math.max(0, Math.min(canvas.height, pos.y));
        draw();
      }
    });

    canvas.addEventListener('pointerup', (e) => {
      e.preventDefault();
      const pos = canvasCoords(e);

      if (closed) {
        draggingIdx = -1;
        return;
      }

      // Only add point if it was a tap (not a drag)
      if (!pointerMoved) {
        points.push(pos);
        undoBtn.disabled = false;
        if (points.length >= 3) closeBtn.style.display = '';
        draw();
      }

      pointerDownPos = null;
      pointerMoved = false;
    });

    // Undo
    undoBtn.addEventListener('click', () => {
      if (closed) {
        // Reopen
        closed = false;
        hint.textContent = 'Tap to add more points';
        document.getElementById('poly-actions').style.display = 'none';
        document.getElementById('poly-drawing-actions').style.display = '';
        if (points.length >= 3) closeBtn.style.display = '';
      } else {
        points.pop();
      }
      undoBtn.disabled = points.length === 0;
      if (points.length < 3) closeBtn.style.display = 'none';
      draw();
    });

    // Close shape button
    closeBtn.addEventListener('click', closePolygon);

    // Confirm
    document.getElementById('poly-confirm').addEventListener('click', async () => {
      if (!closed || points.length < 3) return;

      // Normalize coordinates
      const polygon = points.map(p => ({
        x: p.x / canvas.width,
        y: p.y / canvas.height,
      }));

      // Crop and mask the image
      const croppedBlob = await cropToPolygon(img, polygon);

      cleanup();
      resolve({ polygon, imageBlob: croppedBlob });
    });

    // Reset
    document.getElementById('poly-reset').addEventListener('click', () => {
      points = [];
      closed = false;
      draggingIdx = -1;
      hint.textContent = 'Tap around the item';
      undoBtn.disabled = true;
      closeBtn.style.display = 'none';
      document.getElementById('poly-actions').style.display = 'none';
      document.getElementById('poly-drawing-actions').style.display = '';
      draw();
    });

    // Cancel
    const cancelHandler = () => { cleanup(); resolve(null); };
    document.getElementById('poly-cancel').addEventListener('click', cancelHandler);
    document.getElementById('poly-cancel2').addEventListener('click', cancelHandler);
  });
}

/**
 * Crop image to polygon bounding box with pixels outside the polygon made transparent.
 */
async function cropToPolygon(imgEl, polygon) {
  // Draw original image at full resolution
  const bitmap = await createImageBitmap(imgEl);
  const fullW = bitmap.width;
  const fullH = bitmap.height;

  // Convert normalized polygon to pixel coords
  const pxPoly = polygon.map(p => ({ x: p.x * fullW, y: p.y * fullH }));

  // Bounding box
  let minX = fullW, minY = fullH, maxX = 0, maxY = 0;
  for (const p of pxPoly) {
    minX = Math.min(minX, p.x);
    minY = Math.min(minY, p.y);
    maxX = Math.max(maxX, p.x);
    maxY = Math.max(maxY, p.y);
  }

  // Add small padding
  const pad = 4;
  minX = Math.max(0, Math.floor(minX) - pad);
  minY = Math.max(0, Math.floor(minY) - pad);
  maxX = Math.min(fullW, Math.ceil(maxX) + pad);
  maxY = Math.min(fullH, Math.ceil(maxY) + pad);
  const cropW = maxX - minX;
  const cropH = maxY - minY;

  // Draw cropped region
  const canvas = new OffscreenCanvas(cropW, cropH);
  const ctx = canvas.getContext('2d');

  // Clip to polygon
  ctx.beginPath();
  const first = pxPoly[0];
  ctx.moveTo(first.x - minX, first.y - minY);
  for (let i = 1; i < pxPoly.length; i++) {
    ctx.lineTo(pxPoly[i].x - minX, pxPoly[i].y - minY);
  }
  ctx.closePath();
  ctx.clip();

  // Draw image
  ctx.drawImage(bitmap, minX, minY, cropW, cropH, 0, 0, cropW, cropH);

  return canvas.convertToBlob({ type: 'image/png' });
}