Ardaosta Claude Sonnet 4.6 commited on
Commit
fc7788d
·
1 Parent(s): b16e4fc

Fix layer drag: stop rebuilding DOM during drag and select

Browse files

renderStageLayers() was being called on every selectLayer, every pointermove,
and every refreshLayerMask, which destroyed the DOM node that had pointer
capture, killing the drag mid-stream. Split into renderStageLayers (full
rebuild, only when layer set changes) plus lightweight updateLayerTransforms,
updateLayerSelection, and updateLayerMaskImage helpers that mutate existing
nodes in place.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. static/app.js +54 -17
static/app.js CHANGED
@@ -133,7 +133,7 @@ async function refreshLayerMask(layer) {
133
  }
134
  const data = await res.json();
135
  layer.maskPng = data.mask_png;
136
- renderStageLayers();
137
  } catch (err) {
138
  setStatus(`Mask failed: ${err.message}`, true);
139
  }
@@ -171,7 +171,7 @@ function selectLayer(id) {
171
  state.selectedLayerId = id;
172
  renderLayerList();
173
  renderEditor();
174
- renderStageLayers();
175
  }
176
 
177
  // --- Rendering: layer list ---
@@ -255,7 +255,20 @@ function fitStageInner() {
255
  return { w, h };
256
  }
257
 
 
 
 
 
 
 
 
 
 
258
  function renderStageLayers() {
 
 
 
 
259
  const { w: stageW, h: stageH } = fitStageInner();
260
  const pxPerInX = stageW / state.panelWidthIn;
261
  const pxPerInY = stageH / state.panelHeightIn;
@@ -267,12 +280,7 @@ function renderStageLayers() {
267
  wrap.className = "layer-preview";
268
  if (layer.id === state.selectedLayerId) wrap.classList.add("selected");
269
  wrap.dataset.layerId = layer.id;
270
- const wPx = layer.widthIn * pxPerInX;
271
- const hPx = (layer.widthIn / layer.aspect) * pxPerInY;
272
- wrap.style.width = `${wPx}px`;
273
- wrap.style.height = `${hPx}px`;
274
- wrap.style.left = `${layer.xIn * pxPerInX - wPx / 2}px`;
275
- wrap.style.top = `${layer.yIn * pxPerInY - hPx / 2}px`;
276
  if (layer.maskPng) {
277
  const img = document.createElement("img");
278
  img.src = layer.maskPng;
@@ -282,19 +290,45 @@ function renderStageLayers() {
282
  handle.className = "handle";
283
  handle.dataset.role = "resize";
284
  wrap.appendChild(handle);
 
285
  els.layerPreviewWrap.appendChild(wrap);
286
  });
287
- attachLayerDragHandlers();
288
  }
289
 
290
- // --- Drag-to-move and resize ---
 
 
 
 
 
 
 
 
 
291
 
292
- function attachLayerDragHandlers() {
293
  els.layerPreviewWrap.querySelectorAll(".layer-preview").forEach((node) => {
294
- node.addEventListener("pointerdown", onLayerPointerDown);
295
  });
296
  }
297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
298
  let dragCtx = null;
299
  function onLayerPointerDown(e) {
300
  const node = e.currentTarget;
@@ -333,8 +367,11 @@ function onLayerPointerMove(e) {
333
  const proposed = dragCtx.startWidthIn + dx / dragCtx.pxPerInX;
334
  layer.widthIn = Math.max(0.5, proposed);
335
  }
336
- renderEditor();
337
- renderStageLayers();
 
 
 
338
  }
339
 
340
  function onLayerPointerUp(e) {
@@ -586,7 +623,7 @@ function scheduleLayerMaskRefresh(layer) {
586
  layer.widthIn = parseFloat(els.layerWidthIn.value) || layer.widthIn;
587
  layer.xIn = parseFloat(els.layerXIn.value) || layer.xIn;
588
  layer.yIn = parseFloat(els.layerYIn.value) || layer.yIn;
589
- renderStageLayers();
590
  });
591
  });
592
 
@@ -594,7 +631,7 @@ function scheduleLayerMaskRefresh(layer) {
594
  input.addEventListener("input", () => {
595
  state.panelWidthIn = parseFloat(els.panelWidthIn.value) || state.panelWidthIn;
596
  state.panelHeightIn = parseFloat(els.panelHeightIn.value) || state.panelHeightIn;
597
- renderStageLayers();
598
  updateDimsLabel();
599
  });
600
  });
@@ -630,7 +667,7 @@ els.composeBtn.addEventListener("click", runCompose);
630
  els.exportSvgBtn.addEventListener("click", () => exportFile("/export/svg", "plasma-design.svg"));
631
  els.exportDxfBtn.addEventListener("click", () => exportFile("/export/dxf", "plasma-design.dxf"));
632
 
633
- window.addEventListener("resize", () => renderStageLayers());
634
 
635
  function escapeHtml(s) {
636
  return String(s).replace(/[&<>"']/g, (c) => ({
 
133
  }
134
  const data = await res.json();
135
  layer.maskPng = data.mask_png;
136
+ updateLayerMaskImage(layer);
137
  } catch (err) {
138
  setStatus(`Mask failed: ${err.message}`, true);
139
  }
 
171
  state.selectedLayerId = id;
172
  renderLayerList();
173
  renderEditor();
174
+ updateLayerSelection();
175
  }
