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

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

Hierarchical TRS object.

## TransformNode extends ComponentNode

Hierarchical TRS object.
- Transform features.
- Rendering features. (including components)

Example: Read position, rotation and scale through the parent

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

const parent = new TransformNode();
parent.setLocalPosition(Vector2.create(100, 50));
parent.setLocalRotation(90);
parent.setLocalScale(Vector2.create(2, 2));
const child = new TransformNode();
child.setLocalPositionXY(10, 0);
child.setLocalOpacity(0.5);
parent.addChild(child);
const position = child.getPosition();
const rotation = child.getRotation();
const scale = child.getScale();
const opacity = child.getOpacity();
console.log(position.x, position.y, rotation, scale.x, opacity);
```

- `constructor()` (constructor, since 0.0.9): Constructor.
- `pushTransform(graphic)` (method, since 0.0.11): Begins the draw state.
  - `graphic` Graphic: Graphic to apply the transform to.
- `applyLocalTransform(graphic)` (method, since 0.4.0): Applies the local position, rotation and scale to graphic's transform.
  - Reads position and scale without cloning. Subclasses that override getLocalPosition() / getLocalScale() are read through those functions.
  - `graphic` Graphic: Graphic to apply the transform to.
- `draw(graphic)` (method, since 0.0.9): Draw.
  - `graphic` Graphic: Graphic to draw with.
- `drawChildren(graphic)` (method, since 0.4.0): Draws the children. (Graphic.drawNode for each child in order; called by draw)
  - While subtree culling is on, the visible range of children is narrowed by binary search against the visible rectangle in parent coordinates (Graphic.readLocalVisibleBounds), and the children before and after it are not called.
    The range is found from the children's bounds written when the subtree bounds were calculated (the largest bottom and right gathered from the front, the smallest top and left gathered from the back), so it is correct in any child order,
    and when children are laid out in order like rows only the visible rows remain. A child with unknown bounds stops the narrowing past it. Drawing order is unchanged.
  - If this node's bounds change while drawing (such as a child moving a sibling), all remaining children are called.
  - While this node is drawing a subtree that lies fully inside the visible rectangle (Graphic.isInsideVisibleBounds), the range is not narrowed and all children are called. (The children are all inside the visible rectangle too)
  - `graphic` Graphic: Graphic to draw with.

  Example: A node that overrides draw drawing its children

  ```js
  import { Engine, EngineConfiguration, Scene, WorldNode, Paint, Colors, Rect, Vector2, Pivot } from "./build/vanilla.js";
  
  class FrameNode extends WorldNode {
  	draw(graphic) {
  		graphic.setFillColor(Colors.white);
  		graphic.drawRect(Rect.create(0, 0, 20, 20));
  		this.drawChildren(graphic);
  	}
  	calculateDrawnRect(rect) {
  		rect.position.set(0, 0);
  		rect.size.set(20, 20);
  		return true;
  	}
  }
  
  class MainScene extends Scene {
  	initialize(engine) {
  		super.initialize(engine);
  		const frameNode = new FrameNode();
  		frameNode.setPivot(Pivot.topLeft);
  		this.getRoot().addChild(frameNode);
  		const childNode = new WorldNode();
  		childNode.setPivot(Pivot.topLeft);
  		childNode.setRect(30, 0, 20, 20);
  		childNode.addComponent(Paint);
  		frameNode.addChild(childNode);
  	}
  	draw(graphic) {
  		super.draw(graphic);
  		console.log(graphic.getLastFrameCulledSubtreeCount());
  	}
  }
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  engine.run(new MainScene());
  ```

- `drawGizmos(graphic)` (method, since 0.0.11): Draw.
  - `graphic` Graphic: Graphic to draw with.
- `popTransform(graphic)` (method, since 0.0.11): Ends the draw state.
  - `graphic` Graphic: Graphic whose state is restored.
- `setPosition(position)` (method, since 0.0.9): Sets the global position.
  - `position` Vector2: World position through the parents. (converted to the local position by reversing the parents' position, rotation and scale)
- `getPosition()` (method, since 0.0.9): Returns the global position.
  - returns Vector2: World position through the parents. (the local position if there is no parent)
- `readPosition(out)` (method, since 0.4.0): Writes the global position into a given vector. (Same value as getPosition, no new vector)
  - Subclasses that override getPosition or the parent's position, rotation or scale readers are read through those functions. (Then it allocates as much as they do)
  - `out` Vector2: Vector to write.
  - returns Vector2: out.

  Example: Read the global position into the same vector every frame

  ```js
  import { TransformNode, Vector2 } from "./vanilla.js";
  const parentNode = new TransformNode();
  parentNode.setLocalPosition(Vector2.create(10, 20));
  const childNode = new TransformNode();
  childNode.setParent(parentNode);
  childNode.setLocalPosition(Vector2.create(5, 0));
  const position = Vector2.create(0, 0);
  childNode.readPosition(position);
  if (position.x !== 15 || position.y !== 20) {
  	throw new Error("position failed");
  }
  ```

- `readScale(out)` (method, since 0.4.0): Writes the global scale into a given vector. (Same value as getScale, no new vector)
  - Subclasses that override getScale or getLocalScale are read through those functions.
  - `out` Vector2: Vector to write.
  - returns Vector2: out.

  Example: Read the global scale into the same vector every frame

  ```js
  import { TransformNode, Vector2 } from "./vanilla.js";
  const parentNode = new TransformNode();
  parentNode.setLocalScale(Vector2.create(2, 2));
  const childNode = new TransformNode();
  childNode.setParent(parentNode);
  childNode.setLocalScale(Vector2.create(0.5, 3));
  const scale = Vector2.create(0, 0);
  childNode.readScale(scale);
  if (scale.x !== 1 || scale.y !== 6) {
  	throw new Error("scale failed");
  }
  ```

- `readLocalPosition(out)` (method, since 0.4.0): Writes the local position into a given vector. (Same value as getLocalPosition, no clone)
  - `out` Vector2: Vector to write.
  - returns Vector2: out.

  Example: Read the local position

  ```js
  import { TransformNode, Vector2 } from "./vanilla.js";
  const node = new TransformNode();
  node.setLocalPositionXY(3, 4);
  const position = node.readLocalPosition(Vector2.create(0, 0));
  if (position.x !== 3 || position.y !== 4) {
  	throw new Error("local position failed");
  }
  ```

- `readLocalScale(out)` (method, since 0.4.0): Writes the local scale into a given vector. (Same value as getLocalScale, no clone)
  - `out` Vector2: Vector to write.
  - returns Vector2: out.

  Example: Read the local scale

  ```js
  import { TransformNode, Vector2 } from "./vanilla.js";
  const node = new TransformNode();
  node.setLocalScaleXY(2, 0.5);
  const scale = node.readLocalScale(Vector2.create(0, 0));
  if (scale.x !== 2 || scale.y !== 0.5) {
  	throw new Error("local scale failed");
  }
  ```

- `setScale(scale)` (method, since 0.0.9): Sets the global scale.
  - `scale` Vector2: Scale through the parents. (divided by the parent scale into the local scale; 0 on axes where the parent scale is 0)
- `getScale()` (method, since 0.0.9): Returns the global scale.
  - returns Vector2: Scale through the parents.
- `setRotation(rotation)` (method, since 0.0.9): Sets the global rotation.
  - `rotation` number: Rotation through the parents. (degrees)
- `getRotation()` (method, since 0.0.9): Returns the global rotation.
  - returns number: Rotation through the parents. (degrees)
- `setLocalPosition(position)` (method, since 0.0.9): Sets the local position.
  - `position` Vector2: Position relative to the parent. (values are copied; the given vector is not kept)
- `setLocalPositionXY(x, y)` (method, since 0.4.0): Sets the local position. (takes x, y directly, does not create a Vector2)
  - Subclasses that override getLocalPosition() / setLocalPosition() are forwarded to setLocalPosition.
  - `x` number: X relative to the parent.
  - `y` number: Y relative to the parent.
- `getLocalPosition()` (method, since 0.0.9): Returns the local position.
  - returns Vector2: A copy of the local position.
- `setLocalScale(scale)` (method, since 0.0.9): Sets the local scale.
  - `scale` Vector2: Local scale. (values are copied)
- `setLocalScaleXY(x, y)` (method, since 0.4.0): Sets the local scale. (takes x, y directly, does not create a Vector2)
  - Subclasses that override setLocalScale() are forwarded to setLocalScale.
  - `x` number: Local horizontal scale.
  - `y` number: Local vertical scale.
- `getLocalScale()` (method, since 0.0.9): Returns the local scale.
  - returns Vector2: A copy of the local scale.
- `setLocalRotation(rotation)` (method, since 0.0.9): Sets the local rotation.
  - `rotation` number: Local rotation. (degrees)
- `getLocalRotation()` (method, since 0.0.9): Returns the local rotation.
  - returns number: Local rotation. (degrees)
- `setVisible(isVisible)` (method, since 0.4.0): Sets visibility. (A flag kept separate from active and opacity, the same as the Rust edition's WorldNode::set_visible)
  - If false, this node and its descendants are not drawn and do not receive TouchRaycaster presses. Updates (tick) still run.
  - `isVisible` boolean: Hides the node if false. (default true)

  Example: Hiding the parent makes the child invisible in the hierarchy

  ```js
  import { WorldNode } from "./build/vanilla.js";
  
  const parentNode = new WorldNode();
  const childNode = new WorldNode();
  parentNode.addChild(childNode);
  parentNode.setVisible(false);
  console.log(parentNode.isVisible(), parentNode.isActive(), childNode.isVisible(), childNode.isVisibleInHierarchy());
  ```

- `isVisible()` (method, since 0.0.9): Returns the visibility flag. (The value set with setVisible, active and opacity are not considered, use isVisibleInHierarchy for the hierarchy)
  - returns boolean: Visibility flag. (default true)
- `isVisibleInHierarchy()` (method, since 0.0.9): Returns the visibility of the whole hierarchy from this node to the root. (True only if this node and all ancestors are active, have the visibility flag set, and have local opacity above 0)
  - returns boolean: True if itself and all parents are visible.

  Example: False if any ancestor is inactive, has opacity 0, or is hidden

  ```js
  import { WorldNode } from "./build/vanilla.js";
  
  const parentNode = new WorldNode();
  const childNode = new WorldNode();
  parentNode.addChild(childNode);
  parentNode.setLocalOpacity(0);
  console.log(childNode.isActiveInHierarchy(), childNode.isVisibleInHierarchy());
  parentNode.setLocalOpacity(1);
  childNode.setVisible(false);
  console.log(parentNode.isVisibleInHierarchy(), childNode.isVisibleInHierarchy());
  ```

- `getOpacity()` (method, since 0.0.9): Returns the global alpha.
  - returns number: Opacity multiplied by the parents' opacity. (0 ~ 1)
- `setLocalOpacity(opacity)` (method, since 0.1.0): Sets the local alpha.
  - `opacity` number: Local opacity. (clamped to 0 ~ 1)
- `getLocalOpacity()` (method, since 0.0.11): Returns the local alpha.
  - returns number: Local opacity. (0 ~ 1)
- `setLastDrawnMaximumScale(maximumScale, frameNumber = 0)` (method, since 0.4.0): Sets the largest scale of the transform when last drawn and that frame number. (called by Graphic.drawNode)
  - `maximumScale` number: The largest scale of the transform.
  - `frameNumber` number: The frame number it was drawn in. (a number Graphic raises by one each frame, default 0)
- `getLastDrawnFrameNumber()` (method, since 0.4.0): Returns the frame number when last drawn. (the number written by setLastDrawnMaximumScale, -1 if never drawn)
  - returns number: The frame number when last drawn.
- `getLastDrawnMaximumScale()` (method, since 0.4.0): Returns the largest scale of the transform at the last draw. (0 means not known yet)
  - returns number: The largest scale of the transform. (0 if not yet known)
- `setGizmoVisible(isVisible)` (method, since 0.0.9): Sets whether gizmos are drawn.
  - `isVisible` boolean: If true, gizmos are drawn.
- `isGizmoVisible()` (method, since 0.0.9): Returns whether gizmos are drawn.
  - returns boolean: True if gizmos are drawn.
- `setOpacityLayer(isOpacityLayer)` (method, since 0.4.0): Sets the opacity layer. (Whether to group the opacity over the node and its descendants and lay it down at once, like CSS opacity)
  - When on and the opacity (multiplied by the parent's) is below 1, this node and its descendants are drawn into a layer and laid down at once with that opacity. (Graphic.beginOpacityLayer)
    Overlapping children do not show through each other. When off (the default), opacity is multiplied per node and each is laid down separately.
  - `isOpacityLayer` boolean: Whether to lay the opacity down as a group.

  Example: A translucent group node (overlapping children do not show through each other)

  ```js
  import { WorldNode } from "./build/vanilla.js";
  
  const groupNode = new WorldNode();
  groupNode.setLocalOpacity(0.5);
  groupNode.setOpacityLayer(true);
  console.log(groupNode.isOpacityLayer());
  ```

- `isOpacityLayer()` (method, since 0.4.0): Returns whether the node is an opacity layer.
  - returns boolean: True if the opacity is grouped over the node and its descendants.
- `setParent(parent)` (method, since 0.4.0): Sets the parent. (override: the subtree draw bounds of the old and the new parent change)
  - `parent` Node | null: The new parent. (null detaches)
  - returns boolean: True if attached or detached. (false if a descendant of itself is given as the parent)
- `setActive(active)` (method, since 0.4.0): Sets the active state. (override: an inactive child is not part of the parent's subtree draw bounds)
  - `active` boolean: Whether to enable it.
- `markDrawBoundsDirty()` (method, since 0.4.0): Marks that the subtree draw bounds changed. (this node and its ancestors recalculate the bounds the next time they draw)
  - The engine calls it when a transform, content size, added or removed child, active state, visibility or component changes. Spreading to ancestors stops at a node that is already marked.
  - A node that overrides draw calls it when the rectangle reported by calculateDrawnRect changes.

  Example: Report that a self-drawing node changed where it draws

  ```js
  import { WorldNode, Rect, Colors } from "./build/vanilla.js";
  
  class Ripple extends WorldNode {
  	radius = 10;
  	setRadius(radius) {
  		this.radius = radius;
  		this.markDrawBoundsDirty();
  	}
  	draw(graphic) {
  		super.draw(graphic);
  		graphic.setFillColor(Colors.white);
  		graphic.drawRect(Rect.create(-this.radius, -this.radius, this.radius * 2, this.radius * 2));
  	}
  	calculateDrawnRect(rect) {
  		rect.position.set(-this.radius, -this.radius);
  		rect.size.set(this.radius * 2, this.radius * 2);
  		return true;
  	}
  }
  
  const ripple = new Ripple();
  ripple.setRadius(40);
  const drawBounds = Rect.zero();
  console.log(ripple.readSubtreeDrawBounds(drawBounds), drawBounds.size.x);
  ```

- `markParentDrawBoundsDirty()` (method, since 0.4.0): Marks that the parent's subtree draw bounds changed. (when this node's transform, active state or visibility changes, this node's bounds stay the same and the parent's bounds change)
- `readSubtreeDrawBounds(rect)` (method, since 0.4.0): Reads the subtree draw bounds. (in this node's content coordinates, the coordinates after pushTransform, true if known)
  - A rectangle that covers everything drawn by this node and its active, visible descendants. A size of 0 means nothing is drawn.
  - Recalculated only when something changed (calculateSubtreeDrawBounds), otherwise the stored bounds are returned. The draw traversal skips the whole subtree when this rectangle is outside the screen and the clips.
  - False if something whose drawing area is unknown is included (a component that does not report its bounds, a node that overrides draw without reporting calculateDrawnRect).
  - `rect` Rect: Where to write the bounds.
  - returns boolean: True if the bounds are known. (rect is left untouched if unknown)

  Example: Read the subtree bounds of a row node

  ```js
  import { WorldNode, Paint, Rect, Vector2, Pivot } from "./build/vanilla.js";
  
  const rowNode = new WorldNode();
  rowNode.setPivot(Pivot.topLeft);
  rowNode.setContentSize(Vector2.create(300, 40));
  rowNode.addComponent(Paint);
  const iconNode = new WorldNode();
  iconNode.setPivot(Pivot.topLeft);
  iconNode.setRect(-20, 4, 32, 32);
  iconNode.addComponent(Paint);
  rowNode.addChild(iconNode);
  const drawBounds = Rect.zero();
  const isKnown = rowNode.readSubtreeDrawBounds(drawBounds);
  console.log(isKnown, drawBounds.position.x, drawBounds.size.x);
  ```

- `readParentDrawBounds(rect)` (method, since 0.4.0): Reads this node's subtree draw bounds as calculated by the parent. (In the parent's content coordinates, the bounding box with this node's transform applied; true if valid)
  - The parent writes these for each child when it calculates its subtree bounds (calculateSubtreeDrawBounds). When this node's transform or bounds change the parent recalculates, so they are reused until then.
  - The draw traversal checks this rectangle first under the parent's transform and skips this node without applying its transform when it lies outside the visible rectangle in parent coordinates. (Scrolling changes only the parent's transform, so children are not recalculated one by one)
  - False if the parent has not calculated yet or something changed since (a recalculation is due), or if where this node draws or where it is placed is unknown.
  - `rect` Rect: Where to write the bounds. (In the parent's content coordinates; a size of 0 means nothing is drawn)
  - returns boolean: True if valid bounds exist. (Otherwise rect is left untouched)

  Example: Reading a row's bounds calculated by its parent, in parent coordinates

  ```js
  import { WorldNode, Paint, Rect, Vector2, Pivot } from "./build/vanilla.js";
  
  const listNode = new WorldNode();
  listNode.setPivot(Pivot.topLeft);
  const rowNode = new WorldNode();
  rowNode.setPivot(Pivot.topLeft);
  rowNode.setRect(0, 120, 300, 40);
  rowNode.addComponent(Paint);
  listNode.addChild(rowNode);
  const drawBounds = Rect.zero();
  console.log(rowNode.readParentDrawBounds(drawBounds));
  listNode.readSubtreeDrawBounds(drawBounds);
  console.log(rowNode.readParentDrawBounds(drawBounds), drawBounds.position.y, drawBounds.size.y);
  ```

- `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: A node that does not override draw draws nothing by itself

  ```js
  import { TransformNode, Rect } from "./build/vanilla.js";
  
  const node = new TransformNode();
  const drawnRect = Rect.zero();
  console.log(node.calculateDrawnRect(drawnRect), drawnRect.size.x);
  ```

