@sealpixel/ui 1.0.0-rc.2 → 1.0.0-rc.20

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.
Files changed (148) hide show
  1. package/LICENSE.md +2 -2
  2. package/README.md +1 -1
  3. package/dist/components/sealpixel-button.d.ts +0 -1
  4. package/dist/components/sealpixel-button.js +1 -1
  5. package/dist/components/sealpixel-button.js.map +1 -1
  6. package/dist/components/sealpixel-editor.d.ts +71 -20
  7. package/dist/components/sealpixel-editor.js +265 -57
  8. package/dist/components/sealpixel-editor.js.map +1 -1
  9. package/dist/components/sealpixel-nav.d.ts +0 -1
  10. package/dist/components/sealpixel-nav.js +1 -1
  11. package/dist/components/sealpixel-nav.js.map +1 -1
  12. package/dist/components/sealpixel-slider.d.ts +12 -17
  13. package/dist/components/sealpixel-slider.js +64 -10
  14. package/dist/components/sealpixel-slider.js.map +1 -1
  15. package/dist/components/sealpixel-stage.d.ts +17 -1
  16. package/dist/components/sealpixel-stage.js +57 -6
  17. package/dist/components/sealpixel-stage.js.map +1 -1
  18. package/dist/components/sealpixel-stepper.d.ts +0 -1
  19. package/dist/components/sealpixel-stepper.js +4 -4
  20. package/dist/components/sealpixel-stepper.js.map +1 -1
  21. package/dist/components/sealpixel-toolbar.d.ts +6 -2
  22. package/dist/components/sealpixel-toolbar.js +71 -5
  23. package/dist/components/sealpixel-toolbar.js.map +1 -1
  24. package/dist/controllers/gesture-controller.d.ts +5 -1
  25. package/dist/controllers/gesture-controller.js +24 -4
  26. package/dist/controllers/gesture-controller.js.map +1 -1
  27. package/dist/controllers/session-controller.d.ts +0 -1
  28. package/dist/controllers/session-controller.js +2 -0
  29. package/dist/controllers/session-controller.js.map +1 -1
  30. package/dist/define.d.ts +0 -1
  31. package/dist/define.js.map +1 -1
  32. package/dist/i18n.d.ts +0 -1
  33. package/dist/i18n.js.map +1 -1
  34. package/dist/icons.d.ts +1 -1
  35. package/dist/icons.js +1 -0
  36. package/dist/icons.js.map +1 -1
  37. package/dist/index.d.ts +1 -2
  38. package/dist/index.js +1 -1
  39. package/dist/index.js.map +1 -1
  40. package/dist/locales/de.d.ts +0 -1
  41. package/dist/locales/de.js +22 -0
  42. package/dist/locales/de.js.map +1 -1
  43. package/dist/locales/en.d.ts +0 -1
  44. package/dist/locales/en.js +22 -0
  45. package/dist/locales/en.js.map +1 -1
  46. package/dist/locales/es.d.ts +0 -1
  47. package/dist/locales/es.js +22 -0
  48. package/dist/locales/es.js.map +1 -1
  49. package/dist/locales/fr.d.ts +0 -1
  50. package/dist/locales/fr.js +22 -0
  51. package/dist/locales/fr.js.map +1 -1
  52. package/dist/locales/index.d.ts +0 -1
  53. package/dist/locales/index.js.map +1 -1
  54. package/dist/locales/ja.d.ts +0 -1
  55. package/dist/locales/ja.js +22 -0
  56. package/dist/locales/ja.js.map +1 -1
  57. package/dist/locales/pt.d.ts +0 -1
  58. package/dist/locales/pt.js +22 -0
  59. package/dist/locales/pt.js.map +1 -1
  60. package/dist/theme.d.ts +0 -1
  61. package/dist/theme.js.map +1 -1
  62. package/dist/tool.d.ts +12 -1
  63. package/dist/tool.js.map +1 -1
  64. package/dist/tools/annotate.d.ts +0 -1
  65. package/dist/tools/annotate.js +277 -28
  66. package/dist/tools/annotate.js.map +1 -1
  67. package/dist/tools/assist.d.ts +103 -0
  68. package/dist/tools/assist.js +361 -0
  69. package/dist/tools/assist.js.map +1 -0
  70. package/dist/tools/crop-mask.d.ts +0 -1
  71. package/dist/tools/crop-mask.js.map +1 -1
  72. package/dist/tools/crop-rotation.d.ts +64 -0
  73. package/dist/tools/crop-rotation.js +172 -0
  74. package/dist/tools/crop-rotation.js.map +1 -0
  75. package/dist/tools/crop.d.ts +0 -1
  76. package/dist/tools/crop.js +99 -15
  77. package/dist/tools/crop.js.map +1 -1
  78. package/dist/tools/delete-handle.d.ts +0 -1
  79. package/dist/tools/delete-handle.js +6 -0
  80. package/dist/tools/delete-handle.js.map +1 -1
  81. package/dist/tools/filter.d.ts +0 -1
  82. package/dist/tools/filter.js.map +1 -1
  83. package/dist/tools/finetune.d.ts +0 -1
  84. package/dist/tools/finetune.js.map +1 -1
  85. package/dist/tools/frame.d.ts +0 -1
  86. package/dist/tools/frame.js.map +1 -1
  87. package/dist/tools/handles.d.ts +26 -0
  88. package/dist/tools/handles.js +51 -0
  89. package/dist/tools/handles.js.map +1 -0
  90. package/dist/tools/index.d.ts +2 -2
  91. package/dist/tools/index.js +3 -1
  92. package/dist/tools/index.js.map +1 -1
  93. package/dist/tools/keys.d.ts +59 -0
  94. package/dist/tools/keys.js +120 -0
  95. package/dist/tools/keys.js.map +1 -0
  96. package/dist/tools/redact.d.ts +10 -1
  97. package/dist/tools/redact.js +176 -12
  98. package/dist/tools/redact.js.map +1 -1
  99. package/dist/tools/region-transform.d.ts +69 -0
  100. package/dist/tools/region-transform.js +192 -0
  101. package/dist/tools/region-transform.js.map +1 -0
  102. package/dist/tools/resize.d.ts +0 -1
  103. package/dist/tools/resize.js +19 -6
  104. package/dist/tools/resize.js.map +1 -1
  105. package/dist/tools/sticker.d.ts +0 -1
  106. package/dist/tools/sticker.js +57 -2
  107. package/dist/tools/sticker.js.map +1 -1
  108. package/dist/tools/watermark.d.ts +0 -1
  109. package/dist/tools/watermark.js.map +1 -1
  110. package/dist/view-mapping.d.ts +1 -2
  111. package/dist/view-mapping.js +1 -1
  112. package/dist/view-mapping.js.map +1 -1
  113. package/package.json +8 -8
  114. package/dist/components/sealpixel-button.d.ts.map +0 -1
  115. package/dist/components/sealpixel-editor.d.ts.map +0 -1
  116. package/dist/components/sealpixel-nav.d.ts.map +0 -1
  117. package/dist/components/sealpixel-slider.d.ts.map +0 -1
  118. package/dist/components/sealpixel-stage.d.ts.map +0 -1
  119. package/dist/components/sealpixel-stepper.d.ts.map +0 -1
  120. package/dist/components/sealpixel-toolbar.d.ts.map +0 -1
  121. package/dist/controllers/gesture-controller.d.ts.map +0 -1
  122. package/dist/controllers/session-controller.d.ts.map +0 -1
  123. package/dist/define.d.ts.map +0 -1
  124. package/dist/i18n.d.ts.map +0 -1
  125. package/dist/icons.d.ts.map +0 -1
  126. package/dist/index.d.ts.map +0 -1
  127. package/dist/locales/de.d.ts.map +0 -1
  128. package/dist/locales/en.d.ts.map +0 -1
  129. package/dist/locales/es.d.ts.map +0 -1
  130. package/dist/locales/fr.d.ts.map +0 -1
  131. package/dist/locales/index.d.ts.map +0 -1
  132. package/dist/locales/ja.d.ts.map +0 -1
  133. package/dist/locales/pt.d.ts.map +0 -1
  134. package/dist/theme.d.ts.map +0 -1
  135. package/dist/tool.d.ts.map +0 -1
  136. package/dist/tools/annotate.d.ts.map +0 -1
  137. package/dist/tools/crop-mask.d.ts.map +0 -1
  138. package/dist/tools/crop.d.ts.map +0 -1
  139. package/dist/tools/delete-handle.d.ts.map +0 -1
  140. package/dist/tools/filter.d.ts.map +0 -1
  141. package/dist/tools/finetune.d.ts.map +0 -1
  142. package/dist/tools/frame.d.ts.map +0 -1
  143. package/dist/tools/index.d.ts.map +0 -1
  144. package/dist/tools/redact.d.ts.map +0 -1
  145. package/dist/tools/resize.d.ts.map +0 -1
  146. package/dist/tools/sticker.d.ts.map +0 -1
  147. package/dist/tools/watermark.d.ts.map +0 -1
  148. package/dist/view-mapping.d.ts.map +0 -1
