Skip to content

Visualize Your Ideas With Code

feature: separate origin from offset

aarthificial34072f3a3ba0
diff --git a/src/Project.ts b/src/Project.ts
index 0d5b2da1..eeac1735 100644
--- a/src/Project.ts
+++ b/src/Project.ts
@@ -8,7 +8,7 @@ import {Konva} from 'konva/lib/Global';
import {Thread, ThreadsCallback} from './threading';
import {Scene, SceneRunner} from './Scene';
import {SimpleEventDispatcher} from 'strongly-typed-events';
-import {PlayerState} from './player/Player';
+import {Origin} from './types';
Konva.autoDrawEnabled = false;
@@ -73,6 +73,7 @@ export class Project extends Stage {
y: 0,
width: this.width(),
height: this.height(),
+ origin: Origin.TopLeft,
fill: '#141414',
});
diff --git a/src/components/Surface.ts b/src/components/Surface.ts
index b4887f19..578b9802 100644
--- a/src/components/Surface.ts
+++ b/src/components/Surface.ts
@@ -28,7 +28,6 @@ export interface CircleMask {
export interface SurfaceConfig extends ContainerConfig {
ref?: Reference<Surface>;
radius?: number;
- origin?: Origin;
circleMask?: CircleMask;
background?: string;
child?: Node;
@@ -123,8 +122,6 @@ export class Surface extends Group {
const opaque = parseColor(this.background());
this.ripple.show();
this.ripple
- .offsetX(this.layoutData.width / 2)
- .offsetY(this.layoutData.height / 2)
.width(this.layoutData.width)
.height(this.layoutData.height)
.cornerRadius(this.radius())
@@ -136,8 +133,6 @@ export class Surface extends Group {
const radius = this.radius() + easeOutExpo(value, 0, 50);
this.ripple
- .offsetX(width / 2)
- .offsetY(height / 2)
.width(width)
.height(height)
.cornerRadius(radius)
@@ -160,8 +155,6 @@ export class Surface extends Group {
return;
} else if (this.surfaceMask === null) {
this.box
- .offsetX(5000)
- .offsetY(5000)
.position({x: 0, y: 0})
.width(10000)
.height(100000);
@@ -177,7 +170,6 @@ export class Surface extends Group {
);
this.surfaceMask = data;
- this.offset(newOffset);
child.scaleX(scale);
child.scaleY(scale);
child.position(getOriginDelta(data, Origin.Middle, child.getOrigin()));
@@ -229,8 +221,6 @@ export class Surface extends Group {
if (!this.box) return;
const size = this.getLayoutSize();
this.box
- .offsetX(size.width / 2)
- .offsetY(size.height / 2)
.width(size.width)
.height(size.height);
}
@@ -242,12 +232,10 @@ export class Surface extends Group {
private drawMask(ctx: Context) {
if (this.surfaceMask) {
- const offset = this.offsetY();
- const newOffset = getOriginOffset(this.surfaceMask, this.getOrigin());
CanvasHelper.roundRect(
ctx,
-this.surfaceMask.width / 2,
- -this.surfaceMask.height / 2 + offset - newOffset.y,
+ -this.surfaceMask.height / 2,
this.surfaceMask.width,
this.surfaceMask.height,
this.surfaceMask.radius,
diff --git a/src/patches/Node.ts b/src/patches/Node.ts
index 42f620ba..b76c6e88 100644
--- a/src/patches/Node.ts
+++ b/src/patches/Node.ts
@@ -11,6 +11,8 @@ import {GetSet, IRect, Vector2d} from 'konva/lib/types';
import {Factory} from 'konva/lib/Factory';
import {Rect} from 'konva/lib/shapes/Rect';
import {Container} from 'konva/lib/Container';
+import {Transform} from 'konva/lib/Util';
+import {Konva} from 'konva/lib/Global';
declare module 'konva/lib/Node' {
export interface Node {
@@ -50,7 +52,7 @@ declare module 'konva/lib/Node' {
*
* It will cause the layout of this node and all its ancestors to be recalculated before drawing the next frame.
*/
- markDirty(): void;
+ markDirty(force?: boolean): void;
/**
* Check if this node is dirty.
@@ -80,20 +82,12 @@ Node.prototype.setPadd = function (this: Node, value: PossibleSpacing) {
return this;
};
-Node.prototype.getPadd = function (this: Node) {
- return this.attrs.padd ?? new Spacing();
-};
-
Node.prototype.setMargin = function (this: Node, value: PossibleSpacing) {
this.attrs.margin = new Spacing(value);
this.markDirty();
return this;
};
-Node.prototype.getMargin = function (this: Node) {
- return this.attrs.margin ?? new Spacing();
-};
-
Node.prototype.setOrigin = function (this: Node, value: Origin) {
this.attrs.origin = value;
this.markDirty();
@@ -119,7 +113,6 @@ Node.prototype.getOriginOffset = function (
return getOriginDelta(
this.getLayoutSize(custom),
this._centroid ? Origin.Middle : Origin.TopLeft,
- // Origin.Middle,
custom?.origin ?? this.getOrigin(),
);
};
@@ -145,13 +138,19 @@ Node.prototype.updateLayout = function (this: Node): void {
}
};
-Node.prototype.recalculateLayout = function (this: Node): void {
- this.attrs.dirty = false;
- this.offset(this.getOriginOffset());
-};
+Node.prototype.recalculateLayout = function (this: Node): void {};
-Node.prototype.markDirty = function (this: Node): void {
+Node.prototype.markDirty = function (this: Node, force: boolean = false): void {
this.attrs.dirty = true;
+ if (
+ force ||
+ // When the layout size changes and the origin is other than default,
+ // the transform will also most likely change.
+ (this._centroid ? Origin.Middle : Origin.TopLeft) !== this.origin()
+ ) {
+ this._clearCache('transform');
+ this._clearCache('absoluteTransform');
+ }
};
Node.prototype.isDirty = function (this: Node): boolean {
@@ -211,16 +210,31 @@ Node.prototype.setY = function (this: Node, value: number) {
const super_setWidth = Node.prototype.setWidth;
Node.prototype.setWidth = function (this: Node, value: number) {
- if (this.attrs.width !== value) this.markDirty();
+ if (this.attrs.width !== value) {
+ this.markDirty();
+ }
return super_setWidth.call(this, value);
};
const super_setHeight = Node.prototype.setHeight;
Node.prototype.setHeight = function (this: Node, value: number) {
- if (this.attrs.height !== value) this.markDirty();
+ if (this.attrs.height !== value) {
+ this.markDirty();
+ }
return super_setHeight.call(this, value);
};
-Factory.addGetterSetter(Node, 'padd');
-Factory.addGetterSetter(Node, 'margin');
-Factory.addGetterSetter(Node, 'origin');
+const super__getTransform = Node.prototype._getTransform;
+Node.prototype._getTransform = function () {
+ const m = super__getTransform.call(this);
+ const offset = this.getOriginOffset();
+ if (offset.x !== 0 || offset.y !== 0) {
+ m.translate(-1 * offset.x, -1 * offset.y);
+ }
+ m.dirty = false;
+ return m;
+};
+
+Factory.addGetterSetter(Node, 'padd', new Spacing());
+Factory.addGetterSetter(Node, 'margin', new Spacing());
+Factory.addGetterSetter(Node, 'origin', Origin.Middle);