Skip to content

Visualize Your Ideas With Code

refactor: move animations to Project.ts

aarthificial34c95cfda97f
diff --git a/src/Player.ts b/src/Player.ts
index 2370998e..9dc1a6b0 100644
--- a/src/Player.ts
+++ b/src/Player.ts
@@ -1,17 +1,29 @@
import {Project} from './Project';
+const MINIMUM_ANIMATION_DURATION = 1000;
+
export function Player(factory: () => Project) {
const project = factory();
+ let startTime = performance.now();
project.start();
const run = () => {
try {
if (project.next()) {
+ // Prevent animation from restarting too quickly.
+ const animationDuration = performance.now() - startTime;
+ if (animationDuration < MINIMUM_ANIMATION_DURATION) {
+ setTimeout(run, MINIMUM_ANIMATION_DURATION - animationDuration);
+ return;
+ }
+
+ startTime = performance.now();
project.start();
project.next();
}
requestAnimationFrame(run);
} catch (e) {
+ console.error(e);
}
- }
+ };
run();
}
diff --git a/src/Project.ts b/src/Project.ts
index 3d510fd2..ea62f9c8 100644
--- a/src/Project.ts
+++ b/src/Project.ts
@@ -3,7 +3,16 @@ import {Rect} from 'konva/lib/shapes/Rect';
import {Layer} from 'konva/lib/Layer';
import {Scene, SceneRunner} from './Scene';
import {Vector2d} from 'konva/lib/types';
-import {Konva} from "konva/lib/Global";
+import {Konva} from 'konva/lib/Global';
+import {
+ move,
+ showTop,
+ surfaceTransition,
+ tween,
+ waitFor,
+ waitUntil,
+ sequence,
+} from './animations';
Konva.autoDrawEnabled = false;
@@ -77,35 +86,19 @@ export class Project extends Stage {
return result.done;
}
- public *waitForTime(targetTime = 0, after?: Generator): Generator {
- while (this.frame < targetTime * this.framesPerSeconds) {
- yield;
- }
-
- if (after) {
- yield* after;
- }
+ public secondsToFrames(seconds: number) {
+ return Math.ceil(seconds * this.framesPerSeconds);
}
- public *waitForFrames(numberOfFrames = 0, after?: Generator): Generator {
- const startFrame = this.frame;
- while (this.frame - startFrame < numberOfFrames) {
- yield;
- }
-
- if (after) {
- yield* after;
- }
+ public framesToSeconds(frames: number) {
+ return frames / this.framesPerSeconds;
}
- public *waitForSeconds(numberOfSeconds = 0, after?: Generator): Generator {
- const startFrame = this.frame;
- while (this.frame - startFrame < numberOfSeconds * this.framesPerSeconds) {
- yield;
- }
-
- if (after) {
- yield* after;
- }
- }
+ public waitFor = waitFor;
+ public waitUntil = waitUntil;
+ public tween = tween;
+ public moveNode = move;
+ public showTop = showTop;
+ public surfaceTransition = surfaceTransition;
+ public sequence = sequence;
}
diff --git a/src/tweening/TimeTween.ts b/src/animations/TimeTween.ts
similarity index 77%
rename from src/tweening/TimeTween.ts
rename to src/animations/TimeTween.ts
index 6aa66770..972b948c 100644
--- a/src/tweening/TimeTween.ts
+++ b/src/animations/TimeTween.ts
@@ -1,5 +1,5 @@
-import {Vector2d} from 'konva/lib/types';
-import mixColor from "mix-color";
+import {IRect, Vector2d} from 'konva/lib/types';
+import mixColor from 'mix-color';
export class TimeTween {
public constructor(public value: number) {}
@@ -58,7 +58,9 @@ export class TimeTween {
// left to right
if (to.length > from.length) {
const current = Math.floor(to.length * (value ?? this.value));
- const currentLength = Math.floor(TimeTween.map(from.length - 1, to.length, value ?? this.value));
+ const currentLength = Math.floor(
+ TimeTween.map(from.length - 1, to.length, value ?? this.value),
+ );
let text = '';
for (let i = 0; i < to.length; i++) {
if (i < current) {
@@ -73,7 +75,9 @@ export class TimeTween {
// right to left
else {
const current = Math.round(from.length * (1 - (value ?? this.value)));
- const currentLength = Math.floor(TimeTween.map(from.length + 1, to.length, value ?? this.value));
+ const currentLength = Math.floor(
+ TimeTween.map(from.length + 1, to.length, value ?? this.value),
+ );
let text = [];
for (let i = from.length - 1; i >= 0; i--) {
if (i < current) {
@@ -98,6 +102,27 @@ export class TimeTween {
};
}
+ public rectArc(
+ from: Partial<IRect>,
+ to: Partial<IRect>,
+ reverse?: boolean,
+ value?: number,
+ ) {
+ value ??= this.easeInOutQuint(0, Math.PI / 2);
+ let xValue = Math.sin(value);
+ let yValue = 1 - Math.cos(value);
+ if (reverse) {
+ [xValue, yValue] = [yValue, xValue];
+ }
+
+ return {
+ x: TimeTween.map(from.x ?? 0, to.x ?? 0, xValue),
+ y: TimeTween.map(from.y ?? 0, to.y ?? 0, yValue),
+ width: TimeTween.map(from.width ?? 0, to.width ?? 0, xValue),
+ height: TimeTween.map(from.height ?? 0, to.height ?? 0, yValue),
+ };
+ }
+
public static vector2d(from: Vector2d, to: Vector2d, value: number) {
return {
x: TimeTween.map(from.x, to.x, value),
diff --git a/src/animations/index.ts b/src/animations/index.ts
index 9aaee0fd..1ee7533d 100644
--- a/src/animations/index.ts
+++ b/src/animations/index.ts
@@ -1,4 +1,8 @@
-export * from './move';
+export * from './motion';
export * from './show';
-export * from './pop';
-export * from './surfaceTransition';
\ No newline at end of file
+export * from '../utils/pop';
+export * from './surfaceTransition';
+export * from './tween';
+export * from './TimeTween';
+export * from './scheduling';
+export * from './sequence';
\ No newline at end of file
diff --git a/src/animations/move.ts b/src/animations/motion.ts
similarity index 87%
rename from src/animations/move.ts
rename to src/animations/motion.ts
index de75c61d..1cacf404 100644
--- a/src/animations/move.ts
+++ b/src/animations/motion.ts
@@ -1,19 +1,22 @@
import {Node} from 'konva/lib/Node';
import {Vector2d} from 'konva/lib/types';
-import {tween} from '../tweening';
+import {Project} from "../Project";
export function move(
+ this: Project,
node: Node,
position: Vector2d,
absolute?: boolean,
): Generator;
export function move(
+ this: Project,
node: Node,
positionX: number,
positionY: number,
absolute?: boolean,
): Generator;
export function move(
+ this: Project,
node: Node,
arg0: number | Vector2d,
arg1?: number | boolean,
@@ -39,7 +42,7 @@ export function move(
Math.pow(positionFrom.y - positionTo.y, 2),
);
- return tween(distance / 1000, value =>
+ return this.tween(distance / 1000, value =>
node.position(value.vector2d(positionFrom, positionTo)),
);
}
diff --git a/src/animations/scheduling.ts b/src/animations/scheduling.ts
new file mode 100644
index 00000000..bd120752
--- /dev/null
+++ b/src/animations/scheduling.ts
@@ -0,0 +1,31 @@
+import type {Project} from '../Project';
+
+export function* waitUntil(
+ this: Project,
+ targetTime = 0,
+ after?: Generator,
+): Generator {
+ while (this.frame < targetTime * this.framesPerSeconds) {
+ yield;
+ }
+
+ if (after) {
+ yield* after;
+ }
+}
+
+export function* waitFor(
+ this: Project,
+ seconds = 0,
+ after?: Generator,
+): Generator {
+ const frames = this.secondsToFrames(seconds);
+ const startFrame = this.frame;
+ while (this.frame - startFrame < frames) {
+ yield;
+ }
+
+ if (after) {
+ yield* after;
+ }
+}
diff --git a/src/animations/sequence.ts b/src/animations/sequence.ts
new file mode 100644
index 00000000..6c5e993f
--- /dev/null
+++ b/src/animations/sequence.ts
@@ -0,0 +1,25 @@
+import {Project} from '../Project';
+
+export function* sequence(
+ this: Project,
+ delay: number,
+ ...sequences: Generator[]
+): Generator {
+ const delayFrames = this.secondsToFrames(delay);
+ const startFrame = this.frame;
+ let finished = 0;
+ while (finished < sequences.length) {
+ finished = 0;
+
+ const frames = this.frame - startFrame;
+ const amount = Math.floor(frames / delayFrames) + 1;
+
+ for (let i = 0; i < Math.min(sequences.length, amount); i++) {
+ if (sequences[i].next().done) {
+ finished++;
+ }
+ }
+
+ yield;
+ }
+}
diff --git a/src/animations/show.ts b/src/animations/show.ts
index b9638233..9f630d00 100644
--- a/src/animations/show.ts
+++ b/src/animations/show.ts
@@ -1,18 +1,18 @@
import {Node} from 'konva/lib/Node';
-import {tween} from '../tweening';
+import {Project} from '../Project';
-export function showTop(node: Node): [Generator, Generator] {
+export function showTop(this: Project, node: Node): [Generator, Generator] {
const to = node.offsetY();
const from = to - 40;
node.offsetY(from);
node.opacity(0);
return [
- tween(0.5, value => {
+ this.tween(0.5, value => {
node.opacity(Math.min(1, value.linear(0, 2)));
node.offsetY(value.easeOutExpo(from, to));
}),
- tween(0.5, value => {
+ this.tween(0.5, value => {
node.opacity(Math.min(1, value.linear(2, 0)));
node.offsetY(value.easeInExpo(to, from));
}),
diff --git a/src/animations/surfaceTransition.ts b/src/animations/surfaceTransition.ts
index ca411385..b079b0b5 100644
--- a/src/animations/surfaceTransition.ts
+++ b/src/animations/surfaceTransition.ts
@@ -1,83 +1,90 @@
import {Surface} from '../components/Surface';
-import {TimeTween, tween} from '../tweening';
+import {Project} from '../Project';
+import {TimeTween} from './TimeTween';
-export function surfaceTransition(
- fromSurface: Surface,
- toSurface: Surface,
- inverse?: boolean,
-) {
- const from = fromSurface.getSurfaceData();
- const fromPos = fromSurface.getPosition();
- const to = toSurface.getSurfaceData();
- const toPos = toSurface.getPosition();
+export function surfaceTransition(this: Project, fromSurfaceOriginal: Surface) {
+ const fromSurface = fromSurfaceOriginal
+ .clone()
+ .moveTo(fromSurfaceOriginal.parent)
+ .zIndex(fromSurfaceOriginal.zIndex());
- const fromDelta = fromSurface.calculateOriginDelta(toSurface.origin());
- const fromNewPos = {
- x: fromPos.x + fromDelta.x,
- y: fromPos.y + fromDelta.y,
- };
+ fromSurfaceOriginal.hide();
+ fromSurface.setOverride(true);
- const toDelta = toSurface.calculateOriginDelta(fromSurface.origin());
- const toNewPos = {
- x: toPos.x + toDelta.x,
- y: toPos.y + toDelta.y,
- };
+ const from = fromSurfaceOriginal.getSurfaceData();
+
+ const project = this;
+ return function* (
+ toSurfaceOriginal: Surface,
+ inverse?: boolean,
+ surfaceCallback?: (currentSurface: Surface) => void,
+ ) {
+ const to = toSurfaceOriginal.getSurfaceData();
+ const toPos = toSurfaceOriginal.getPosition();
+ const fromPos = fromSurface.getPosition();
+
+ const fromDelta = fromSurface.calculateOriginDelta(
+ toSurfaceOriginal.origin(),
+ );
+ const fromNewPos = {
+ x: fromPos.x + fromDelta.x,
+ y: fromPos.y + fromDelta.y,
+ };
+ const toDelta = toSurfaceOriginal.calculateOriginDelta(
+ fromSurfaceOriginal.origin(),
+ );
+ const toNewPos = {
+ x: toPos.x + toDelta.x,
+ y: toPos.y + toDelta.y,
+ };
- fromSurface.show();
- toSurface.hide();
+ const toSurface = toSurfaceOriginal
+ .clone()
+ .moveTo(toSurfaceOriginal.parent)
+ .zIndex(toSurfaceOriginal.zIndex());
+ toSurfaceOriginal.hide();
+ toSurface.hide();
+ toSurface.setOverride(true);
- let check = true;
- return tween(0.6, value => {
- const distance = value.easeInOutQuint(0, Math.PI / 2);
- let xValue = Math.sin(distance);
- let yValue = 1 - Math.cos(distance);
- if (inverse) {
- [xValue, yValue] = [yValue, xValue];
- }
+ surfaceCallback?.(fromSurface);
- if (value.value > 1 / 3) {
- if (check) {
- toSurface.setOverride(true);
- toSurface.show();
- fromSurface.hide();
- fromSurface.setSurfaceData(from);
- fromSurface.setPosition(fromPos);
- fromSurface.getChild().opacity(1);
- fromSurface.setOverride(false);
+ let check = true;
+ yield* project.tween(0.6, value => {
+ if (value.value > 1 / 3) {
+ if (check) {
+ toSurface.show();
+ fromSurface.destroy();
+ }
+
+ toSurface.setSurfaceData({
+ ...from,
+ ...value.rectArc(from, to, inverse),
+ 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));
+
+ if (check) {
+ surfaceCallback?.(toSurface);
+ check = false;
+ }
+ } else {
+ fromSurface.setSurfaceData({
+ ...from,
+ ...value.rectArc(from, to, inverse),
+ 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)));
}
+ });
- toSurface.setSurfaceData({
- ...from,
- x: value.linear(from.x, to.x, xValue),
- y: value.linear(from.y, to.y, yValue),
- width: value.linear(from.width, to.width, xValue),
- height: value.linear(from.height, to.height, yValue),
- radius: value.easeInOutCubic(from.radius, to.radius),
- color: value.color(from.color, to.color, value.easeInOutQuint()),
- });
- toSurface.setPosition({
- x: value.linear(fromNewPos.x, toPos.x, xValue),
- y: value.linear(fromNewPos.y, toPos.y, yValue),
- });
- toSurface
- .getChild()
- .opacity(Math.max(TimeTween.map(0, 1, value.linear(-1 / 2, 1)), 0));
- } else {
- fromSurface.setOverride(true);
- fromSurface.setSurfaceData({
- ...from,
- x: value.linear(from.x, to.x, xValue),
- y: value.linear(from.y, to.y, yValue),
- width: value.linear(from.width, to.width, xValue),
- height: value.linear(from.height, to.height, yValue),
- radius: value.easeInOutCubic(from.radius, to.radius),
- color: value.color(from.color, to.color, value.easeInOutQuint()),
- });
- fromSurface.setPosition({
- x: value.linear(fromPos.x, toNewPos.x, xValue),
- y: value.linear(fromPos.y, toNewPos.y, yValue),
- });
- fromSurface.getChild().opacity(TimeTween.map(1, 0, value.linear(0, 3)));
- }
- });
-}
\ No newline at end of file
+ toSurface.destroy();
+ toSurfaceOriginal.show();
+ surfaceCallback?.(toSurfaceOriginal);
+ };
+}
diff --git a/src/animations/tween.ts b/src/animations/tween.ts
new file mode 100644
index 00000000..4d20d16e
--- /dev/null
+++ b/src/animations/tween.ts
@@ -0,0 +1,19 @@
+import {TimeTween} from './TimeTween';
+import {Project} from '../Project';
+
+export function* tween(
+ this: Project,
+ duration: number,
+ callback: (value: TimeTween) => void,
+): Generator {
+ const frames = duration * this.framesPerSeconds;
+ let startFrame = this.frame;
+ const timeTween = new TimeTween(0);
+ while (this.frame - startFrame < frames) {
+ timeTween.value = (this.frame - startFrame) / frames;
+ callback(timeTween);
+ yield;
+ }
+ timeTween.value = 1;
+ callback(timeTween);
+}
diff --git a/src/components/Arrow.ts b/src/components/Arrow.ts
index 1b690507..645be87c 100644
--- a/src/components/Arrow.ts
+++ b/src/components/Arrow.ts
@@ -255,6 +255,7 @@ export class Arrow extends Shape<ArrowConfig> {
) {
const arrowSize = (this.attrs.arrowSize || 0) * size;
const offset = this.strokeWidth() / 2;
+ // noinspection JSSuspiciousNameCombination
const normal = {
x: -tangent.y,
y: tangent.x,
diff --git a/src/components/Connection.ts b/src/components/Connection.ts
index 93dd7990..bac0bb89 100644
--- a/src/components/Connection.ts
+++ b/src/components/Connection.ts
@@ -5,7 +5,8 @@ import {Factory} from 'konva/lib/Factory';
import {GetSet, Vector2d} from 'konva/lib/types';
import {Direction} from '../types/Origin';
import {Context} from 'konva/lib/Context';
-import {TimeTween} from '../tweening';
+import {TimeTween} from '../animations';
+import {SURFACE_CHANGE_EVENT} from 'MC/components/Surface';
export interface ConnectionPoint {
node: Node;
@@ -34,11 +35,14 @@ function clamp(value: number, min: number, max: number): number {
}
export class Connection extends Arrow {
+ private nodeCache: Record<string, Node> = {};
+ private markAsDirtyCallback: () => void;
+
constructor(config?: ConnectionConfig) {
super(config);
}
- private measurePosition(
+ private static measurePosition(
positionA: number,
sizeA: number,
positionB: number,
@@ -65,7 +69,7 @@ export class Connection extends Arrow {
return {direction, range, rangeOffset, from: positionA, to: positionB};
}
- private calculateCrossing(
+ private static calculateCrossing(
crossing: number,
measurement: Measurement,
): number {
@@ -96,127 +100,111 @@ export class Connection extends Arrow {
_sceneFunc(context: Context) {
if (this.dirty) {
- if (!this.attrs.from || !this.attrs.to) {
- this.attrs.points = [];
- } else {
- const from: ConnectionPoint = this.attrs.from;
- const to: ConnectionPoint = this.attrs.to;
+ this.recalculate();
+ }
- const fromPosition = from.node.absolutePosition();
- const fromSize = from.node.size();
- fromSize.width /= 2;
- fromSize.height /= 2;
- const toPosition = to.node.absolutePosition();
- const toSize = to.node.size();
- toSize.width /= 2;
- toSize.height /= 2;
+ super._sceneFunc(context);
+ }
- this.attrs.points = [];
- const horizontal = this.measurePosition(
- fromPosition.x,
- fromSize.width,
- toPosition.x,
- toSize.width,
- );
- const vertical = this.measurePosition(
- fromPosition.y,
- fromSize.height,
- toPosition.y,
- toSize.height,
- );
+ private recalculate() {
+ const from: ConnectionPoint = this.from();
+ const to: ConnectionPoint = this.to();
+ const crossing = this.crossing();
- if (from.vertical) {
- this.attrs.points.push(fromPosition.x, vertical.range[0]);
- } else {
- this.attrs.points.push(horizontal.range[0], fromPosition.y);
- }
+ if (!from.node || !to.node) {
+ this.points([]);
+ return;
+ }
- let toVertical = to.vertical;
- if (
- from.vertical !== toVertical &&
- (horizontal.direction === 0 || vertical.direction === 0)
- ) {
- toVertical = from.vertical;
- }
+ const fromRect = from.node.getClientRect();
+ fromRect.width /= 2;
+ fromRect.height /= 2;
+ fromRect.x += fromRect.width;
+ fromRect.y += fromRect.height;
+ const toRect = to.node.getClientRect();
+ toRect.width /= 2;
+ toRect.height /= 2;
+ toRect.x += toRect.width;
+ toRect.y += toRect.height;
+
+ const points: number[] = [];
+ const horizontal = Connection.measurePosition(
+ fromRect.x,
+ fromRect.width,
+ toRect.x,
+ toRect.width,
+ );
+ const vertical = Connection.measurePosition(
+ fromRect.y,
+ fromRect.height,
+ toRect.y,
+ toRect.height,
+ );
- let distance = 100;
- if (from.vertical === toVertical) {
- if (from.vertical) {
- distance = Math.abs(toPosition.x - fromPosition.x);
- if (distance > 0) {
- const y = this.calculateCrossing(this.attrs.crossing.y, vertical);
- this.attrs.points.push(fromPosition.x, y);
- this.attrs.points.push(toPosition.x, y);
- }
- } else {
- distance = Math.abs(toPosition.y - fromPosition.y);
- if (distance > 0) {
- const x = this.calculateCrossing(
- this.attrs.crossing.x,
- horizontal,
- );
- this.attrs.points.push(x, fromPosition.y);
- this.attrs.points.push(x, toPosition.y);
- }
- }
- } else {
- if (from.vertical) {
- this.attrs.points.push(fromPosition.x, toPosition.y);
- } else {
- this.attrs.points.push(toPosition.x, fromPosition.y);
- }
- }
+ if (from.vertical) {
+ points.push(fromRect.x, vertical.range[0]);
+ } else {
+ points.push(horizontal.range[0], fromRect.y);
+ }
- if (toVertical) {
- this.attrs.points.push(toPosition.x, vertical.range[1]);
- } else {
- this.attrs.points.push(horizontal.range[1], toPosition.y);
- }
+ let toVertical = to.vertical;
+ if (
+ from.vertical !== toVertical &&
+ (horizontal.direction === 0 || vertical.direction === 0)
+ ) {
+ toVertical = from.vertical;
+ }
- this.attrs.radius = Math.min(8, distance / 2);
+ let distance = 100;
+ if (from.vertical === toVertical) {
+ if (from.vertical) {
+ distance = Math.abs(toRect.x - fromRect.x);
+ if (distance >= 1) {
+ const y = Connection.calculateCrossing(crossing.y, vertical);
+ points.push(fromRect.x, y);
+ points.push(toRect.x, y);
+ }
+ } else {
+ distance = Math.abs(toRect.y - fromRect.y);
+ if (distance >= 1) {
+ const x = Connection.calculateCrossing(crossing.x, horizontal);
+ points.push(x, fromRect.y);
+ points.push(x, toRect.y);
+ }
+ }
+ } else {
+ if (from.vertical) {
+ points.push(fromRect.x, toRect.y);
+ } else {
+ points.push(toRect.x, fromRect.y);
}
}
- super._sceneFunc(context);
- }
-
- private previousFromNode: Node | null = null;
- public fromChanged() {
- if (this.previousFromNode === this.attrs.from?.node) return;
- this.markAsDirtyCallback ??= () => this.markAsDirty();
+ if (toVertical) {
+ points.push(toRect.x, vertical.range[1]);
+ } else {
+ points.push(horizontal.range[1], toRect.y);
+ }
- this.previousFromNode?.off(
- 'absoluteTransformChange',
- this.markAsDirtyCallback,
- );
- this.previousFromNode = this.attrs.from?.node;
- this.previousFromNode?.on(
- 'absoluteTransformChange',
- this.markAsDirtyCallback,
- );
- this.markAsDirty();
+ this.radius(Math.min(8, distance / 2));
+ this.points(points);
}
- private previousToNode: Node | null = null;
- public toChanged() {
- if (this.previousToNode === this.attrs.to?.node) return;
+ public nodeChanged(name: string, node: Node) {
+ this.nodeCache ??= {};
this.markAsDirtyCallback ??= () => this.markAsDirty();
- this.previousToNode?.off(
- 'absoluteTransformChange',
- this.markAsDirtyCallback,
- );
- this.previousToNode = this.attrs.to?.node;
- this.previousToNode?.on(
- 'absoluteTransformChange',
- this.markAsDirtyCallback,
- );
- this.markAsDirty();
- }
+ if (this.nodeCache[name] === node) return;
- private markAsDirtyCallback = () => {
+ this.nodeCache[name]
+ ?.off('absoluteTransformChange', this.markAsDirtyCallback)
+ .off(SURFACE_CHANGE_EVENT, this.markAsDirtyCallback);
+ this.nodeCache[name] = node;
+ this.nodeCache[name]
+ ?.on('absoluteTransformChange', this.markAsDirtyCallback)
+ .on(SURFACE_CHANGE_EVENT, this.markAsDirtyCallback);
this.markAsDirty();
- };
+ }
from: GetSet<ConnectionPoint, this>;
to: GetSet<ConnectionPoint, this>;
@@ -237,8 +225,11 @@ Factory.addGetterSetter(
offset: 0,
},
undefined,
- Connection.prototype.fromChanged,
+ function (this: Connection) {
+ this.nodeChanged('from', this.from().node);
+ },
);
+
Factory.addGetterSetter(
Connection,
'to',
@@ -248,7 +239,9 @@ Factory.addGetterSetter(
offset: 0,
},
undefined,
- Connection.prototype.toChanged,
+ function (this: Connection) {
+ this.nodeChanged('to', this.to().node);
+ },
);
Factory.addGetterSetter(
Connection,
diff --git a/src/components/Sprite.ts b/src/components/Sprite.ts
index dde740f8..e271bcf4 100644
--- a/src/components/Sprite.ts
+++ b/src/components/Sprite.ts
@@ -7,7 +7,7 @@ export interface SpriteConfig extends ShapeConfig {
}
export class Sprite extends Shape {
- private image: HTMLImageElement;
+ private readonly image: HTMLImageElement;
constructor(config?: SpriteConfig) {
super(config);
diff --git a/src/components/Surface.ts b/src/components/Surface.ts
index dd4b1444..abe1ccfb 100644
--- a/src/components/Surface.ts
+++ b/src/components/Surface.ts
@@ -1,5 +1,5 @@
import {Group} from 'konva/lib/Group';
-import {ContainerConfig} from 'konva/lib/Container';
+import {Container, ContainerConfig} from 'konva/lib/Container';
import {Rect} from 'konva/lib/shapes/Rect';
import {Shape} from 'konva/lib/Shape';
import {GetSet, IRect, Vector2d} from 'konva/lib/types';
@@ -7,8 +7,8 @@ import {SceneContext} from 'konva/lib/Context';
import {Direction, Origin} from 'MC/types/Origin';
import {Factory} from 'konva/lib/Factory';
import {_registerNode} from 'konva/lib/Global';
-import {tween} from 'MC/tweening';
import {parseColor} from 'mix-color';
+import {Project} from 'MC/Project';
export const SURFACE_CHANGE_EVENT = 'surfaceChange';
@@ -110,6 +110,10 @@ export class Surface extends Group {
return this;
}
+ public get project(): Project {
+ return <Project>this.getStage();
+ }
+
public *doRipple() {
if (this.override) return;
const opaque = parseColor(this.surfaceData.color);
@@ -122,7 +126,7 @@ export class Surface extends Group {
.cornerRadius(this.surfaceData.radius)
.fill(`rgba(${opaque.r}, ${opaque.g}, ${opaque.b}, ${0.5})`);
- yield* tween(1, value => {
+ yield* this.project.tween(1, value => {
const width = this.surfaceData.width + value.easeOutExpo(0, 100);
const height = this.surfaceData.height + value.easeOutExpo(0, 100);
const radius = this.surfaceData.radius + value.easeOutExpo(0, 50);
@@ -203,13 +207,20 @@ export class Surface extends Group {
.cornerRadius(this.surfaceData.radius)
.fill(this.surfaceData.color);
- this.offset(this.calculateOffset(this.surfaceData));
+ this.offset(this.calculateOffset());
+ }
+
+ public withOrigin(origin: Origin, action: () => void) {
+ const previousOrigin = this.origin();
+ this.origin(origin);
+ action();
+ this.origin(previousOrigin);
}
public handleOriginChange() {
if (!this.surfaceData || this.override) return;
const previousOffset = this.offset();
- const nextOffset = this.calculateOffset(this.surfaceData);
+ const nextOffset = this.calculateOffset();
this.offset(nextOffset);
this.move({
x: -previousOffset.x + nextOffset.x,
@@ -218,7 +229,7 @@ export class Surface extends Group {
}
public calculateOriginDelta(newOrigin: Origin): Vector2d {
- const offset = this.calculateOffset(this.surfaceData);
+ const offset = this.calculateOffset();
const nextOffset = this.calculateOffset(this.surfaceData, newOrigin);
return {
@@ -260,6 +271,27 @@ export class Surface extends Group {
);
}
+ getClientRect(config?: {
+ skipTransform?: boolean;
+ skipShadow?: boolean;
+ skipStroke?: boolean;
+ relativeTo?: Container;
+ }): IRect {
+ if (!this.override || !this.maskData) {
+ return super.getClientRect(config);
+ }
+
+ const position = this.getAbsolutePosition();
+ const offset = this.calculateOffset(this.maskData);
+
+ return {
+ x: position.x - offset.x - this.maskData.width / 2,
+ y: position.y - offset.y - this.maskData.height / 2,
+ width: this.maskData.width,
+ height: this.maskData.height,
+ };
+ }
+
origin: GetSet<Origin, this>;
}
diff --git a/src/components/TextContent.ts b/src/components/TextContent.ts
index eceb2596..0987010e 100644
--- a/src/components/TextContent.ts
+++ b/src/components/TextContent.ts
@@ -1,11 +1,11 @@
import {Text, TextConfig} from 'konva/lib/shapes/Text';
import {GetSet, Vector2d} from 'konva/lib/types';
-import {tween} from '../tweening';
import {ISurfaceChild, SURFACE_CHANGE_EVENT, SurfaceData} from './Surface';
import {ShapeGetClientRectConfig} from 'konva/lib/Shape';
import {_registerNode} from 'konva/lib/Global';
import {Factory} from 'konva/lib/Factory';
import {getNumberValidator} from 'konva/lib/Validators';
+import {Project} from 'MC/Project';
export interface TextContentConfig extends TextConfig {
minWidth?: number;
@@ -14,6 +14,10 @@ export interface TextContentConfig extends TextConfig {
export class TextContent extends Text implements ISurfaceChild {
private contentOffset = 0;
+ public get project(): Project {
+ return <Project>this.getStage();
+ }
+
public constructor(config?: TextContentConfig) {
super({
...config,
@@ -49,13 +53,16 @@ export class TextContent extends Text implements ISurfaceChild {
const from = this.recalculateValues(fromText);
const to = this.recalculateValues(text);
- yield* tween(0.3, value => {
+ yield* this.project.tween(0.3, value => {
this.text(value.text(fromText, text, value.easeInOutCubic()));
this.width(value.easeInOutCubic(from.width, to.width));
this.offset(
value.vector2d(from.offset, to.offset, value.easeInOutCubic()),
);
- this.contentOffset = value.easeInOutCubic(from.contentOffset, to.contentOffset);
+ this.contentOffset = value.easeInOutCubic(
+ from.contentOffset,
+ to.contentOffset,
+ );
this.fire(SURFACE_CHANGE_EVENT, undefined, true);
});
diff --git a/src/flow/index.ts b/src/flow/index.ts
index 3dc999a2..7994f379 100644
--- a/src/flow/index.ts
+++ b/src/flow/index.ts
@@ -1,4 +1,3 @@
export * from './all';
export * from './any';
export * from './loop';
-export * from './sequence';
diff --git a/src/flow/sequence.ts b/src/flow/sequence.ts
deleted file mode 100644
index 792d87aa..00000000
--- a/src/flow/sequence.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-export function* sequence(delay: number, ...sequences: Generator[]): Generator {
- let finished = 0;
- let ticks = 0;
- while (finished < sequences.length) {
- finished = 0;
- ticks++;
-
- const time = ticks / 60;
- const amount = Math.floor(time / delay) + 1;
-
- for (let i = 0; i < Math.min(sequences.length, amount); i++) {
- if (sequences[i].next().done) {
- finished++;
- }
- }
-
- yield;
- }
-}
diff --git a/src/tweening/index.ts b/src/tweening/index.ts
deleted file mode 100644
index ddb01f3f..00000000
--- a/src/tweening/index.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export * from './tween';
-export * from './TimeTween';
diff --git a/src/tweening/tween.ts b/src/tweening/tween.ts
deleted file mode 100644
index b3a46632..00000000
--- a/src/tweening/tween.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import {TimeTween} from './TimeTween';
-
-export function* tween(duration: number, callback: (value: TimeTween) => void): Generator {
- let time = 0;
- const timeTween = new TimeTween(0);
- while (time < duration) {
- timeTween.value = time / duration;
- callback(timeTween);
- time += 1 / 60;
- yield;
- }
- timeTween.value = 1;
- callback(timeTween);
-}
diff --git a/src/utils/index.ts b/src/utils/index.ts
new file mode 100644
index 00000000..2d090d0c
--- /dev/null
+++ b/src/utils/index.ts
@@ -0,0 +1 @@
+export * from './pop';
\ No newline at end of file
diff --git a/src/animations/pop.ts b/src/utils/pop.ts
similarity index 100%
rename from src/animations/pop.ts
rename to src/utils/pop.ts