@@ -4,6 +4,8 @@ import { icons } from '../icons.js';
4
4
  import { cropClipPath } from './crop-mask.js';
5
5
  import { deleteHandle, pointsBox, toPx } from './delete-handle.js';
6
6
  import { opId } from './finetune.js';
7
+ import { elementKeys, focusEditor, focusWhen } from './keys.js';
8
+ import { corners, mapPoints, resize, slide, snapAngle, } from './region-transform.js';
7
9
  const KINDS = ['rect', 'ellipse', 'line', 'arrow', 'path', 'text'];
8
10
  /** Annotate coordinates live in image space: show the image before crop and resize. */
9
11
  function beforeCrop(log) {
@@ -25,7 +27,9 @@ const state = {
25
27
  selected: null,
26
28
  drawing: null,
27
29
  drag: null,
30
+ transform: null,
28
31
  editingText: null,
32
+ focusPending: null,
29
33
  };
30
34
  const hex = (c) => `#${[c[0], c[1], c[2]]
31
35
  .map((v) => Math.round(v * 255)
@@ -64,8 +68,10 @@ export const annotateTool = {
64
68
  deactivate: (ctx) => {
65
69
  state.drawing = null;
66
70
  state.drag = null;
71
+ state.transform = null;
67
72
  state.selected = null;
68
73
  state.editingText = null;
74
+ state.focusPending = null;
69
75
  ctx.clipPreview(null);
70
76
  ctx.session.setPreviewTransform(null);
71
77
  },
@@ -161,10 +167,14 @@ export const annotateTool = {
161
167
  }}></textarea>
162
168
  </label>
163
169
  ${colorField('annotate.color', hex(text.color), (c) => setStroke({ color: c }))}
164
- <label class="field">
170
+ ${
171
+ // A text background is a fill, so `fill: false` hides it like the shape fill.
172
+ options.fill
173
+ ? html `<label class="field">
165
174
  <span>${ctx.t('annotate.background')}</span>
166
175
  <input data-field="background" type="color" .value=${hex(text.background ?? [1, 1, 1, 1])} @change=${(e) => setText({ background: fromHex(e.target.value) })} />
167
- </label>
176
+ </label>`
177
+ : nothing}
168
178
  ${text.background
169
179
  ? html `<sealpixel-button variant="ghost" data-action="no-background" .label=${ctx.t('annotate.noBackground')} @click=${() => setText({ background: undefined })}>${ctx.t('annotate.noBackground')}</sealpixel-button>`
170
180
  : nothing}
@@ -223,10 +233,22 @@ export const annotateTool = {
223
233
  state.drawing.points.push(q);
224
234
  ctx.refresh();
225
235
  };
226
- const up = () => {
236
+ const cleanup = () => {
227
237
  window.removeEventListener('pointermove', move);
228
238
  window.removeEventListener('pointerup', up);
229
239
  window.removeEventListener('pointercancel', up);
240
+ window.removeEventListener('keydown', cancel);
241
+ };
242
+ // Escape while drawing drops the shape: nothing reaches the log.
243
+ const cancel = (ev) => {
244
+ if (ev.key !== 'Escape')
245
+ return;
246
+ state.drawing = null;
247
+ cleanup();
248
+ ctx.refresh();
249
+ };
250
+ const up = () => {
251
+ cleanup();
230
252
  const d = state.drawing;
231
253
  state.drawing = null;
232
254
  if (!d)
@@ -236,24 +258,28 @@ export const annotateTool = {
236
258
  const id = opId();
237
259
  ctx.dispatch({ type: 'annotate.add', id, shape });
238
260
  state.selected = id;
261
+ state.focusPending = id;
239
262
  }
240
263
  ctx.refresh();
241
264
  };
242
265
  window.addEventListener('pointermove', move);
243
266
  window.addEventListener('pointerup', up);
244
267
  window.addEventListener('pointercancel', up);
268
+ window.addEventListener('keydown', cancel);
245
269
  };
246
270
  const pick = (a) => (e) => {
247
271
  e.stopPropagation();
248
272
  e.preventDefault();
249
273
  state.selected = a.id;
274
+ // preventDefault above keeps the browser from focusing the shape.
275
+ e.currentTarget.focus({ preventScroll: true });
250
276
  const start = toN(e);
251
277
  state.drag = { id: a.id, shape: a.shape, start };
252
278
  const move = (ev) => {
253
279
  if (!state.drag)
254
280
  return;
255
281
  const q = toN(ev);
256
- state.drag.shape = translate(a.shape, q[0] - start[0], q[1] - start[1]);
282
+ state.drag.shape = translate(a.shape, q[0] - start[0], q[1] - start[1], W, H);
257
283
  ctx.refresh();
258
284
  };
259
285
  const up = () => {
@@ -272,10 +298,74 @@ export const annotateTool = {
272
298
  window.addEventListener('pointercancel', up);
273
299
  };
274
300
  const selected = doc.shapes.find((s) => s.id === state.selected) ?? null;
275
- const outline = (a) => {
276
- const dragging = state.drag?.id === a.id;
277
- const s = dragging ? state.drag?.shape : a.shape;
301
+ const shown = (a) => state.transform?.id === a.id
302
+ ? state.transform.shape
303
+ : state.drag?.id === a.id
304
+ ? state.drag.shape
305
+ : a.shape;
306
+ const outline = (a, i) => {
307
+ const dragging = state.drag?.id === a.id || state.transform?.id === a.id;
308
+ const s = shown(a);
278
309
  const selected = state.selected === a.id;
310
+ // The keyboard moves the shape through the same transient drag as the
311
+ // pointer and commits one update when the key is released.
312
+ const commit = () => {
313
+ const d = state.drag;
314
+ if (!d?.keys || d.id !== a.id)
315
+ return;
316
+ state.drag = null;
317
+ if (JSON.stringify(d.shape) !== JSON.stringify(a.shape)) {
318
+ ctx.dispatch({ type: 'annotate.update', id: opId(), shapeId: a.id, shape: d.shape });
319
+ }
320
+ ctx.refresh();
321
+ };
322
+ const keys = elementKeys(ctx.stage, {
323
+ select: () => {
324
+ if (state.selected === a.id)
325
+ return;
326
+ state.selected = a.id;
327
+ ctx.refresh();
328
+ },
329
+ move: (dx, dy) => {
330
+ if (state.drawing || state.transform || (state.drag && !state.drag.keys))
331
+ return;
332
+ const base = state.drag?.id === a.id ? state.drag.shape : a.shape;
333
+ state.drag = {
334
+ id: a.id,
335
+ shape: translate(base, dx, dy, W, H),
336
+ start: [0, 0],
337
+ keys: true,
338
+ };
339
+ ctx.refresh();
340
+ },
341
+ commit,
342
+ remove: (el) => {
343
+ commit();
344
+ // Focus moves on before the shape goes, so Tab stays in the editor.
345
+ const next = doc.shapes[i + 1] ?? doc.shapes[i - 1] ?? null;
346
+ state.selected = next?.id ?? null;
347
+ state.focusPending = next?.id ?? null;
348
+ if (!next)
349
+ focusEditor(el);
350
+ ctx.dispatch({ type: 'annotate.remove', id: opId(), shapeId: a.id });
351
+ },
352
+ escape: () => {
353
+ // Mid-drawing the key belongs to the drawing: the canvas listener drops it.
354
+ if (state.drawing)
355
+ return false;
356
+ const open = state.selected !== null || !!state.drag?.keys;
357
+ if (state.drag?.keys)
358
+ state.drag = null;
359
+ state.selected = null;
360
+ ctx.refresh();
361
+ return open;
362
+ },
363
+ });
364
+ const label = ctx.t('annotate.shapeLabel', {
365
+ kind: ctx.t(`annotate.kind.${s.kind}`),
366
+ n: i + 1,
367
+ });
368
+ const pressed = selected ? 'true' : 'false';
279
369
  const stroke = selected ? 'var(--sealpixel-color-accent)' : 'rgba(255,255,255,0.7)';
280
370
  const dash = `${4 / zoom} ${3 / zoom}`;
281
371
  // Almost transparent fill keeps the interior grabbable without hiding the pixels.
@@ -287,20 +377,20 @@ export const annotateTool = {
287
377
  case 'rect':
288
378
  case 'text': {
289
379
  const r = s.rect;
290
- return svg `${ghost}<rect data-interactive data-shape=${a.id} x=${r.x * W} y=${r.y * H} width=${Math.max(r.width * W, 2)} height=${Math.max(r.height * H, 2)} fill=${fill} stroke=${stroke} stroke-width=${sw} stroke-dasharray=${dash} style="cursor:move" @pointerdown=${pick(a)} />`;
380
+ return svg `${ghost}<rect data-interactive data-shape=${a.id} tabindex="0" role="button" aria-pressed=${pressed} aria-label=${label} ${focusWhen(state, a.id)} @focus=${keys.focus} @blur=${keys.blur} @keydown=${keys.keydown} @keyup=${keys.keyup} x=${r.x * W} y=${r.y * H} width=${Math.max(r.width * W, 2)} height=${Math.max(r.height * H, 2)} transform=${turnOf(s, W, H)} fill=${fill} stroke=${stroke} stroke-width=${sw} stroke-dasharray=${dash} style="cursor:move" @pointerdown=${pick(a)} />`;
291
381
  }
292
382
  case 'ellipse': {
293
383
  const r = s.rect;
294
- return svg `${ghost}<ellipse data-interactive data-shape=${a.id} cx=${(r.x + r.width / 2) * W} cy=${(r.y + r.height / 2) * H} rx=${Math.max((r.width / 2) * W, 1)} ry=${Math.max((r.height / 2) * H, 1)} fill=${fill} stroke=${stroke} stroke-width=${sw} stroke-dasharray=${dash} style="cursor:move" @pointerdown=${pick(a)} />`;
384
+ return svg `${ghost}<ellipse data-interactive data-shape=${a.id} tabindex="0" role="button" aria-pressed=${pressed} aria-label=${label} ${focusWhen(state, a.id)} @focus=${keys.focus} @blur=${keys.blur} @keydown=${keys.keydown} @keyup=${keys.keyup} cx=${(r.x + r.width / 2) * W} cy=${(r.y + r.height / 2) * H} rx=${Math.max((r.width / 2) * W, 1)} ry=${Math.max((r.height / 2) * H, 1)} transform=${turnOf(s, W, H)} fill=${fill} stroke=${stroke} stroke-width=${sw} stroke-dasharray=${dash} style="cursor:move" @pointerdown=${pick(a)} />`;
295
385
  }
296
386
  case 'line':
297
387
  case 'arrow':
298
- return svg `${ghost}<line data-interactive data-shape=${a.id} x1=${s.from[0] * W} y1=${s.from[1] * H} x2=${s.to[0] * W} y2=${s.to[1] * H} stroke=${stroke} stroke-width=${grab} stroke-opacity="0.3" style="cursor:move" @pointerdown=${pick(a)} />`;
388
+ return svg `${ghost}<line data-interactive data-shape=${a.id} tabindex="0" role="button" aria-pressed=${pressed} aria-label=${label} ${focusWhen(state, a.id)} @focus=${keys.focus} @blur=${keys.blur} @keydown=${keys.keydown} @keyup=${keys.keyup} x1=${s.from[0] * W} y1=${s.from[1] * H} x2=${s.to[0] * W} y2=${s.to[1] * H} stroke=${stroke} stroke-width=${grab} stroke-opacity="0.3" style="cursor:move" @pointerdown=${pick(a)} />`;
299
389
  case 'path': {
300
390
  const d = s.points
301
391
  .map((p, i) => `${i === 0 ? 'M' : 'L'}${p[0] * W} ${p[1] * H}`)
302
392
  .join(' ');
303
- return svg `${ghost}<path data-interactive data-shape=${a.id} d=${d} fill="none" stroke=${stroke} stroke-width=${grab} stroke-opacity="0.3" style="cursor:move" @pointerdown=${pick(a)} />`;
393
+ return svg `${ghost}<path data-interactive data-shape=${a.id} tabindex="0" role="button" aria-pressed=${pressed} aria-label=${label} ${focusWhen(state, a.id)} @focus=${keys.focus} @blur=${keys.blur} @keydown=${keys.keydown} @keyup=${keys.keyup} d=${d} fill="none" stroke=${stroke} stroke-width=${grab} stroke-opacity="0.3" style="cursor:move" @pointerdown=${pick(a)} />`;
304
394
  }
305
395
  }
306
396
  };
@@ -311,11 +401,105 @@ export const annotateTool = {
311
401
  if (shape)
312
402
  live = preview(shape, W, H, sw);
313
403
  }
404
+ // Handles on the selected shape. A boxed shape (rect, ellipse, text) has
405
+ // eight to resize and one below to turn, through its frame; a line or an
406
+ // arrow has one at each end; a path scales and turns through its points.
407
+ const transformHandles = (a) => {
408
+ const s = shown(a);
409
+ const hr = 7 / zoom;
410
+ const track = (e, onMove) => {
411
+ e.stopPropagation();
412
+ e.preventDefault();
413
+ const start = ctx.stage.toImage(e.clientX, e.clientY);
414
+ state.transform = { id: a.id, origin: a.shape, shape: a.shape };
415
+ const move = (ev) => {
416
+ if (!state.transform)
417
+ return;
418
+ const next = onMove(ctx.stage.toImage(ev.clientX, ev.clientY), start);
419
+ if (next)
420
+ state.transform.shape = next;
421
+ ctx.refresh();
422
+ };
423
+ const up = () => {
424
+ window.removeEventListener('pointermove', move);
425
+ window.removeEventListener('pointerup', up);
426
+ window.removeEventListener('pointercancel', up);
427
+ const next = state.transform?.shape ?? a.shape;
428
+ state.transform = null;
429
+ if (JSON.stringify(next) !== JSON.stringify(a.shape)) {
430
+ ctx.dispatch({ type: 'annotate.update', id: opId(), shapeId: a.id, shape: next });
431
+ }
432
+ ctx.refresh();
433
+ };
434
+ window.addEventListener('pointermove', move);
435
+ window.addEventListener('pointerup', up);
436
+ window.addEventListener('pointercancel', up);
437
+ };
438
+ const origin = a.shape;
439
+ if (origin.kind === 'line' || origin.kind === 'arrow') {
440
+ const ends = s;
441
+ const end = (which) => (e) => track(e, (q) => ({
442
+ ...origin,
443
+ [which]: [clamp01(q.x / W), clamp01(q.y / H)],
444
+ }));
445
+ return svg `
446
+ <circle data-interactive data-handle="from" cx=${ends.from[0] * W} cy=${ends.from[1] * H} r=${hr} fill="white" stroke="var(--sealpixel-color-accent)" stroke-width=${sw} style="cursor:move" @pointerdown=${end('from')} />
447
+ <circle data-interactive data-handle="to" cx=${ends.to[0] * W} cy=${ends.to[1] * H} r=${hr} fill="white" stroke="var(--sealpixel-color-accent)" stroke-width=${sw} style="cursor:move" @pointerdown=${end('to')} />
448
+ `;
449
+ }
450
+ const from = frameOf(origin, W, H);
451
+ const frame = frameOf(s, W, H);
452
+ const c = corners(frame);
453
+ const mid = (p, q) => [(p[0] + q[0]) / 2, (p[1] + q[1]) / 2];
454
+ const [nw, ne, se, sw2] = c;
455
+ const at = [
456
+ ['nw', nw],
457
+ ['n', mid(nw, ne)],
458
+ ['ne', ne],
459
+ ['e', mid(ne, se)],
460
+ ['se', se],
461
+ ['s', mid(se, sw2)],
462
+ ['sw', sw2],
463
+ ['w', mid(sw2, nw)],
464
+ ];
465
+ const cos = Math.cos(frame.rotation);
466
+ const sin = Math.sin(frame.rotation);
467
+ const bottom = mid(se, sw2);
468
+ const stem = 28 / zoom;
469
+ const knob = [bottom[0] - sin * stem, bottom[1] + cos * stem];
470
+ const degrees = Math.round((frame.rotation * 180) / Math.PI);
471
+ const cursor = {
472
+ nw: 'nwse-resize',
473
+ se: 'nwse-resize',
474
+ ne: 'nesw-resize',
475
+ sw: 'nesw-resize',
476
+ n: 'ns-resize',
477
+ s: 'ns-resize',
478
+ e: 'ew-resize',
479
+ w: 'ew-resize',
480
+ };
481
+ const grab = (handle) => (e) => track(e, (q, start) => shapeInFrame(origin, from, resize(from, handle, q.x - start.x, q.y - start.y, 6 / zoom), W, H));
482
+ const turn = (e) => {
483
+ const start = ctx.stage.toImage(e.clientX, e.clientY);
484
+ const startAngle = Math.atan2(start.y - from.cy, start.x - from.cx);
485
+ track(e, (q) => shapeInFrame(origin, from, {
486
+ ...from,
487
+ rotation: snapAngle(from.rotation + Math.atan2(q.y - from.cy, q.x - from.cx) - startAngle),
488
+ }, W, H));
489
+ };
490
+ return svg `
491
+ <line x1=${bottom[0]} y1=${bottom[1]} x2=${knob[0]} y2=${knob[1]} stroke="var(--sealpixel-color-accent)" stroke-width=${sw} pointer-events="none" />
492
+ ${at.map(([id, p]) => svg `<circle data-interactive data-handle=${id} cx=${p[0]} cy=${p[1]} r=${hr} fill="white" stroke="var(--sealpixel-color-accent)" stroke-width=${sw} style=${`cursor:${cursor[id]}`} @pointerdown=${grab(id)} />`)}
493
+ <circle data-interactive data-handle="rotate" role="slider" aria-label=${ctx.t('annotate.rotate')} aria-valuemin="-180" aria-valuemax="180" aria-valuenow=${degrees} aria-valuetext=${`${degrees}°`} cx=${knob[0]} cy=${knob[1]} r=${hr * 1.15} fill="var(--sealpixel-color-accent)" stroke="white" stroke-width=${sw} style="cursor:grab" @pointerdown=${turn} />
494
+ `;
495
+ };
496
+ const options = ctx.options('annotate') ?? ANNOTATE_DEFAULT_OPTIONS;
314
497
  return svg `
315
498
  <rect data-interactive data-canvas x="0" y="0" width=${W} height=${H} fill="transparent" style="cursor:crosshair" @pointerdown=${down} />
316
499
  ${doc.shapes.map(outline)}
500
+ ${selected && options.transform && !state.drag ? transformHandles(selected) : nothing}
317
501
  ${selected
318
- ? deleteHandle(toPx(bboxOf(state.drag?.id === selected.id ? state.drag.shape : selected.shape), W, H), ctx.stage, ctx.t('annotate.delete'), () => {
502
+ ? deleteHandle(toPx(bboxOf(shown(selected)), W, H), ctx.stage, ctx.t('annotate.delete'), () => {
319
503
  state.selected = null;
320
504
  ctx.dispatch({ type: 'annotate.remove', id: opId(), shapeId: selected.id });
321
505
  })
@@ -324,6 +508,62 @@ export const annotateTool = {
324
508
  `;
325
509
  },
326
510
  };
511
+ /** The SVG transform that turns a boxed shape about its centre, or nothing. */
512
+ function turnOf(s, W, H) {
513
+ if ((s.kind !== 'rect' && s.kind !== 'ellipse' && s.kind !== 'text') || !s.rotation)
514
+ return nothing;
515
+ const cx = (s.rect.x + s.rect.width / 2) * W;
516
+ const cy = (s.rect.y + s.rect.height / 2) * H;
517
+ return `rotate(${(s.rotation * 180) / Math.PI} ${cx} ${cy})`;
518
+ }
519
+ /** A shape's frame in preview pixels: its rect and rotation, or the box of its points. */
520
+ function frameOf(s, W, H) {
521
+ if (s.kind === 'rect' || s.kind === 'ellipse' || s.kind === 'text') {
522
+ return {
523
+ cx: (s.rect.x + s.rect.width / 2) * W,
524
+ cy: (s.rect.y + s.rect.height / 2) * H,
525
+ width: s.rect.width * W,
526
+ height: s.rect.height * H,
527
+ rotation: s.rotation ?? 0,
528
+ };
529
+ }
530
+ const box = bboxOf(s);
531
+ return {
532
+ cx: (box.x + box.width / 2) * W,
533
+ cy: (box.y + box.height / 2) * H,
534
+ width: box.width * W,
535
+ height: box.height * H,
536
+ rotation: 0,
537
+ };
538
+ }
539
+ /**
540
+ * The shape a frame stands for. A boxed shape takes the frame's rect and its
541
+ * rotation, the field left out at zero so an unturned shape reads as before;
542
+ * a path maps its points from the old frame into the new one. Null when the
543
+ * result would leave the image: the last one that fit stays.
544
+ */
545
+ function shapeInFrame(origin, from, to, W, H) {
546
+ const inside = (p) => p[0] >= -1e-9 && p[0] <= 1 + 1e-9 && p[1] >= -1e-9 && p[1] <= 1 + 1e-9;
547
+ if (origin.kind === 'rect' || origin.kind === 'ellipse' || origin.kind === 'text') {
548
+ if (!corners(to).every((p) => inside([p[0] / W, p[1] / H])))
549
+ return null;
550
+ const { rotation: _dropped, ...rest } = origin;
551
+ const rect = {
552
+ x: (to.cx - to.width / 2) / W,
553
+ y: (to.cy - to.height / 2) / H,
554
+ width: to.width / W,
555
+ height: to.height / H,
556
+ };
557
+ return to.rotation === 0 ? { ...rest, rect } : { ...rest, rect, rotation: to.rotation };
558
+ }
559
+ const src = origin.kind === 'path' ? origin.points : [origin.from, origin.to];
560
+ const mapped = mapPoints(src.map((p) => [p[0] * W, p[1] * H]), from, to).map((p) => [p[0] / W, p[1] / H]);
561
+ if (!mapped.every(inside))
562
+ return null;
563
+ if (origin.kind === 'path')
564
+ return { ...origin, points: mapped };
565
+ return { ...origin, from: mapped[0], to: mapped[1] };
566
+ }
327
567
  function bboxOf(s) {
328
568
  switch (s.kind) {
329
569
  case 'rect':
@@ -379,37 +619,46 @@ function shapeFromDrawing(d, kind, ctx) {
379
619
  return d.points.length < 2 ? null : { kind: 'path', points: d.points, stroke };
380
620
  case 'text': {
381
621
  const src = ctx.state.source;
382
- const size = Math.max(12, Math.round((src?.height ?? 400) / 20));
622
+ const options = ctx.options('annotate') ?? ANNOTATE_DEFAULT_OPTIONS;
623
+ const range = options.sizeRange;
624
+ const size = Math.min(range.max, Math.max(range.min, Math.max(12, Math.round((src?.height ?? 400) / 20))));
383
625
  return {
384
626
  kind: 'text',
385
627
  rect: { x, y, width: Math.max(width, 0.3), height: Math.max(height, 0.1) },
386
- text: ctx.t('annotate.defaultText'),
387
- font: 'font.roboto',
628
+ text: options.defaultText || ctx.t('annotate.defaultText'),
629
+ // The first font is the default, as the options document it.
630
+ font: options.fonts[0]?.id ?? 'font.roboto',
388
631
  size,
389
632
  color: state.stroke,
390
633
  };
391
634
  }
392
635
  }
393
636
  }
394
- function translate(s, dx, dy) {
395
- const mv = (p) => [clamp01(p[0] + dx), clamp01(p[1] + dy)];
637
+ /**
638
+ * The shape moved by `dx`, `dy` in unit space, as one body: the move stops
639
+ * where the shape reaches the edge, so nothing is pinned and nothing changes
640
+ * form. A turned box is bounded by its corners, not by its rect.
641
+ */
642
+ function translate(s, dx, dy, W, H) {
396
643
  switch (s.kind) {
397
644
  case 'rect':
398
645
  case 'ellipse':
399
- case 'text':
646
+ case 'text': {
647
+ const box = corners(frameOf(s, W, H)).map((p) => [p[0] / W, p[1] / H]);
648
+ const [moved] = slide(box, dx, dy);
649
+ const [first] = box;
400
650
  return {
401
651
  ...s,
402
- rect: {
403
- ...s.rect,
404
- x: clamp(s.rect.x + dx, 0, 1 - s.rect.width),
405
- y: clamp(s.rect.y + dy, 0, 1 - s.rect.height),
406
- },
652
+ rect: { ...s.rect, x: s.rect.x + moved[0] - first[0], y: s.rect.y + moved[1] - first[1] },
407
653
  };
654
+ }
408
655
  case 'line':
409
- case 'arrow':
410
- return { ...s, from: mv(s.from), to: mv(s.to) };
656
+ case 'arrow': {
657
+ const [from, to] = slide([s.from, s.to], dx, dy);
658
+ return { ...s, from, to };
659
+ }
411
660
  case 'path':
412
- return { ...s, points: s.points.map(mv) };
661
+ return { ...s, points: slide(s.points, dx, dy) };
413
662
  }
414
663
  }
415
664
  function preview(s, W, H, sw) {
@@ -417,9 +666,9 @@ function preview(s, W, H, sw) {
417
666
  switch (s.kind) {
418
667
  case 'rect':
419
668
  case 'text':
420
- return svg `<rect x=${s.rect.x * W} y=${s.rect.y * H} width=${s.rect.width * W} height=${s.rect.height * H} fill="rgba(91,141,239,0.15)" stroke=${accent} stroke-width=${sw} />`;
669
+ return svg `<rect x=${s.rect.x * W} y=${s.rect.y * H} width=${s.rect.width * W} height=${s.rect.height * H} transform=${turnOf(s, W, H)} fill="rgba(91,141,239,0.15)" stroke=${accent} stroke-width=${sw} />`;
421
670
  case 'ellipse':
422
- return svg `<ellipse cx=${(s.rect.x + s.rect.width / 2) * W} cy=${(s.rect.y + s.rect.height / 2) * H} rx=${(s.rect.width / 2) * W} ry=${(s.rect.height / 2) * H} fill="rgba(91,141,239,0.15)" stroke=${accent} stroke-width=${sw} />`;
671
+ return svg `<ellipse cx=${(s.rect.x + s.rect.width / 2) * W} cy=${(s.rect.y + s.rect.height / 2) * H} rx=${(s.rect.width / 2) * W} ry=${(s.rect.height / 2) * H} transform=${turnOf(s, W, H)} fill="rgba(91,141,239,0.15)" stroke=${accent} stroke-width=${sw} />`;
423
672
  case 'line':
424
673
  case 'arrow':
425
674
  return svg `<line x1=${s.from[0] * W} y1=${s.from[1] * H} x2=${s.to[0] * W} y2=${s.to[1] * H} stroke=${accent} stroke-width=${sw * 2} />`;