Give Each Photo Placement Its Own Crop in JavaScript

Give Each Photo Placement Its Own Crop in JavaScript

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.

设备检查表需要同一张照片的两个视图。一个单元格保持整个控制面板在画面中。另一个单元格则框选右上角的一个划痕仪表。如果两个单元格都从以照片为键的记录中读取裁剪信息,那么缩小细节裁剪范围也会改变概览视图。本文由人工智能为 LensUp 关联账户撰写,并由另一人工智能进行编辑。

Store the crop on the slot 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.

将裁剪信息存储在插槽上。File 对象使 JavaScript 能够访问照片的字节和文件信息。两个放置位置都可以引用同一个 File 作为其源资产(参考 MDN File 文档)。为每个插槽提供一个唯一的 slotId,并在每次放置照片时创建一个新的嵌套裁剪对象。两个裁剪初始值相同。使用插槽 ID 来选择记录,即使数组顺序发生变化,也能确保编辑内容关联到正确的放置位置。调用者的现有 UI 会提供唯一的插槽 ID,并在调用 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.

矩形从 x 和 y 开始,从图像的左上角测量。水平距离是图像宽度的分数,垂直距离是图像高度的分数。有效的裁剪必须具有非负偏移量、正尺寸,且右边缘和底边缘不超过 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);

Check what the update changes 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.

检查更新后的变化。在 updated 中,细节裁剪从图像横向 62%、纵向 12% 的位置开始,跨度为图像宽高的 28%。概览矩形仍然覆盖整张照片,且两个放置位置都指向同一个 File。输入数组保留了两个带有全图裁剪的原始记录。要检查是否仅替换了细节记录,可以比较对象标识。updated[0] === placements[0] 应为 true,而 updated[1] === placements[1] 应为 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.

对象展开(spread)仅复制一层。内部展开将四个裁剪值复制到一个新对象中,因此后续对 draftCrop.x 的更改不会影响已存储的值(参考 MDN 对象展开文档)。返回的数组包含放置元数据。请将两个数组中的记录视为只读,因为更新操作重用了概览记录。进行下一次编辑时,请将 updated 作为输入传递。如果您稍后修剪概览,第二次编辑将保留已保存的仪表裁剪信息。