- `transformDrawBoundsToParent(rect)` (method, since 0.4.0): Moves subtree draw bounds into the parent's content coordinates. (True if known; calculateSubtreeDrawBounds calls it for each child)
  - TransformNode has no content area or pivot and does not define its placement, so it returns false. (WorldNode and UINode override it with their own placement)
  - `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: A node whose placement is unknown

  ```js
  import { TransformNode, Rect } from "./build/vanilla.js";
  
  const node = new TransformNode();
  console.log(node.transformDrawBoundsToParent(Rect.create(0, 0, 10, 10)));
  ```

- `transformLocalRectToParent(rect, pivotOffsetX, pivotOffsetY, translateX, translateY)` (method, since 0.4.0): Turns a rectangle into the bounding box after this node's rotation and scale and the move into parent coordinates. (Used by transformDrawBoundsToParent)
  - Order: subtract the pivot → scale → rotate → translate. (Same result as pushTransform's translate, rotate, scale)
  - `rect` Rect: The rectangle to transform. (In this node's content coordinates; the transformed bounding box is written back)
  - `pivotOffsetX` number: Horizontal pivot position. (Content coordinates; subtracted before scaling)
  - `pivotOffsetY` number: Vertical pivot position.
  - `translateX` number: Horizontal distance to move into parent coordinates.
  - `translateY` number: Vertical distance to move into parent coordinates.

  Example: A rectangle scaled by two and moved

  ```js
  import { TransformNode, Rect, Vector2 } from "./build/vanilla.js";
  
  const node = new TransformNode();
  node.setLocalScale(Vector2.create(2, 2));
  const rect = Rect.create(0, 0, 10, 10);
  node.transformLocalRectToParent(rect, 5, 5, 100, 0);
  console.log(rect.position.x, rect.size.x);
  ```

- `calculateSubtreeDrawBounds(rect)` (method, since 0.4.0): Calculates subtree draw bounds. (The union of the node's own drawing and its enabled, visible children's bounds; readSubtreeDrawBounds calls it only after a change; true if known)
  - Own drawing: calculateDrawnRect and enabled components. Components drawing only inside the content area (Component.isDrawnInsideContent) count as the content area grown by one unit,
    components drawing outside use Component.calculateDrawnRect. Components that draw nothing (no draw override) are skipped;
    a single unknown component makes the bounds unknown. A node drawing gizmos, or one whose content size cannot be read (when it has inside-content components), is also unknown.
  - Children: each child's subtree bounds moved by the child (transformDrawBoundsToParent) into this node's content coordinates. A child with unknown placement makes the bounds unknown.
  - Even when unknown, all enabled and visible children's bounds are read. (Children must cache theirs so descendant changes propagate up to this node)
  - Each child's bounds moved into parent coordinates are written onto the child. (readParentDrawBounds, which the draw traversal checks before applying the child's transform, valid until this node recalculates)
  - Bounds are not shrunk by masks clipping children. (Kept generous)
  - `rect` Rect: Where to write the calculated bounds. (in this node's content coordinates)
  - returns boolean: True if the bounds are known.
