@nice2dev/ui-graphics 1.2.2 → 1.3.1

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.
@@ -92,8 +92,12 @@ function useVectorEditor({ artW, artH, onSaveToLibrary }) {
92
92
  if (!r) {
93
93
  return { x: 0, y: 0 };
94
94
  }
95
- let x = (cx - r.left - r.width / 2 - px) / zoom + artW / 2;
96
- let y = (cy - r.top - r.height / 2 - py) / zoom + artH / 2;
95
+ // The <svg> fills the box and its viewBox fits the artboard into it (xMidYMid meet), so one
96
+ // art unit is `fit` screen px before the zoom. Dividing by the zoom alone put every shape
97
+ // closer to the centre than the pointer — off by half in the 900×520 showcase demo.
98
+ const fit = Math.min(r.width / artW, r.height / artH) || 1;
99
+ let x = (cx - r.left - r.width / 2 - px) / (zoom * fit) + artW / 2;
100
+ let y = (cy - r.top - r.height / 2 - py) / (zoom * fit) + artH / 2;
97
101
  if (snapGrid) {
98
102
  x = snapToGrid(x, gridSize);
99
103
  y = snapToGrid(y, gridSize);
@@ -331,32 +335,37 @@ function useVectorEditor({ artW, artH, onSaveToLibrary }) {
331
335
  }));
332
336
  return;
333
337
  }
334
- if (!pendingShape.current) {
338
+ const pending = pendingShape.current;
339
+ if (!pending) {
335
340
  return;
336
341
  }
337
- const t = pendingShape.current.type;
342
+ // The pending shape's ref follows the drag as well. onUp sizes the result from the ref,
343
+ // which kept the 0×0 shape of the press — so every dragged rectangle, ellipse, line and
344
+ // arrow counted as "tiny" and was deleted on release: nothing appeared on the canvas.
345
+ const resize = (patch) => {
346
+ pendingShape.current = { ...pending, ...patch };
347
+ setShapes((prev) => prev.map((s) => (s.id === pending.id ? { ...s, ...patch } : s)));
348
+ };
349
+ const t = pending.type;
338
350
  if (t === 'polygon' || t === 'star') {
339
351
  const cx = startPt.current.x;
340
352
  const cy = startPt.current.y;
341
353
  const dx = p.x - cx;
342
354
  const dy = p.y - cy;
343
355
  const r2 = Math.sqrt(dx * dx + dy * dy);
344
- setShapes((prev) => prev.map((s) => s.id === pendingShape.current.id
345
- ? { ...s, x: cx - r2, y: cy - r2, w: r2 * 2, h: r2 * 2 }
346
- : s));
356
+ resize({ x: cx - r2, y: cy - r2, w: r2 * 2, h: r2 * 2 });
347
357
  return;
348
358
  }
349
359
  if (t === 'rect' || t === 'roundrect' || t === 'ellipse') {
350
- const x1 = Math.min(startPt.current.x, p.x);
351
- const y1 = Math.min(startPt.current.y, p.y);
352
- const w2 = Math.abs(p.x - startPt.current.x);
353
- const h2 = Math.abs(p.y - startPt.current.y);
354
- setShapes((prev) => prev.map((s) => s.id === pendingShape.current.id ? { ...s, x: x1, y: y1, w: w2, h: h2 } : s));
360
+ resize({
361
+ x: Math.min(startPt.current.x, p.x),
362
+ y: Math.min(startPt.current.y, p.y),
363
+ w: Math.abs(p.x - startPt.current.x),
364
+ h: Math.abs(p.y - startPt.current.y),
365
+ });
355
366
  }
356
367
  else if (t === 'line' || t === 'arrow') {
357
- setShapes((prev) => prev.map((s) => s.id === pendingShape.current.id
358
- ? { ...s, w: Math.abs(p.x - s.x), h: Math.abs(p.y - s.y), x2: p.x, y2: p.y }
359
- : s));
368
+ resize({ w: Math.abs(p.x - pending.x), h: Math.abs(p.y - pending.y), x2: p.x, y2: p.y });
360
369
  }
361
370
  }, [tool, scr2art, selId]);
362
371
  const onUp = useCallback(() => {
@@ -406,7 +415,8 @@ function useVectorEditor({ artW, artH, onSaveToLibrary }) {
406
415
  dragStarted.current = false;
407
416
  return;
408
417
  }
409
- snap(shapesRef.current);
418
+ // the ref holds the final geometry even when the last move has not rendered yet
419
+ snap(shapesRef.current.map((s) => (s.id === ps.id ? ps : s)));
410
420
  setSelId(ps.id);
411
421
  pendingShape.current = null;
412
422
  dragStarted.current = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nice2dev/ui-graphics",
3
- "version": "1.2.2",
3
+ "version": "1.3.1",
4
4
  "description": "Nice2Dev Graphics Editors — Pixel, Vector, Photo and Animation editors for React",
5
5
  "author": "NiceToDev <contact@nicetodev.com>",
6
6
  "license": "SEE LICENSE IN LICENSE",
@@ -64,15 +64,15 @@
64
64
  "nicetodev"
65
65
  ],
66
66
  "peerDependencies": {
67
- "@nice2dev/ui-core": "^1.2.0",
67
+ "@nice2dev/ui-core": "^1.3.0",
68
68
  "@nice2dev/ui": "^1.2.0",
69
69
  "framer-motion": ">=10.0.0",
70
70
  "react": ">=17.0.0",
71
71
  "react-dom": ">=17.0.0"
72
72
  },
73
73
  "devDependencies": {
74
- "@nice2dev/ui": "^1.2.2",
75
- "@nice2dev/ui-core": "^1.2.2",
74
+ "@nice2dev/ui": "^1.3.1",
75
+ "@nice2dev/ui-core": "^1.3.1",
76
76
  "@rollup/plugin-commonjs": "^29.0.3",
77
77
  "@rollup/plugin-node-resolve": "^16.0.3",
78
78
  "@rollup/plugin-typescript": "^12.3.0",