Skip to content

Visualize Your Ideas With Code

refactor: surface transitions without cloning

aarthificial9a0c3657c4ac
diff --git a/src/animations/TimeTween.ts b/src/animations/TimeTween.ts
index 972b948c..a6a68334 100644
--- a/src/animations/TimeTween.ts
+++ b/src/animations/TimeTween.ts
@@ -133,4 +133,31 @@ export class TimeTween {
public static map(from: number, to: number, value: number) {
return from + (to - from) * value;
}
+
+ public static remap(
+ fromIn: number,
+ toIn: number,
+ fromOut: number,
+ toOut: number,
+ value: number,
+ ) {
+ return fromOut + ((value - fromIn) * (toOut - fromOut)) / (toIn - fromIn);
+ }
+
+ public static clampRemap(
+ fromIn: number,
+ toIn: number,
+ fromOut: number,
+ toOut: number,
+ value: number,
+ ) {
+ const remappedValue = TimeTween.remap(fromIn, toIn, fromOut, toOut, value);
+ if (fromOut > toOut) [fromOut, toOut] = [toOut, fromOut];
+
+ return remappedValue < fromOut
+ ? fromOut
+ : remappedValue > toOut
+ ? toOut
+ : remappedValue;
+ }
}
diff --git a/src/animations/surfaceTransition.ts b/src/animations/surfaceTransition.ts
index b079b0b5..f6f3ba92 100644
--- a/src/animations/surfaceTransition.ts
+++ b/src/animations/surfaceTransition.ts
@@ -2,6 +2,13 @@ import {Surface} from '../components/Surface';
import {Project} from '../Project';
import {TimeTween} from './TimeTween';
+export interface SurfaceTransitionConfig {
+ reverse?: boolean;
+ onSurfaceChange?: (surface: Surface) => void;
+ onFromOpacityChange?: (surface: Surface, value: TimeTween) => boolean | void;
+ onToOpacityChange?: (surface: Surface, value: TimeTween) => boolean | void;
+}
+
export function surfaceTransition(this: Project, fromSurfaceOriginal: Surface) {
const fromSurface = fromSurfaceOriginal
.clone()
@@ -15,22 +22,21 @@ export function surfaceTransition(this: Project, fromSurfaceOriginal: Surface) {
const project = this;
return function* (
- toSurfaceOriginal: Surface,
- inverse?: boolean,
- surfaceCallback?: (currentSurface: Surface) => void,
+ target: Surface,
+ config: SurfaceTransitionConfig = {},
) {
- const to = toSurfaceOriginal.getSurfaceData();
- const toPos = toSurfaceOriginal.getPosition();
+ const to = target.getSurfaceData();
+ const toPos = target.getPosition();
const fromPos = fromSurface.getPosition();
const fromDelta = fromSurface.calculateOriginDelta(
- toSurfaceOriginal.origin(),
+ target.origin(),
);
const fromNewPos = {
x: fromPos.x + fromDelta.x,
y: fromPos.y + fromDelta.y,
};
- const toDelta = toSurfaceOriginal.calculateOriginDelta(
+ const toDelta = target.calculateOriginDelta(
fromSurfaceOriginal.origin(),
);
const toNewPos = {
@@ -38,53 +44,56 @@ export function surfaceTransition(this: Project, fromSurfaceOriginal: Surface) {
y: toPos.y + toDelta.y,
};
- const toSurface = toSurfaceOriginal
- .clone()
- .moveTo(toSurfaceOriginal.parent)
- .zIndex(toSurfaceOriginal.zIndex());
- toSurfaceOriginal.hide();
- toSurface.hide();
- toSurface.setOverride(true);
+ target.hide();
- surfaceCallback?.(fromSurface);
+ config.onSurfaceChange?.(fromSurface);
let check = true;
yield* project.tween(0.6, value => {
if (value.value > 1 / 3) {
if (check) {
- toSurface.show();
+ target.show();
+ target.setOverride(true);
fromSurface.destroy();
}
- toSurface.setSurfaceData({
+ target.setSurfaceData({
...from,
- ...value.rectArc(from, to, inverse),
+ ...value.rectArc(from, to, config.reverse),
radius: value.easeInOutCubic(from.radius, to.radius),
color: value.color(from.color, to.color, value.easeInOutQuint()),
});
- toSurface.setPosition(value.rectArc(fromNewPos, toPos, inverse));
- toSurface
- .getChild()
- .opacity(Math.max(TimeTween.map(0, 1, value.linear(-1 / 2, 1)), 0));
+ target.setPosition(value.rectArc(fromNewPos, toPos, config.reverse));
+ if (!config.onToOpacityChange?.(target, value)) {
+ target
+ .getChild()
+ .opacity(Math.max(TimeTween.map(0, 1, value.linear(-1 / 2, 1)), 0));
+ }
if (check) {
- surfaceCallback?.(toSurface);
+ config.onSurfaceChange?.(target);
check = false;
}
} else {
fromSurface.setSurfaceData({
...from,
- ...value.rectArc(from, to, inverse),
+ ...value.rectArc(from, to, config.reverse),
radius: value.easeInOutCubic(from.radius, to.radius),
color: value.color(from.color, to.color, value.easeInOutQuint()),
});
- fromSurface.setPosition(value.rectArc(fromPos, toNewPos, inverse));
- fromSurface.getChild().opacity(TimeTween.map(1, 0, value.linear(0, 3)));
+ fromSurface.setPosition(
+ value.rectArc(fromPos, toNewPos, config.reverse),
+ );
+
+ if (!config.onFromOpacityChange?.(target, value)) {
+ fromSurface
+ .getChild()
+ .opacity(TimeTween.map(1, 0, value.linear(0, 3)));
+ }
}
});
- toSurface.destroy();
- toSurfaceOriginal.show();
- surfaceCallback?.(toSurfaceOriginal);
+ target.setOverride(false);
+ target.show();
};
}