NoriginMedia / Norigin-Spatial-Navigation

React Hooks based Spatial Navigation (Key & Remote Control Navigation) / Web Browsers, Smart TVs and Connected TVs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong decision on distance measurement SpatialNavigation.ts : getSecondaryAxisDistance()

a-roz opened this issue · comments

SpatialNavigation.ts

has getSecondaryAxisDistance() , which has logically incorrect behavior when measure distance between Ref an Sibling
it's done by measure on all corners and leads to wrong choose in case of elements of a VARIOUS WIDTH / HEIGHT

To correct this, have to use CENTER coordinate between corners and measure the distance between them

so instead of

    const distancesToCompare = [];

    distancesToCompare.push(Math.abs(siblingCoordinateA - refCoordinateA));
    distancesToCompare.push(Math.abs(siblingCoordinateA - refCoordinateB));
    distancesToCompare.push(Math.abs(siblingCoordinateB - refCoordinateA));
    distancesToCompare.push(Math.abs(siblingCoordinateB - refCoordinateB));

    return Math.min(...distancesToCompare);

simply to put

    const refCoordinateCenter = (refCoordinateA + refCoordinateB) / 2;
    const siblingCoordinateCenter = (siblingCoordinateA + siblingCoordinateB) / 2;
    return Math.abs(refCoordinateCenter - siblingCoordinateCenter)

and it works perfect

commented

@a-roz: Hello!

You're quite right, the current comparison can be somewhat incorrect due to the way it compares using the corners.

We're discussing making it possible to define this in some way, for example by making it possible to overwrite this distance comparison function, so that you can define the origins you want to compare yourself.

The current approach is more performant for uniformly sized elements, so I think having some option for some customization would be good here, for those who could benefit from it.

Thanks for the feedback. We'll get back to this topic over the summer.

Update: The PR with the changes has been successfully merged. I'll now proceed to close the related issue. 🎉

Thank you for your valuable feedback, @a-roz!

You can view the PR here: PR #138