# core/node/worldnode (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/node/worldnode/

Hierarchical area object.

## WorldNode extends TransformNode

Hierarchical area object.
- Anchor, Pivot, ContentSize features.

Example: Place a rectangle inside a parent and test hits

```js
import { WorldNode, Vector2 } from "./build/vanilla.js";

const panel = new WorldNode();
panel.setPivot(Vector2.create(0, 0));
panel.setContentSize(Vector2.create(400, 300));
const button = new WorldNode();
button.setRect(20, 20, 120, 40);
button.setInteractable(true);
panel.addChild(button);
const corners = button.getWorldCorners();
console.log(corners[0].x, corners[0].y, corners[2].x, corners[2].y);
const touchPosition = Vector2.create(50, 30);
const isInside = button.contains(touchPosition);
const localPosition = button.convertViewToLocal(touchPosition);
console.log(isInside, localPosition.x, localPosition.y);
```

- `constructor()` (constructor, since 0.1.0): Constructor.
- `pushTransform(graphic)` (method, since 0.1.0): Begins the draw state.
  - `graphic` Graphic: Graphic to apply the transform to.
- `draw(graphic)` (method, since 0.2.1): Draw. (override: if a Mask component is attached, crops children to its area)
  - `graphic` Graphic: Graphic to draw with.
