Skip to content

Commit

Permalink
fix(2d): respect offset in absolute positions (#987)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarthificial committed Mar 11, 2024
1 parent 11ef7ea commit 491080d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/2d/src/lib/components/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ export class Node implements Promisable<Node> {
public declare readonly absolutePosition: SimpleVector2Signal<this>;

protected getAbsolutePosition(): Vector2 {
const matrix = this.localToWorld();
return new Vector2(matrix.m41, matrix.m42);
return new Vector2(this.parentToWorld().transformPoint(this.position()));
}

protected setAbsolutePosition(value: SignalValue<PossibleVector2>) {
Expand Down Expand Up @@ -600,6 +599,18 @@ export class Node implements Promisable<Node> {
return this.parent()?.worldToLocal() ?? new DOMMatrix();
}

/**
* Get the parent-to-world matrix for this node.
*
* @remarks
* This matrix transforms vectors from local space of this node's parent to
* world space.
*/
@computed()
public parentToWorld(): DOMMatrix {
return this.parent()?.localToWorld() ?? new DOMMatrix();
}

/**
* Get the local-to-parent matrix for this node.
*
Expand Down
5 changes: 5 additions & 0 deletions packages/docs/docs/getting-started/positioning.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ The available matrices include:
<ApiSnippet url={'/api/2d/components/Node#localToParent'} />
<hr />

### `Node.parentToWorld`

<ApiSnippet url={'/api/2d/components/Node#parentToWorld'} />
<hr />

### `Node.worldToParent`

<ApiSnippet url={'/api/2d/components/Node#worldToParent'} />
Expand Down

0 comments on commit 491080d

Please sign in to comment.