Visualize Your Ideas With Code
refactor: better text animation
aarthificial72c47bff0251
diff --git a/src/Project.ts b/src/Project.tsindex c613ebd7..f6bc593a 100644--- a/src/Project.ts+++ b/src/Project.ts@@ -68,6 +68,12 @@ export class Project extends Stage {this.add(this.foreground);for (const scene of scenes) {+ if (+ scene.name === undefined ||+ scene.name === '__WEBPACK_DEFAULT_EXPORT__'+ ) {+ console.warn('Runner without a name: ', scene);+ }const handle = new Scene(this, scene);this.sceneLookup[scene.name] = handle;handle.threadsCallback = (...args) => {diff --git a/src/components/Grid.ts b/src/components/Grid.tsindex e24bf69c..977c5ded 100644--- a/src/components/Grid.ts+++ b/src/components/Grid.ts@@ -54,7 +54,7 @@ export class Grid extends LayoutShape {}const size = this.getSize();size.width = size.width / 2 + gridSize;- size.height = size.width / 2 + gridSize;+ size.height = size.height / 2 + gridSize;for (let x = -size.width; x <= size.width; x += gridSize) {this.path.moveTo(x, -size.height);diff --git a/src/components/LayoutText.ts b/src/components/LayoutText.tsindex d842fd7c..68d8feb6 100644--- a/src/components/LayoutText.ts+++ b/src/components/LayoutText.ts@@ -10,7 +10,8 @@ import {LayoutAttrs,} from './ILayoutNode';import {Origin, Size, PossibleSpacing, Spacing} from '../types';-import {easeInOutCubic, tween, textTween} from '../tweening';+import {Animator, tween, textTween, InterpolationFunction} from '../tweening';+import {threadable} from '../decorators';export interface LayoutTextConfig extends Partial<LayoutAttrs>, TextConfig {minWidth?: number;@@ -119,19 +120,30 @@ export class LayoutText extends Text implements ILayoutNode {return offset;}- public *animate(text: string) {- const fromText = this.text();- const fromWidth = this.getLayoutSize({minWidth: 0}).width;+ public get animate(): Animator<string, this> {+ return new Animator<string, this>(this, 'text', this.textTween);+ }++ @threadable()+ private *textTween(+ fromText: string,+ text: string,+ time: number,+ interpolation: InterpolationFunction,+ onEnd: Function,+ ) {+ const fromWidth = this.getLayoutSize({text: fromText, minWidth: 0}).width;const toWidth = this.getLayoutSize({text, minWidth: 0}).width;this.overrideWidth = fromWidth;- yield* tween(0.3, value => {- this.overrideWidth = easeInOutCubic(value, fromWidth, toWidth);- this.setText(textTween(fromText, text, easeInOutCubic(value)));+ yield* tween(time, value => {+ this.overrideWidth = interpolation(value, fromWidth, toWidth);+ this.setText(textTween(fromText, text, interpolation(value)));});this.overrideWidth = null;this.setText(text);+ onEnd();}public getOriginDelta(newOrigin: Origin, custom?: LayoutTextConfig) {diff --git a/src/decorators/getset.ts b/src/decorators/getset.tsindex d2d77336..b4797637 100644--- a/src/decorators/getset.ts+++ b/src/decorators/getset.ts@@ -1,10 +1,18 @@import {Factory} from 'konva/lib/Factory';+import {TweenProvider} from '../tweening';export function getset(defaultValue?: any,after?: () => void,+ tween?: TweenProvider<any>,): PropertyDecorator {return function (target, propertyKey) {- Factory.addGetterSetter(target.constructor, propertyKey, defaultValue, undefined, after);+ Factory.addGetterSetter(+ target.constructor,+ propertyKey,+ defaultValue,+ tween,+ after,+ );};-}\ No newline at end of file+}diff --git a/src/helpers/GeneratorHelper.ts b/src/helpers/GeneratorHelper.tsindex a196ca4e..4465774d 100644--- a/src/helpers/GeneratorHelper.ts+++ b/src/helpers/GeneratorHelper.ts@@ -7,10 +7,16 @@ export namespace GeneratorHelper {if (!prototype.threadable) {prototype.threadable = true;prototype.name =- typeof source === 'string' ? source : Object.getPrototypeOf(source).name;+ typeof source === 'string'+ ? source+ : Object.getPrototypeOf(source).name;}}+ export function isThreadable(value: any): boolean {+ return typeof value === 'function' && value.prototype.threadable === true;+ }+export function getName(task: Generator): string {return Object.getPrototypeOf(task).name ?? null;}diff --git a/src/patches/Factory.ts b/src/patches/Factory.tsindex ca55db67..4db66222 100644--- a/src/patches/Factory.ts+++ b/src/patches/Factory.ts@@ -1,7 +1,7 @@import {Factory} from 'konva/lib/Factory';import {ANIMATE} from '../symbols';import type {Node} from 'konva/lib/Node';-import {Animator} from '../tweening/Animator';+import {Animator, TweenProvider} from '../tweening';import {ThreadGenerator} from '../threading';declare module 'konva/lib/types' {@@ -16,18 +16,18 @@ declare module 'konva/lib/types' {Factory.overWriteSetter = function overWriteSetter(constructor: Function,attr: string,- validator?: any,+ validator?: TweenProvider<any>,after?: () => void,) {const name = attr.charAt(0).toUpperCase() + attr.slice(1);const setter = `set${name}`;constructor.prototype[setter] = function (val: any, time?: any) {if (val === ANIMATE) {- return new Animator(this, attr);+ return new Animator(this, attr, validator);}if (time !== undefined) {- return new Animator(this, attr).key(val, time).run();+ return new Animator(this, attr, validator).key(val, time).run();}const old = this.attrs[attr];diff --git a/src/tweening/Animator.ts b/src/tweening/Animator.tsindex 1c025812..dcd8d1e3 100644--- a/src/tweening/Animator.ts+++ b/src/tweening/Animator.ts@@ -14,6 +14,17 @@ import {import {threadable} from '../decorators';import {waitFor, waitUntil} from '../animations';import {ThreadGenerator} from '../threading';+import {GeneratorHelper} from '../helpers';++export interface TweenProvider<T> {+ (+ from: T,+ to: T,+ time: number,+ interpolation: InterpolationFunction,+ onEnd: Function,+ ): ThreadGenerator;+}export class Animator<Type, This extends Node> {private valueFrom: Type = null;@@ -27,6 +38,7 @@ export class Animator<Type, This extends Node> {public constructor(private readonly object: This,private readonly prop: string,+ private readonly tweenProvider?: TweenProvider<Type>,) {const name = this.prop.charAt(0).toUpperCase() + this.prop.slice(1);this.getter = `get${name}`;@@ -47,20 +59,29 @@ export class Animator<Type, This extends Node> {): this {this.lastValue = value;this.keys.push(() =>- tween(- time,- v => {- // @ts-ignore- this.object[this.setter](- mapper === undefined- ? this.mapper(this.valueFrom, value, interpolation(v))- : mapper(this.valueFrom, value, interpolation(v), ...args),- );- },- () => {- this.valueFrom = value;- },- ),+ GeneratorHelper.isThreadable(this.tweenProvider)+ ? this.tweenProvider.call(+ this.object,+ this.valueFrom,+ value,+ time,+ interpolation,+ () => (this.valueFrom = value),+ )+ : tween(+ time,+ v => {+ // @ts-ignore+ this.object[this.setter](+ mapper === undefined+ ? this.mapper(this.valueFrom, value, interpolation(v))+ : mapper(this.valueFrom, value, interpolation(v), ...args),+ );+ },+ () => {+ this.valueFrom = value;+ },+ ),);return this;@@ -147,7 +168,7 @@ export class Animator<Type, This extends Node> {} else {this.mapper = textTween;}- } else if (typeof this.valueFrom === 'object') {+ } else if (this.valueFrom && typeof this.valueFrom === 'object') {if ('x' in this.valueFrom) {if ('width' in this.valueFrom) {this.mapper = rectArcTween;diff --git a/src/tweening/index.ts b/src/tweening/index.tsindex 05bf8f71..e0580810 100644--- a/src/tweening/index.ts+++ b/src/tweening/index.ts@@ -1,3 +1,4 @@+export * from './Animator';export * from './tween';export * from './tweenFunctions';export * from './interpolationFunctions';diff --git a/src/tweening/tweenFunctions.ts b/src/tweening/tweenFunctions.tsindex b04dc2a2..5ead14eb 100644--- a/src/tweening/tweenFunctions.ts+++ b/src/tweening/tweenFunctions.ts@@ -8,7 +8,7 @@ export interface TweenFunction<T, Rest extends any[] = any[]> {export function textTween(from: string, to: string, value: number) {// left to right- if (to.length > from.length) {+ if (to.length >= from.length) {const current = Math.floor(to.length * value);const currentLength = Math.floor(map(from.length - 1, to.length, value));let text = '';