Skip to content

Visualize Your Ideas With Code

feat: add reparent helper

aarthificial80b95a9ce89d
diff --git a/packages/2d/src/components/Layout.ts b/packages/2d/src/components/Layout.ts
index 2020ba6a..69459a7c 100644
--- a/packages/2d/src/components/Layout.ts
+++ b/packages/2d/src/components/Layout.ts
@@ -319,7 +319,7 @@ export class Layout extends Node {
if (typeof size.x !== 'number' || typeof size.y !== 'number') {
from = this.size();
} else {
- from = <Vector2>size;
+ from = new Vector2(<Vector2>size);
}
let to: Vector2;
@@ -328,7 +328,7 @@ export class Layout extends Node {
typeof value.x === 'number' &&
typeof value.y === 'number'
) {
- to = <Vector2>value;
+ to = new Vector2(<Vector2>value);
} else {
this.size(value);
to = this.size();
diff --git a/packages/2d/src/components/Node.ts b/packages/2d/src/components/Node.ts
index 2a614fa3..c55742c3 100644
--- a/packages/2d/src/components/Node.ts
+++ b/packages/2d/src/components/Node.ts
@@ -454,6 +454,7 @@ export class Node implements Promisable<Node> {
for (const node of array) {
if (node instanceof Node) {
newChildren.push(node);
+ node.remove();
node.parent(this);
}
}
@@ -572,16 +573,23 @@ export class Node implements Promisable<Node> {
return this.move(-Infinity);
}
- protected moveTo(parent: Node): this {
- const current = this.parent();
- if (current === parent) {
- return this;
- }
-
- parent.children([...parent.children(), this]);
- this.parent(parent);
-
- return this;
+ /**
+ * Change the parent of this node while keeping the absolute transform.
+ *
+ * @remarks
+ * After performing this operation, the node will stay in the same place
+ * visually, but its parent will be changed.
+ *
+ * @param newParent - The new parent of this node.
+ */
+ public reparent(newParent: Node) {
+ const position = this.absolutePosition();
+ const rotation = this.absoluteRotation();
+ const scale = this.absoluteScale();
+ newParent.add(this);
+ this.absolutePosition(position);
+ this.absoluteRotation(rotation);
+ this.absoluteScale(scale);
}
/**
diff --git a/packages/core/src/tweening/timingFunctions.ts b/packages/core/src/tweening/timingFunctions.ts
index b5b3842d..a080237a 100644
--- a/packages/core/src/tweening/timingFunctions.ts
+++ b/packages/core/src/tweening/timingFunctions.ts
@@ -60,17 +60,29 @@ export function easeInOutQuint(value: number, from = 0, to = 1) {
return map(from, to, value);
}
-export function easeInOutSine(value: number, from: number, to: number): number {
+export function easeInOutSine(
+ value: number,
+ from = 0,
+ to = 1,
+): number {
value = -(Math.cos(Math.PI * value) - 1) / 2;
return map(from, to, value);
}
-export function easeInSine(value: number, from: number, to: number): number {
+export function easeInSine(
+ value: number,
+ from = 0,
+ to = 1,
+): number {
value = 1 - Math.cos((value * Math.PI) / 2);
return map(from, to, value);
}
-export function easeOutSine(value: number, from: number, to: number): number {
+export function easeOutSine(
+ value: number,
+ from = 0,
+ to = 1,
+): number {
value = Math.sin((value * Math.PI) / 2);
return map(from, to, value);
}
diff --git a/packages/docs/docs/getting-started/hierarchy.mdx b/packages/docs/docs/getting-started/hierarchy.mdx
index 94e98074..2fe44c4f 100644
--- a/packages/docs/docs/getting-started/hierarchy.mdx
+++ b/packages/docs/docs/getting-started/hierarchy.mdx
@@ -116,6 +116,11 @@ following helper methods:
<ApiSnippet url={'/api/2d/components/Node#remove'} />
<hr />
+## `Node.reparent`
+
+<ApiSnippet url={'/api/2d/components/Node#reparent'} />
+<hr />
+
## `Node.moveUp`
<ApiSnippet url={'/api/2d/components/Node#moveUp'} />