Skip to content

ySortBy

ySortBy(offsetOf): LayerSortFn

Defined in: renderer/src/ySort.ts:47

Y-depth key with a per-container offset, the way Godot’s y_sort_origin shifts a sprite’s apparent “footprint” for depth comparisons. Use when a sprite’s anchor is set to its top (so the visual base sits well below position.y) and the raw position.y produces wrong overlaps — a player whose feet are at the bottom of the sprite should pass behind a tree whose trunk is at the bottom of its own sprite, not be sorted by the headtop.

offsetOf(container) is called once per child per frame; cheap data (a getter / a property read) keeps the per-frame cost negligible. The key falls back to plain position.y when offsetOf returns undefined, so mixed-content layers work without every child having a depth offset.

import { ySortBy } from "@yagejs/renderer";
// Read the offset off a custom property (containers are extensible).
const sort = ySortBy((c) => (c as { depthOffset?: number }).depthOffset);
// Then per-sprite:
sprite.sprite.depthOffset = 32; // pivot for sort lives 32 px below `position.y`

(c) => number | undefined

LayerSortFn