176
 
177
  // --- Rendering: layer list ---
 
255
  return { w, h };
256
  }
257
 
258
+ function applyLayerTransform(node, layer, pxPerInX, pxPerInY) {
259
+ const wPx = layer.widthIn * pxPerInX;
260
+ const hPx = (layer.widthIn / layer.aspect) * pxPerInY;
261
+ node.style.width = `${wPx}px`;
262
+ node.style.height = `${hPx}px`;
263
+ node.style.left = `${layer.xIn * pxPerInX - wPx / 2}px`;
264
+ node.style.top = `${layer.yIn * pxPerInY - hPx / 2}px`;
265
+ }
266
+
267
  function renderStageLayers() {
268
+ // Full rebuild — call only when the layer set or visibility changes.
269
+ // For position/scale updates during drag or input edits, use
270
+ // updateLayerTransforms() which leaves DOM nodes intact (otherwise we
271
+ // destroy the node we're dragging and the pointer capture dies with it).
272
  const { w: stageW, h: stageH } = fitStageInner();
273
  const pxPerInX = stageW / state.panelWidthIn;
274
  const pxPerInY = stageH / state.panelHeightIn;
 
280
  wrap.className = "layer-preview";
281
  if (layer.id === state.selectedLayerId) wrap.classList.add("selected");
282
  wrap.dataset.layerId = layer.id;
283
+ applyLayerTransform(wrap, layer, pxPerInX, pxPerInY);
 
 
 
 
 
284
  if (layer.maskPng) {
285
  const img = document.createElement("img");
286
  img.src = layer.maskPng;
 
290
  handle.className = "handle";
291
  handle.dataset.role = "resize";
292
  wrap.appendChild(handle);
293
+ wrap.addEventListener("pointerdown", onLayerPointerDown);
294
  els.layerPreviewWrap.appendChild(wrap);
295
  });
 
296
  }
297
 
298
+ function updateLayerTransforms() {
299
+ // Light update: reposition/resize existing nodes without rebuilding the DOM.
300
+ const { w: stageW, h: stageH } = fitStageInner();
301
+ const pxPerInX = stageW / state.panelWidthIn;
302
+ const pxPerInY = stageH / state.panelHeightIn;
303
+ els.layerPreviewWrap.querySelectorAll(".layer-preview").forEach((node) => {
304
+ const layer = state.layers.find((l) => l.id === node.dataset.layerId);
305
+ if (layer) applyLayerTransform(node, layer, pxPerInX, pxPerInY);
306
+ });
307
+ }
308
 
309
+ function updateLayerSelection() {
310
  els.layerPreviewWrap.querySelectorAll(".layer-preview").forEach((node) => {
311
+ node.classList.toggle("selected", node.dataset.layerId === state.selectedLayerId);
312
  });
313
  }
314
 
315
+ function updateLayerMaskImage(layer) {
316
+ const node = els.layerPreviewWrap.querySelector(
317
+ `.layer-preview[data-layer-id="${layer.id}"]`
318
+ );
319
+ if (!node) return;
320
+ let img = node.querySelector("img");
321
+ if (!layer.maskPng) {
322
+ if (img) img.remove();
323
+ return;
324
+ }
325
+ if (!img) {
326
+ img = document.createElement("img");
327
+ node.insertBefore(img, node.firstChild);
328
+ }
329
+ img.src = layer.maskPng;
330
+ }
331
+
332
  let dragCtx = null;
333
  function onLayerPointerDown(e) {
334
  const node = e.currentTarget;
 
367
  const proposed = dragCtx.startWidthIn + dx / dragCtx.pxPerInX;
368
  layer.widthIn = Math.max(0.5, proposed);
369
  }
370
+ // Update editor inputs and node styles without rebuilding the DOM.
371
+ els.layerWidthIn.value = layer.widthIn.toFixed(2);
372
+ els.layerXIn.value = layer.xIn.toFixed(2);
373
+ els.layerYIn.value = layer.yIn.toFixed(2);
374
+ updateLayerTransforms();
375
  }
376
 
377
  function onLayerPointerUp(e) {
 
623
  layer.widthIn = parseFloat(els.layerWidthIn.value) || layer.widthIn;
624
  layer.xIn = parseFloat(els.layerXIn.value) || layer.xIn;
625
  layer.yIn = parseFloat(els.layerYIn.value) || layer.yIn;
626
+ updateLayerTransforms();
627
  });
628
  });
629
 
 
631
  input.addEventListener("input", () => {
632
  state.panelWidthIn = parseFloat(els.panelWidthIn.value) || state.panelWidthIn;
633
  state.panelHeightIn = parseFloat(els.panelHeightIn.value) || state.panelHeightIn;
634
+ updateLayerTransforms();
635
  updateDimsLabel();
636
  });
637
  });
 
667
  els.exportSvgBtn.addEventListener("click", () => exportFile("/export/svg", "plasma-design.svg"));
668
  els.exportDxfBtn.addEventListener("click", () => exportFile("/export/dxf", "plasma-design.dxf"));
669
 
670
+ window.addEventListener("resize", () => updateLayerTransforms());
671
 
672
  function escapeHtml(s) {
673
  return String(s).replace(/[&<>"']/g, (c) => ({