Visualize Your Ideas With Code
feat: surfaceFrom animation
aarthificial77bb69e6a648
diff --git a/src/animations/surfaceFrom.ts b/src/animations/surfaceFrom.tsnew file mode 100644index 00000000..1624457f--- /dev/null+++ b/src/animations/surfaceFrom.ts@@ -0,0 +1,81 @@+import {Surface} from '../components';+import {+ calculateRatio,+ clampRemap,+ colorTween,+ easeInOutCubic,+ easeInOutQuint,+ rectArcTween,+ tween,+} from '../tweening';+import {decorate, threadable} from '../decorators';+import {ThreadGenerator} from '../threading';++export interface SurfaceFromConfig {+ reverse?: boolean;+ onOpacityChange?: (+ surface: Surface,+ value: number,+ relativeValue: number,+ ) => boolean | void;+ transitionTime?: number;+}++decorate(surfaceFrom, threadable());+export function surfaceFrom(fromSurface: Surface) {+ const from = fromSurface.getMask();++ decorate(surfaceTransitionExecutor, threadable());+ function* surfaceTransitionExecutor(+ target: Surface,+ config: SurfaceFromConfig = {},+ ): ThreadGenerator {+ const transitionTime = config.transitionTime ?? 1 / 3;+ const to = target.getMask();+ const toPos = target.getPosition();+ const fromDelta = fromSurface.getOriginDelta(target.getOrigin());+ target.show();++ let ratio =+ (calculateRatio(fromSurface.getPosition(), toPos) ++ calculateRatio(from, to)) /+ 2;++ yield* tween(0.6, value => {+ const relativeValue = clampRemap(transitionTime, 1, 0, 1, value);+ const fromPos = fromSurface.getPosition();+ const fromNewPos = {+ x: fromPos.x + fromDelta.x,+ y: fromPos.y + fromDelta.y,+ };++ target.setMask({+ ...from,+ ...rectArcTween(from, to, easeInOutQuint(value), config.reverse, ratio),+ radius: easeInOutCubic(value, from.radius, target.radius()),+ color: colorTween(+ from.color,+ target.background(),+ easeInOutQuint(value),+ ),+ });+ target.setPosition(+ rectArcTween(+ fromNewPos,+ toPos,+ easeInOutQuint(value),+ config.reverse,+ ratio,+ ),+ );+ if (!config.onOpacityChange?.(target, value, relativeValue)) {+ target.getChild().opacity(relativeValue);+ }+ });++ target.setMask(null);+ target.show();+ }++ return surfaceTransitionExecutor;+}diff --git a/src/tweening/Animator.ts b/src/tweening/Animator.tsindex fcea8a58..1c025812 100644--- a/src/tweening/Animator.ts+++ b/src/tweening/Animator.ts@@ -17,6 +17,7 @@ import {ThreadGenerator} from '../threading';export class Animator<Type, This extends Node> {private valueFrom: Type = null;+ private lastValue: Type;private keys: (() => ThreadGenerator)[] = [];private mapper: TweenFunction<any> = map;private loops: number = 1;@@ -44,6 +45,7 @@ export class Animator<Type, This extends Node> {mapper?: TweenFunction<Type, Rest>,...args: Rest): this {+ this.lastValue = value;this.keys.push(() =>tween(time,@@ -64,6 +66,29 @@ export class Animator<Type, This extends Node> {return this;}+ public diff<Rest extends any[]>(+ value: Type,+ time: number,+ interpolation: InterpolationFunction = easeInOutCubic,+ mapper?: TweenFunction<Type, Rest>,+ ...args: Rest+ ): this {+ let next: any = value;+ const last: any =+ this.lastValue === undefined ? this.getValueFrom() : this.lastValue;+ if (Array.isArray(last)) {+ next = last.map((value1, index) => value1 + next[index]);+ } else if (typeof last === 'object') {+ for (const key in last) {+ next[key] += last[key];+ }+ } else {+ next += last;+ }++ return this.key(next, time, interpolation, mapper, ...args);+ }+public back<Rest extends any[]>(time: number,interpolation: InterpolationFunction = easeInOutCubic,diff --git a/src/tweening/tweenFunctions.ts b/src/tweening/tweenFunctions.tsindex 61f420d8..b04dc2a2 100644--- a/src/tweening/tweenFunctions.ts+++ b/src/tweening/tweenFunctions.ts@@ -96,16 +96,19 @@ export function rectArcTween(};}-export function calculateRatio(from: Partial<IRect>, to: Partial<IRect>): number {+export function calculateRatio(+ from: Partial<IRect>,+ to: Partial<IRect>,+): number {let numberOfValues = 0;let ratio = 0;- if(from.x) {+ if (from.x) {ratio += Math.abs((from.x - to.x) / (from.y - to.y));numberOfValues++;}if (from.width) {ratio += Math.abs((from.width - to.width) / (from.height - to.height));- numberOfValues ++;+ numberOfValues++;}if (numberOfValues) {