Skip to main content

Node

Base class for 2D scene graph nodes, implementing the Promisable<Node> interface. Provides transformation, hierarchy, rendering, and state management functionality.

Overview

Node is the base class for all 2D components, providing:

  • Transformation system (position, rotation, scale)
  • Hierarchy management
  • State saving and restoration
  • Hit testing
  • Cache control

Transformation Properties

Position

PropertyTypeDescription
positionVector2Signal<this>Local space position
absolutePositionSimpleVector2Signal<this>World space position
xSimpleSignal<number, this>X position
ySimpleSignal<number, this>Y position

Rotation and Scale

PropertyTypeDescription
rotationSimpleSignal<number, this>Rotation angle (degrees)
absoluteRotationSimpleSignal<number, this>World space rotation
scaleVector2Signal<this>Scale
absoluteScaleSimpleVector2Signal<this>World space scale
skewVector2Signal<this>Skew

skew

skew: Vector2Signal<this>;

Skew property.

Display

PropertyTypeDescription
opacitySimpleSignal<number, this>Opacity (0-1)
absoluteOpacityComputedSignal<number>Absolute opacity
zIndexSimpleSignal<number, this>Z-axis ordering
cacheSimpleSignal<boolean, this>Whether to cache
compositeSimpleSignal<boolean, this>Whether to composite
compositeOperationSimpleSignal<GlobalCompositeOperation, this>Composite operation

opacity

opacity: SimpleSignal<number, this>;

Opacity (0-1).

zIndex

zIndex: SimpleSignal<number, this>;

Z-axis ordering.

cache

cache: SimpleSignal<boolean, this>;

Whether to cache.

Shadows and Filters

PropertyTypeDescription
shadowColorColorSignal<this>Shadow color
shadowBlurSimpleSignal<number, this>Shadow blur
shadowOffsetVector2Signal<this>Shadow offset
filtersFiltersSignal<this>Filter effects
shadersShadersSignal<this>Shader effects

shaders

shaders: ShadersSignal<this>;

Shader effects.

Children

children

children: Signal<ComponentChildren, Node[], this>;

Manages the child node list.

parent

parent: Node;

Parent node reference.

Cache Control

cachePadding

cachePadding: Signal<PossibleSpacing, Spacing, this>;

Inner padding for cache.

getCacheBBox

getCacheBBox(): BBox

Gets the cache bounding box.

cacheBBox

cacheBBox: ComputedSignal<BBox>;

Cache bounding box signal.

fullCacheBBox

fullCacheBBox: ComputedSignal<BBox>;

Full cache bounding box (including padding).

Hierarchy Methods

Add Children

add(node: ComponentChildren): this
insert(node: ComponentChildren, index?: number): this

Remove

remove(): this
removeChildren(): this

Reorder

moveUp(): this
moveDown(): this
moveToTop(): this
moveToBottom(): this

moveTo

moveTo(index: number): this

Moves to a specified index position.

moveAbove

moveAbove(node: Node, directlyAbove?: boolean): this

Moves above the specified node.

moveBelow

moveBelow(node: Node, directlyBelow?: boolean): this

Moves below the specified node.

Find

findFirst<T>(predicate: (node: Node) => node is T): T | null
findAncestor<T>(predicate: (node: Node) => node is T): T | null

findAll

findAll<T>(predicate: (node: Node) => node is T): T[]

Finds all nodes matching the condition.

Matrix Transformations

localToWorld(): DOMMatrix
worldToLocal(): DOMMatrix
parentToWorld(): DOMMatrix

State Management

save

save(): void

Saves the current node state.

restore

restore(duration?: number, timing?: TimingFunction): void | ThreadGenerator

Restores to the last saved state.

applyState

applyState(state: NodeState, duration?: number, timing?: TimingFunction): void | ThreadGenerator

Applies the specified state to the node.

Cloning

clone(customProps?: PartialNodeProps<this>): this
snapshotClone(customProps?: PartialNodeProps<this>): this
reactiveClone(customProps?: PartialNodeProps<this>): this

Examples

import {Circle, Rect} from '@wangyaoshen/locus';

const circle = new Circle({
radius: 100,
position: new Vector2(0, 0),
fill: 'red',
});

const rect = new Rect({
width: 200,
height: 100,
position: new Vector2(300, 0),
fill: 'blue',
});

// Create node hierarchy
const node = new Node();
node.add(circle);
node.add(rect);

// Animate
circle().position().x(400, 1);