Files
odysseus/static/js/editor/layer-hit-test.js
T
pewdiepie-archdaemonandClaude Opus 5.5 5fac693c1c feat(photo-editor): Ajax Image Editor client (Photoshop-parity tools)
Photoshop-parity editor client: layers, selection/masking, brushes,
smart filters, LUT import, Quick Edit, mobile gestures, workspaces,
batch operations, and the service-worker versioning (v978->v1006).

Source: vendor/photocraft (ad1cff44) photo-editor client files, merged
onto upstream/dev (a8c147b2). Conflict resolution documented in
pr_work/A_conflicts.md; editor files taken from vendor/photocraft
(upstream's editor is an earlier snapshot of the same feature).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 11:57:35 +00:00

32 lines
1.3 KiB
JavaScript

// Auto-Select hit-testing (Move tool). Walks the layer stack front-to-back
// and returns the topmost layer whose rendered pixels are opaque at the
// requested document point. Kept dependency-free so it can run under node
// for unit tests; the caller injects layer-geometry and pixel sampling.
export const AUTO_SELECT_ALPHA_THRESHOLD = 8;
export function pickTopmostLayerAt({
layers,
point,
isHidden = () => false,
isLocked = () => false,
getOffset = () => ({ x: 0, y: 0 }),
sampleAlpha,
} = {}) {
if (!Array.isArray(layers) || !point || typeof sampleAlpha !== 'function') return null;
// Front-to-back: the last layer in the array paints on top.
for (let index = layers.length - 1; index >= 0; index -= 1) {
const layer = layers[index];
if (!layer?.visible || layer.kind === 'adjustment' || !layer.canvas) continue;
if (isHidden(layer)) continue;
if (isLocked(layer)) continue;
const offset = getOffset(layer) || { x: 0, y: 0 };
const x = Math.floor(point.x - offset.x);
const y = Math.floor(point.y - offset.y);
if (x < 0 || y < 0 || x >= layer.canvas.width || y >= layer.canvas.height) continue;
const alpha = sampleAlpha(layer, x, y);
if (alpha != null && alpha > AUTO_SELECT_ALPHA_THRESHOLD) return layer;
}
return null;
}