Skip to content

Commit 0e32253

Browse files
committed
fix(canvas): snap whole-line drag and guard image div-by-zero
LineObject's whole-line onDragMove only updated dragDelta with the raw cursor delta — the endpoint handles in the same component snap, and the rest of the canvas snaps too, so the inconsistency was visible. Mirror the endpoint idiom: round-trip pixel → dot → snap → pixel, then update both Konva position and the React state. ImageObject divided cached.height by cached.width to compute aspect ratio. A 0-width image would yield NaN. Guard with width > 0; falls back to a square placeholder.
1 parent a783674 commit 0e32253

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎src/components/Canvas/ImageObject.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ export function ImageObject({
2727
const p = obj.props;
2828
const cached = getImage(p.imageId);
2929
const w = dotsToPx(p.widthDots, scale, dpmm);
30-
const h = cached ? w * (cached.height / cached.width) : w;
30+
// Guard against a 0-width cached image: the imageCache pipeline
31+
// doesn't normally produce one, but a malformed file could leak
32+
// through and div-by-zero would render NaN-sized canvas nodes.
33+
const h = cached && cached.width > 0
34+
? w * (cached.height / cached.width)
35+
: w;
3136
const x = offsetX + dotsToPx(obj.x, scale, dpmm);
3237
const y = offsetY + dotsToPx(obj.y, scale, dpmm);
3338

‎src/components/Canvas/LineObject.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,15 @@ export function LineObject({
8686
}
8787
onTap={() => onSelect(false)}
8888
onDragMove={(e) => {
89-
setDragDelta({ x: e.target.x(), y: e.target.y() });
89+
// Snap the whole-line delta in dot space, then mirror back
90+
// to pixels — same idiom the endpoint handles use, and
91+
// keeps the line consistent with shape/text/image drag.
92+
const deltaXPx =
93+
dotsToPx(snap(pxToDots(e.target.x(), scale, dpmm)), scale, dpmm);
94+
const deltaYPx =
95+
dotsToPx(snap(pxToDots(e.target.y(), scale, dpmm)), scale, dpmm);
96+
e.target.position({ x: deltaXPx, y: deltaYPx });
97+
setDragDelta({ x: deltaXPx, y: deltaYPx });
9098
}}
9199
onDragEnd={(e) => {
92100
const deltaXPx = e.target.x();

0 commit comments

Comments
 (0)