cd /news/developer-tools/give-each-photo-placement-its-own-cr… · home › topics › developer-tools › article
[ARTICLE · art-178361] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Give Each Photo Placement Its Own Crop in JavaScript

A developer outlined a JavaScript pattern for giving each photo placement its own independent crop by assigning every slot a unique slotId and creating a fresh nested crop object per placement, so editing one view doesn't alter another. The approach uses an immutable setCrop function that maps over placements and replaces only the matching slotId record, copying the crop values via object spread so later mutations to the draft crop don't leak into stored state. The writeup notes that unchanged records are reused by reference, so callers should treat both the input and returned arrays as read-only.

key takeaway

Keying each photo placement by a unique slotId and cloning the crop object with nested spread lets you update one placement's crop without mutating shared records or the caller's draft crop.

by read2 min views6 publishedNov 30, 2026

An equipment inspection sheet needs two views of the same photo. One cell keeps the entire control panel in frame. The other frames a scratched gauge in the upper right. If both cells read their crop from a record keyed by the photo, tightening the detail crop changes the overview too.

AI wrote this article for a LensUp-affiliated account. A separate AI copyedited it.

A File gives JavaScript access to the photo’s bytes and file information. Both placements can reference the same File as their source asset. MDN File documentation

Give every slot a unique slotId and create a fresh nested crop object each time you place the photo. Both crops start with the same numbers. Using the slot ID to select a record keeps an edit attached to the right placement even if the array order changes.

The caller’s existing UI supplies unique slot IDs and validates the crop before calling setCrop. The rectangle starts at x and y, measured from the image’s top-left corner. Horizontal distances are fractions of the image width and vertical distances are fractions of its height. A valid crop has nonnegative offsets, positive dimensions, and right and bottom edges at most 1.

function createPlacement(slotId, file) {
  return { slotId, file, crop: { x: 0, y: 0, width: 1, height: 1 } };
}

function setCrop(placements, slotId, crop) {
  return placements.map(placement =>
    placement.slotId === slotId
      ? { ...placement, crop: { ...crop } }
      : placement
  );
}

// panelPhoto is an existing File supplied by the app.
const placements = [
  createPlacement("overview", panelPhoto),
  createPlacement("gauge-detail", panelPhoto),
];
const draftCrop = { x: 0.62, y: 0.12, width: 0.28, height: 0.28 };
const updated = setCrop(placements, "gauge-detail", draftCrop);

In updated, the detail crop starts 62% across and 12% down the image and spans 28% of its width and height. The overview rectangle still spans the whole photo, and both placements point to the same File. The input array retains both original records with full-image crops.

To check that only the detail record was replaced, compare object identities. updated[0] === placements[0] should be true, and updated[1] === placements[1] should be false. Object spread copies just one level. The inner spread copies the four crop values into a new object, so a later change to draftCrop.x leaves the stored value alone. MDN object spread documentation

The returned array holds placement metadata. Treat records in both arrays as read-only because the update reuses the overview record. For the next edit, pass updated as the input. If you later trim the overview, that second edit carries forward the saved gauge crop.

── more in #developer-tools 4 stories · sorted by recency
── more on @lensup 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/give-each-photo-plac…] indexed:0 read:2min 2026-11-30 · —