- `calculateDrawnRect(rect)` (method, since 0.4.0): Writes into rect the rectangle the node draws by itself (outside its components and children). (origin at the top-left of the content area, true if known)
  - A node that does not override draw draws nothing by itself, so it writes size 0 and returns true.
  - A node that overrides draw does not know where it draws and returns false. (its ancestors do not skip the subtree drawing)
    If the overriding node also overrides this function to report the drawn rectangle it can be skipped. Call markDrawBoundsDirty when the rectangle changes.
  - `rect` Rect: Where to write the drawn rectangle. (origin at the top-left of the node's content area)
  - returns boolean: True if known.

  Example: Report where a self-drawing node draws

  ```js
  import { WorldNode, Rect, Vector2, Colors } from "./build/vanilla.js";
  
  class Badge extends WorldNode {
  	draw(graphic) {
  		super.draw(graphic);
  		const contentSize = this.getContentSize();
  		graphic.setFillColor(Colors.white);
  		graphic.drawRect(Rect.create(contentSize.x - 6, -6, 12, 12));
  	}
  	calculateDrawnRect(rect) {
  		const contentSize = this.getContentSize();
  		rect.position.set(contentSize.x - 6, -6);
  		rect.size.set(12, 12);
  		return true;
  	}
  }
  
  const badge = new Badge();
  badge.setContentSize(Vector2.create(40, 40));
  const drawnRect = Rect.zero();
  console.log(badge.calculateDrawnRect(drawnRect), drawnRect.position.x);
  ```

- `transformDrawBoundsToParent(rect)` (method, since 0.4.0): Moves subtree draw bounds into the parent's content coordinates. (Override: by position, rotation, scale and pivot, in the same order as pushTransform)
  - A node that overrides pushTransform or the position, scale, rotation, pivot or content size getters has unknown placement and returns false. (The parent is not skipped; nodes below this one can still be skipped on their own)
  - `rect` Rect: The bounds to move. (In this node's content coordinates; the bounding box in the parent's content coordinates is written back)
  - returns boolean: True if where the node is placed is known.

  Example: Moving a child's draw bounds into parent coordinates

  ```js
  import { WorldNode, Rect, Vector2, Pivot } from "./build/vanilla.js";
  
  const node = new WorldNode();
  node.setPivot(Pivot.topLeft);
  node.setLocalPosition(Vector2.create(100, 20));
  node.setLocalScale(Vector2.create(2, 2));
  const drawBounds = Rect.create(0, 0, 10, 10);
  node.transformDrawBoundsToParent(drawBounds);
  console.log(drawBounds.position.x, drawBounds.size.x);
  ```

- `drawGizmos(graphic)` (method, since 0.1.0): Draw.
  - `graphic` Graphic: Graphic to draw with.
- `setPivot(pivot)` (method, since 0.1.0): Sets the pivot.
  - `pivot` Vector2: Pivot. (ratio of the content size, clamped to 0 ~ 1, (0, 0) is the top-left)
- `getPivot()` (method, since 0.1.0): Returns the pivot.
  - returns Vector2: A copy of the pivot.
- `readPivot(out)` (method, since 0.4.0): Writes the pivot into a given vector. (Same value as getPivot, no clone)
  - `out` Vector2: Vector to write.
  - returns Vector2: out.

  Example: Read the pivot

  ```js
  import { WorldNode, Vector2 } from "./vanilla.js";
  const node = new WorldNode();
  node.setPivot(Vector2.create(0.5, 1));
  const pivot = node.readPivot(Vector2.create(0, 0));
  if (pivot.x !== 0.5 || pivot.y !== 1) {
  	throw new Error("pivot failed");
  }
  ```

- `setContentSize(contentSize)` (method, since 0.1.0): Sets the actual content size.
  - `contentSize` Vector2: Content size. (values are copied)
- `setContentSizeXY(width, height)` (method, since 0.4.0): Sets the actual content size. (takes width, height directly, does not create a Vector2)
  - Subclasses that override setContentSize() are forwarded to setContentSize.
  - `width` number: Content width.
  - `height` number: Content height.
- `setRect(x, y, width, height)` (method, since 0.4.0): Places the node in a rectangle within the parent area. (x, y is this node's top-left, origin at the top-left of the parent content area)
  - Sets the content size to width, height and sets the local position so the top-left lands at (x, y) wherever the pivot is. (x + width * pivot.x, y + height * pivot.y)
  - Does not change the anchor. The local position is relative to the parent's top-left regardless of the anchor, so it is the same spot wherever the anchor is. (getAnchoredPosition recomputes it relative to that anchor)
  - Ignores local scale and rotation. (with scale 1 and rotation 0, it is that rectangle on screen too)
  - `x` number: Top-left X. (origin at the top-left of the parent's content area)
  - `y` number: Top-left Y. (origin at the top-left of the parent's content area)
  - `width` number: Content width.
  - `height` number: Content height.
- `getContentSize()` (method, since 0.1.0): Returns the actual content size.
  - If there is no parent (root node) and it was never set explicitly, returns the current view size.
  - returns Vector2: A copy of the content size.
- `readContentSize(out)` (method, since 0.4.0): Writes the content size into a given vector. (Same value as getContentSize, no clone)
  - `out` Vector2: Vector to write.
  - returns Vector2: out.

  Example: Read the content size

  ```js
  import { WorldNode, Vector2 } from "./vanilla.js";
  const node = new WorldNode();
  node.setContentSize(Vector2.create(120, 40));
  const contentSize = node.readContentSize(Vector2.create(0, 0));
  if (contentSize.x !== 120 || contentSize.y !== 40) {
  	throw new Error("content size failed");
  }
  ```

- `copyContentSizeTo(target)` (method, since 0.4.0): Writes the actual content size into target. (does not create a copy, used by Paint on every draw)
  - Subclasses that override getContentSize() write the value that function returns.
  - `target` Vector2: Vector that receives the content size.
- `setAnchor(anchor)` (method, since 0.1.0): Sets the anchor.
  - `anchor` Vector2: Anchor. (ratio of the parent's content size, clamped to 0 ~ 1)
- `getAnchor()` (method, since 0.1.0): Returns the anchor.
  - returns Vector2: The anchor. (the internal vector as is)
- `setAnchoredPosition(anchoredPosition)` (method, since 0.2.0): Sets the anchored position.
  - anchoredPosition: position relative to the anchor reference point (= parentContentSize * anchor) within the parent area.
  - Internally converted and stored as localPosition = anchorRef + anchoredPosition.
    (localPosition represents the coordinates of its own center point within the parent area regardless of anchor)
  - `anchoredPosition` Vector2: Position relative to the anchor. (used as the local position if the parent is not a WorldNode; does nothing if null)

  Example: Offset from the parent's top-right corner

  ```js
  import { WorldNode, Vector2 } from "./build/vanilla.js";
  
  const parent = new WorldNode();
  parent.setContentSize(Vector2.create(800, 600));
  const closeButton = new WorldNode();
  parent.addChild(closeButton);
  closeButton.setAnchor(Vector2.create(1, 0));
  closeButton.setAnchoredPosition(Vector2.create(-50, 40));
  const localPosition = closeButton.getLocalPosition();
  const anchoredPosition = closeButton.getAnchoredPosition();
  console.log(localPosition.x, localPosition.y, anchoredPosition.x, anchoredPosition.y);
  ```

- `getAnchoredPosition()` (method, since 0.2.0): Returns the anchored position.
  - Computed back as localPosition - anchorRef.
  - returns Vector2: Position relative to the anchor. (the local position if the parent is not a WorldNode)
- `getPosition()` (method, since 0.1.0): Returns the global position.
  - localPosition is the object's own center coordinate relative to the parent content top-left.
  - The parent's pivot point is at (parentContentSize * parentPivot), so
    it is converted into an offset from parent.getPosition() (= world coordinates of the parent pivot point) and then
    the parent rotation/scale is applied. (anchor has no direct effect on the position calculation)
  - returns Vector2: World position. (position of the pivot point)
- `readPosition(out)` (method, since 0.4.0): Writes the global position into a given vector. (Same arithmetic in the same order as getPosition, no new vector)
  - Subclasses that override getPosition or the parent's content size or pivot readers are read through those functions.
  - `out` Vector2: Vector to write.
  - returns Vector2: out. (Position of the pivot)
- `setPosition(position)` (method, since 0.4.0): Sets the global position. (inverse of getPosition)
  - Takes the difference seen from parent.getPosition() (= world coordinates of the parent pivot point), undoes the parent rotation/scale to get back the offset from the parent pivot point, then
    moves it to the parent content top-left basis (localPosition). On axes where the parent scale is 0, it is placed at the parent pivot point.
  - `position` Vector2: World position. (position of the pivot point, converted to the local position by undoing the parent's rotation and scale)
- `getWorldCorners()` (method, since 0.1.0): Returns the area actually drawn on screen. (OBB)
  - returns Vector2[]: The four corners. (top-left, top-right, bottom-right, bottom-left, in view coordinates)
- `getWorldBounds()` (method, since 0.1.0): Forms a bounding box from the min and max positions based on getWorldCorners().
  - returns Rect: Axis-aligned rectangle enclosing the four corners.
- `contains(viewPosition)` (method, since 0.1.0): Collision detection via getWorldCorners().
  - `viewPosition` Vector2: View coordinates to test.
  - returns boolean: True if inside the four corners. (false if null)
- `convertViewToLocal(viewPosition)` (method, since 0.4.0): View coordinates → this node's local coordinates. (origin at the top-left of the content area, bottom-right is the content size)
  - Computed with the four corners (getWorldCorners) after all parents' position, scale and rotation, so it is the same test as contains.
  - Returns 0 on axes where the content size is 0.
  - `viewPosition` Vector2: View coordinates to convert.
  - returns Vector2: Local coordinates. (origin at the top-left of the content area)
- `convertNodePositionToViewPosition(nodePosition)` (method, since 0.4.0): A point inside the node area → view coordinates. (The top left of the content area is the origin, goes through all parent transforms, Rust edition NodeTree::convert_node_position_to_view_position)
  - `nodePosition` Vector2: A point in this node's content coordinates.
  - returns Vector2: A point in view coordinates.

  Example: View position of a button's bottom right corner

  ```js
  import { Vector2, WorldNode } from "./build/vanilla.js";
  
  const rootNode = new WorldNode();
  const buttonNode = new WorldNode();
  rootNode.addChild(buttonNode);
  buttonNode.setLocalPosition(Vector2.create(100, 100));
  buttonNode.setContentSize(Vector2.create(40, 20));
  const corner = buttonNode.convertNodePositionToViewPosition(Vector2.create(40, 20));
  console.log(corner.x, corner.y);
  ```

- `convertViewPositionToNodePosition(viewPosition)` (method, since 0.4.0): View coordinates → a point inside the node area. (null if the matrix cannot be inverted (scale 0), Rust edition NodeTree::convert_view_position_to_node_position)
  - `viewPosition` Vector2: A point in view coordinates.
  - returns Vector2 | null: A point in this node's content coordinates. (null if the scale is 0 and it cannot be inverted)

  Example: Converting a view position to a position inside the node and back

  ```js
  import { Vector2, WorldNode } from "./build/vanilla.js";
  
  const rootNode = new WorldNode();
  const panelNode = new WorldNode();
  rootNode.addChild(panelNode);
  panelNode.setLocalPosition(Vector2.create(200, 100));
  panelNode.setContentSize(Vector2.create(100, 50));
  panelNode.setLocalScale(Vector2.create(2, 2));
  const nodePosition = panelNode.convertViewPositionToNodePosition(Vector2.create(200, 100));
  console.log(nodePosition.x, nodePosition.y);
  panelNode.setLocalScale(Vector2.create(0, 1));
  console.log(panelNode.convertViewPositionToNodePosition(Vector2.create(200, 100)));
  ```

- `setInteractable(isInteractable)` (method, since 0.2.0): Sets whether touch interaction is enabled.
  - `isInteractable` boolean: If true, TouchRaycaster can hit this node.
- `isInteractable()` (method, since 0.2.0): Returns whether touch interaction is enabled.
  - returns boolean: True if interactable.
- `touchPress(viewInputPosition)` (method, since 0.2.0): Touch press. (called by TouchRaycaster, forwarded to components)
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchMove(viewInputPosition)` (method, since 0.2.0): Touch move. (called by TouchRaycaster, forwarded to components)
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchRelease(viewInputPosition)` (method, since 0.2.0): Touch release. (called by TouchRaycaster, forwarded to components)
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchCancel(viewInputPosition)` (method, since 0.2.0): Touch cancel. (called by TouchRaycaster, forwarded to components)
  - `viewInputPosition` Vector2: Input position in view coordinates.
