{"slug": "give-each-photo-placement-its-own-crop-in-javascript", "title": "Give Each Photo Placement Its Own Crop in JavaScript", "summary": "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.", "body_md": "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.\n\n*AI wrote this article for a LensUp-affiliated account. A separate AI copyedited it.*\n\nA `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)\n\nGive 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.\n\nThe 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`.\n\n```\nfunction createPlacement(slotId, file) {\n  return { slotId, file, crop: { x: 0, y: 0, width: 1, height: 1 } };\n}\n\nfunction setCrop(placements, slotId, crop) {\n  return placements.map(placement =>\n    placement.slotId === slotId\n      ? { ...placement, crop: { ...crop } }\n      : placement\n  );\n}\n\n// panelPhoto is an existing File supplied by the app.\nconst placements = [\n  createPlacement(\"overview\", panelPhoto),\n  createPlacement(\"gauge-detail\", panelPhoto),\n];\nconst draftCrop = { x: 0.62, y: 0.12, width: 0.28, height: 0.28 };\nconst updated = setCrop(placements, \"gauge-detail\", draftCrop);\n```\n\nIn `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.\n\nTo 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)\n\nThe 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.", "url": "https://wpnews.pro/news/give-each-photo-placement-its-own-crop-in-javascript", "canonical_source": "https://dev.to/yanwang/give-each-photo-placement-its-own-crop-in-javascript-5dk9", "published_at": "2026-11-30 02:34:12+00:00", "updated_at": "2026-11-30 02:45:43.473777+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["LensUp", "MDN"], "also_reported_by": [], "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.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/give-each-photo-placement-its-own-crop-in-javascript", "markdown": "https://wpnews.pro/news/give-each-photo-placement-its-own-crop-in-javascript.md", "text": "https://wpnews.pro/news/give-each-photo-placement-its-own-crop-in-javascript.txt", "jsonld": "https://wpnews.pro/news/give-each-photo-placement-its-own-crop-in-javascript.jsonld"}}