# Give Each Photo Placement Its Own Crop in JavaScript

> Source: <https://dev.to/yanwang/give-each-photo-placement-its-own-crop-in-javascript-5dk9>
> Published: 2026-11-30 02:34:12+00:00

**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.

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](https://developer.mozilla.org/en-US/docs/Web/API/File)

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#spread_in_object_literals)

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.
