Skip to content

Visualize Your Ideas With Code

feat: sprites and threading

aarthificiala5416828bfb5
diff --git a/package-lock.json b/package-lock.json
index 4cdcf6e0..d90e738e 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"canvas": "^2.9.0",
+ "image-size": "^1.0.1",
"konva": "^8.3.2",
"mix-color": "^1.1.2",
"ts-loader": "^9.2.6",
@@ -19,6 +20,7 @@
"webpack-dev-server": "^4.7.4"
},
"bin": {
+ "mc-image": "tools/image.mjs",
"mc-render": "tools/render.mjs",
"mc-serve": "tools/serve.mjs"
},
@@ -1879,6 +1881,20 @@
"node": ">= 4"
}
},
+ "node_modules/image-size": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/image-size/-/image-size-1.0.1.tgz",
+ "integrity": "sha512-VAwkvNSNGClRw9mDHhc5Efax8PLlsOGcUTh0T/LIriC8vPA3U5PdqXWqkz406MoYHMKW8Uf9gWr05T/rYB44kQ==",
+ "dependencies": {
+ "queue": "6.0.2"
+ },
+ "bin": {
+ "image-size": "bin/image-size.js"
+ },
+ "engines": {
+ "node": ">=12.0.0"
+ }
+ },
"node_modules/import-local": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/import-local/-/import-local-3.1.0.tgz",
@@ -2898,6 +2914,14 @@
"url": "https://github.com/sponsors/ljharb"
}
},
+ "node_modules/queue": {
+ "version": "6.0.2",
+ "resolved": "https://registry.npmjs.org/queue/-/queue-6.0.2.tgz",
+ "integrity": "sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==",
+ "dependencies": {
+ "inherits": "~2.0.3"
+ }
+ },
"node_modules/queue-microtask": {
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz",
@@ -5648,6 +5672,14 @@
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.0.tgz",
"integrity": "sha512-CmxgYGiEPCLhfLnpPp1MoRmifwEIOgjcHXxOBjv7mY96c+eWScsOP9c112ZyLdWHi0FxHjI+4uVhKYp/gcdRmQ=="
},
+ "image-size": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/image-size/-/image-size-1.0.1.tgz",
+ "integrity": "sha512-VAwkvNSNGClRw9mDHhc5Efax8PLlsOGcUTh0T/LIriC8vPA3U5PdqXWqkz406MoYHMKW8Uf9gWr05T/rYB44kQ==",
+ "requires": {
+ "queue": "6.0.2"
+ }
+ },
"import-local": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/import-local/-/import-local-3.1.0.tgz",
@@ -6341,6 +6373,14 @@
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.6.tgz",
"integrity": "sha512-TIRk4aqYLNoJUbd+g2lEdz5kLWIuTMRagAXxl78Q0RiVjAOugHmeKNGdd3cwo/ktpf9aL9epCfFqWDEKysUlLQ=="
},
+ "queue": {
+ "version": "6.0.2",
+ "resolved": "https://registry.npmjs.org/queue/-/queue-6.0.2.tgz",
+ "integrity": "sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==",
+ "requires": {
+ "inherits": "~2.0.3"
+ }
+ },
"queue-microtask": {
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz",
diff --git a/package.json b/package.json
index dd8c8569..c2ceef12 100644
--- a/package.json
+++ b/package.json
@@ -9,10 +9,12 @@
"prepare": "npm run build",
"build": "tsc",
"test:serve": "node ./tools/serve.mjs ./test/player.ts",
- "test:render": "node ./tools/render.mjs ./test/render.ts"
+ "test:render": "node ./tools/render.mjs ./test/render.ts",
+ "test:image": "node ./tools/image.mjs ./test/img ./test/animations.json"
},
"dependencies": {
"canvas": "^2.9.0",
+ "image-size": "^1.0.1",
"konva": "^8.3.2",
"mix-color": "^1.1.2",
"ts-loader": "^9.2.6",
@@ -23,7 +25,8 @@
},
"bin": {
"mc-serve": "./tools/serve.mjs",
- "mc-render": "./tools/render.mjs"
+ "mc-render": "./tools/render.mjs",
+ "mc-image": "./tools/image.mjs"
},
"files": [
"dist/**/*",
diff --git a/src/Player.ts b/src/Player.ts
index f7b23d36..30ffa307 100644
--- a/src/Player.ts
+++ b/src/Player.ts
@@ -37,6 +37,7 @@ export function Player(factory: () => Project, audioSrc?: string) {
return;
}
+ startTime = performance.now();
project.start();
project.next();
if (audio) {
diff --git a/src/Project.ts b/src/Project.ts
index 834139ff..191fb580 100644
--- a/src/Project.ts
+++ b/src/Project.ts
@@ -13,6 +13,8 @@ import {
waitUntil,
sequence,
threads,
+ ThreadsJoin,
+ ThreadsCancel,
} from './animations';
Konva.autoDrawEnabled = false;
@@ -79,8 +81,9 @@ export class Project extends Stage {
this.scenes.forEach(scene => scene.layer.destroy());
this.scenes = [];
this.frame = 0;
- this.runner = this.threads(join => {
+ this.runner = this.threads((join, cancel) => {
this.join = join;
+ this.cancel = cancel;
return this.runnerFactory(this);
});
}
@@ -101,7 +104,8 @@ export class Project extends Stage {
return frames / this.framesPerSeconds;
}
- public join: (...runners: Generator[]) => Generator;
+ public join: ThreadsJoin;
+ public cancel: ThreadsCancel;
public threads = threads;
public waitFor = waitFor;
public waitUntil = waitUntil;
diff --git a/src/animations/motion.ts b/src/animations/motion.ts
index 1cacf404..bdbc5b0e 100644
--- a/src/animations/motion.ts
+++ b/src/animations/motion.ts
@@ -2,38 +2,43 @@ import {Node} from 'konva/lib/Node';
import {Vector2d} from 'konva/lib/types';
import {Project} from "../Project";
+export interface MoveConfig {
+ absolute?: boolean,
+ speed?: number,
+}
+
export function move(
this: Project,
node: Node,
position: Vector2d,
- absolute?: boolean,
+ config?: MoveConfig,
): Generator;
export function move(
this: Project,
node: Node,
positionX: number,
positionY: number,
- absolute?: boolean,
+ config?: MoveConfig,
): Generator;
export function move(
this: Project,
node: Node,
arg0: number | Vector2d,
- arg1?: number | boolean,
- arg2?: boolean,
+ arg1?: number | MoveConfig,
+ arg2?: MoveConfig,
): Generator {
let delta: Vector2d;
- let absolute: boolean;
+ let config: MoveConfig;
if (typeof arg0 === 'number') {
delta = {x: arg0, y: <number>arg1};
- absolute = arg2;
+ config = arg2 ?? {};
} else {
delta = arg0;
- absolute = <boolean>arg1;
+ config = <MoveConfig>arg1 ?? {};
}
const positionFrom = node.position();
- let positionTo = absolute
+ let positionTo = config.absolute
? delta
: {x: delta.x + positionFrom.x, y: delta.y + positionFrom.y};
@@ -42,7 +47,7 @@ export function move(
Math.pow(positionFrom.y - positionTo.y, 2),
);
- return this.tween(distance / 1000, value =>
- node.position(value.vector2d(positionFrom, positionTo)),
+ return this.tween(distance / 1000 * (config.speed ?? 1), value =>
+ node.position(value.vector2d(positionFrom, positionTo, value.easeInOutQuint())),
);
}
diff --git a/src/animations/threads.ts b/src/animations/threads.ts
index 57f9805d..608aab6a 100644
--- a/src/animations/threads.ts
+++ b/src/animations/threads.ts
@@ -1,23 +1,46 @@
-export function* threads(
- factory: (join: (...tasks: Generator[]) => Generator) => Generator,
-): Generator {
+export interface ThreadsJoin {
+ (...tasks: Generator[]): Generator;
+}
+
+export interface ThreadsCancel {
+ (...tasks: Generator[]): void;
+}
+
+export interface ThreadsFactory {
+ (join: ThreadsJoin, cancel: ThreadsCancel): Generator;
+}
+
+export function* threads(factory: ThreadsFactory): Generator {
let runners: Generator[] = [];
+ let cancelled: Set<Generator> = new Set<Generator>();
+ let previous: Map<Generator, Generator> = new Map<Generator, Generator>();
const join = function* (...tasks: Generator[]): Generator {
while (tasks.find(runner => runners.includes(runner))) {
yield;
}
};
- runners.push(factory(join));
+ const cancel = (...tasks: Generator[]) => {
+ tasks.forEach(task => cancelled.add(task));
+ };
+
+ runners.push(factory(join, cancel));
while (runners.length > 0) {
const newRunners = [];
for (let i = 0; i < runners.length; i++) {
const runner = runners[i];
- const result = runner.next();
+ if (cancelled.has(runner)) {
+ cancelled.delete(runner);
+ continue;
+ }
+
+ const result = runner.next(previous.get(runner));
if (result.value?.next) {
if (!result.done) {
runners.push(runner);
}
+ previous.set(runner, result.value);
+ cancelled.delete(result.value);
runners.push(result.value);
} else if (!result.done) {
newRunners.push(runner);
diff --git a/src/components/Arrow.ts b/src/components/Arrow.ts
index 645be87c..e132c786 100644
--- a/src/components/Arrow.ts
+++ b/src/components/Arrow.ts
@@ -177,15 +177,15 @@ export class Arrow extends Shape<ArrowConfig> {
this.dirty = false;
}
- let start = (this.attrs.start ?? 0) * this.arcLength;
- let end = (this.attrs.end ?? 1) * this.arcLength;
+ let start = this.start() * this.arcLength;
+ let end = this.end() * this.arcLength;
if (start > end) {
[start, end] = [end, start];
}
let offset = start;
const distance = end - start;
- const arrowSize = this.attrs.arrowSize || 0;
+ const arrowSize = this.arrowSize();
const arrowScale =
(distance > arrowSize ? arrowSize : distance < 0 ? 0 : distance) /
arrowSize;
@@ -253,7 +253,7 @@ export class Arrow extends Shape<ArrowConfig> {
tangent: Vector2d,
size: number,
) {
- const arrowSize = (this.attrs.arrowSize || 0) * size;
+ const arrowSize = this.arrowSize() * size;
const offset = this.strokeWidth() / 2;
// noinspection JSSuspiciousNameCombination
const normal = {
@@ -278,12 +278,11 @@ export class Arrow extends Shape<ArrowConfig> {
}
private calculatePath() {
- if (!this.attrs.points || !this.attrs.radius) return;
this.arcLength = 0;
this.segments = [];
- const points: number[] = this.attrs.points;
- const radius = this.attrs.radius || 0;
+ const points: number[] = this.points();
+ const radius = this.radius();
let lastX = points[0];
let lastY = points[1];
@@ -356,13 +355,13 @@ export class Arrow extends Shape<ArrowConfig> {
}
getWidth() {
- const points = (<number[]>this.attrs.points || [0, 0]).filter(
+ const points = (<number[]>this.points()).filter(
(value, index) => index % 2 === 1,
);
return Math.max(...points) - Math.min(...points);
}
getHeight() {
- const points = (<number[]>this.attrs.points || [0, 0]).filter(
+ const points = (<number[]>this.points()).filter(
(value, index) => index % 2 === 0,
);
return Math.max(...points) - Math.min(...points);
@@ -372,6 +371,7 @@ export class Arrow extends Shape<ArrowConfig> {
points: GetSet<number[], this>;
start: GetSet<number, this>;
end: GetSet<number, this>;
+ arrowSize: GetSet<number, this>;
}
Arrow.prototype.className = 'Arrow';
@@ -382,7 +382,7 @@ _registerNode(Arrow);
Factory.addGetterSetter(
Arrow,
'radius',
- 0,
+ 8,
getNumberValidator(),
Arrow.prototype.markAsDirty,
);
@@ -395,3 +395,4 @@ Factory.addGetterSetter(
);
Factory.addGetterSetter(Arrow, 'start', 0, getNumberValidator());
Factory.addGetterSetter(Arrow, 'end', 1, getNumberValidator());
+Factory.addGetterSetter(Arrow, 'arrowSize', 16, getNumberValidator());
diff --git a/src/components/Connection.ts b/src/components/Connection.ts
index bac0bb89..4e099460 100644
--- a/src/components/Connection.ts
+++ b/src/components/Connection.ts
@@ -6,7 +6,8 @@ import {GetSet, Vector2d} from 'konva/lib/types';
import {Direction} from '../types/Origin';
import {Context} from 'konva/lib/Context';
import {TimeTween} from '../animations';
-import {SURFACE_CHANGE_EVENT} from 'MC/components/Surface';
+import {SURFACE_CHANGE_EVENT} from './Surface';
+import {Project} from '../Project';
export interface ConnectionPoint {
node: Node;
@@ -38,6 +39,10 @@ export class Connection extends Arrow {
private nodeCache: Record<string, Node> = {};
private markAsDirtyCallback: () => void;
+ public get project(): Project {
+ return <Project>this.getStage();
+ }
+
constructor(config?: ConnectionConfig) {
super(config);
}
@@ -116,12 +121,13 @@ export class Connection extends Arrow {
return;
}
- const fromRect = from.node.getClientRect();
+ const fromRect = from.node.getClientRect({relativeTo: this.getLayer()});
fromRect.width /= 2;
fromRect.height /= 2;
fromRect.x += fromRect.width;
fromRect.y += fromRect.height;
- const toRect = to.node.getClientRect();
+ const toRect = to.node.getClientRect({relativeTo: this.getLayer()});
+
toRect.width /= 2;
toRect.height /= 2;
toRect.x += toRect.width;
diff --git a/src/components/Layout.ts b/src/components/Layout.ts
index a0f711b3..ab2f2cfe 100644
--- a/src/components/Layout.ts
+++ b/src/components/Layout.ts
@@ -1,21 +1,30 @@
import {Group} from 'konva/lib/Group';
import {Container, ContainerConfig} from 'konva/lib/Container';
-import {Center} from 'MC/types/Origin';
+import {Center} from '../types/Origin';
import {GetSet, IRect} from 'konva/lib/types';
import {_registerNode} from 'konva/lib/Global';
import {Factory} from 'konva/lib/Factory';
-import {ISurfaceChild, Surface, SURFACE_CHANGE_EVENT, SurfaceData} from 'MC/components/Surface';
+import {
+ ISurfaceChild,
+ Surface,
+ SURFACE_CHANGE_EVENT,
+ SurfaceData,
+} from './Surface';
import {Shape} from 'konva/lib/Shape';
-import {getNumberValidator} from 'konva/lib/Validators';
+import {getNumberValidator, getStringValidator} from 'konva/lib/Validators';
export interface LayoutConfig extends ContainerConfig {
direction?: Center;
padding?: number;
+ background?: string;
}
export class Layout extends Group implements ISurfaceChild {
public direction: GetSet<Center, this>;
public padding: GetSet<number, this>;
+ public background: GetSet<string, this>;
+
+ private contentSize = {width: 0, height: 0};
constructor(config?: LayoutConfig) {
super(config);
@@ -26,8 +35,8 @@ export class Layout extends Group implements ISurfaceChild {
getSurfaceData(): SurfaceData {
return {
- ...this.getClientRect(),
- color: 'rgba(36, 36, 36, 1)',
+ ...this.getClientRect({relativeTo: this.getLayer()}),
+ color: this.background(),
radius: 20,
};
}
@@ -52,28 +61,32 @@ export class Layout extends Group implements ISurfaceChild {
if (!this.children) return;
const padding = this.attrs.padding ?? 20;
- let height = 0;
- let width = 0;
+ this.contentSize.height = 0;
+ this.contentSize.width = 0;
if (this.attrs.direction === Center.Horizontal) {
for (const child of this.children) {
- const rect = child.getClientRect();
- const offset = child instanceof Surface ? child.calculateOffset() : {x: 0, y: 0};
- height = Math.max(height, rect.height);
- width += rect.width / 2;
- child.position({x: width, y: offset.y});
- width += rect.width / 2 + padding;
+ const rect = child.getClientRect({relativeTo: child.getLayer()});
+ const offset =
+ child instanceof Surface ? child.calculateOffset() : {x: 0, y: 0};
+ this.contentSize.height = Math.max(this.contentSize.height, rect.height);
+ this.contentSize.width += rect.width / 2;
+ child.position({x: this.contentSize.width, y: offset.y});
+ this.contentSize.width += rect.width / 2 + padding;
}
- this.offsetX((width - padding) / 2);
+ this.contentSize.width -= padding;
+ this.offsetX((this.contentSize.width) / 2);
} else {
for (const child of this.children) {
- const rect = child.getClientRect();
- const offset = child instanceof Surface ? child.calculateOffset() : {x: 0, y: 0};
- width = Math.max(width, rect.width);
- height += rect.height / 2;
- child.position({x: offset.x, y: height});
- height += rect.height / 2 + padding;
+ const rect = child.getClientRect({relativeTo: child.getLayer()});
+ const offset =
+ child instanceof Surface ? child.calculateOffset() : {x: 0, y: 0};
+ this.contentSize.width = Math.max(this.contentSize.width, rect.width);
+ this.contentSize.height += rect.height / 2;
+ child.position({x: offset.x, y: this.contentSize.height});
+ this.contentSize.height += rect.height / 2 + padding;
}
- this.offsetY((height - padding) / 2);
+ this.contentSize.height -= padding;
+ this.offsetY((this.contentSize.height) / 2);
}
this.fire(SURFACE_CHANGE_EVENT, undefined, true);
@@ -85,14 +98,20 @@ export class Layout extends Group implements ISurfaceChild {
skipStroke?: boolean;
relativeTo?: Container;
}): IRect {
- const padding = this.attrs.padding ?? 20;
- const rect = super.getClientRect(config);
- rect.x -= padding;
- rect.y -= padding;
- rect.width += padding * 2;
- rect.height += padding * 2;
+ const padding = this.padding();
+ const position = this.getAbsolutePosition(config?.relativeTo);
+ const scale = this.getAbsoluteScale(config?.relativeTo);
+ const size = {
+ width: (this.contentSize.width + padding * 2) * scale.x,
+ height: (this.contentSize.height + padding * 2) * scale.y,
+ };
- return rect;
+ return {
+ x: position.x - size.width / 2,
+ y: position.y - size.height / 2,
+ width: size.width,
+ height: size.height,
+ };
}
}
@@ -115,3 +134,4 @@ Factory.addGetterSetter(
// @ts-ignore
Layout.prototype.recalculate,
);
+Factory.addGetterSetter(Layout, 'background', '#242424', getStringValidator());
diff --git a/src/components/Sprite.ts b/src/components/Sprite.ts
index e271bcf4..c22423e5 100644
--- a/src/components/Sprite.ts
+++ b/src/components/Sprite.ts
@@ -1,22 +1,175 @@
import {Shape, ShapeConfig} from 'konva/lib/Shape';
import {Context} from 'konva/lib/Context';
import {Util} from 'konva/lib/Util';
+import {GetSet} from 'konva/lib/types';
+import {Factory} from 'konva/lib/Factory';
+import {
+ getBooleanValidator,
+ getNumberValidator,
+ getStringValidator,
+} from 'konva/lib/Validators';
+import {Project} from 'MC/Project';
+import {
+ ISurfaceChild,
+ SURFACE_CHANGE_EVENT,
+ SurfaceData,
+} from 'MC/components/Surface';
+
+interface FrameData {
+ fileName: string;
+ url: string;
+ data: number[];
+ width: number;
+ height: number;
+}
+
+interface SpriteData {
+ animations: Record<string, {frames: FrameData[]}>;
+ skins: Record<string, FrameData>;
+}
export interface SpriteConfig extends ShapeConfig {
- image: string;
+ animationData: SpriteData;
+ animation: string;
+ skin?: string;
+ playing?: boolean;
+ fps?: number;
}
-export class Sprite extends Shape {
- private readonly image: HTMLImageElement;
+const COMPUTE_CANVAS_SIZE = 1024;
+
+export class Sprite extends Shape implements ISurfaceChild {
+ public animation: GetSet<string, this>;
+ public skin: GetSet<string, this>;
+ public playing: GetSet<boolean, this>;
+ public fps: GetSet<number, this>;
+
+ private readonly animationData: SpriteData;
+ private frame: FrameData = {
+ height: 0,
+ width: 0,
+ url: '',
+ data: [],
+ fileName: '',
+ };
+ private frameId: number = 0;
+ private stopped: boolean = false;
+ private readonly computeCanvas: HTMLCanvasElement;
+
+ public get context(): CanvasRenderingContext2D {
+ return this.computeCanvas.getContext('2d');
+ }
+
+ public get project(): Project {
+ return <Project>this.getStage();
+ }
constructor(config?: SpriteConfig) {
super(config);
- this.image = Util.createImageElement();
- this.image.src = config?.image;
+ this.animationData = config.animationData;
+ this.computeCanvas = Util.createCanvasElement();
+ this.computeCanvas.width = COMPUTE_CANVAS_SIZE;
+ this.computeCanvas.height = COMPUTE_CANVAS_SIZE;
+
+ this.recalculate();
+ }
+
+ getSurfaceData(): SurfaceData {
+ return {
+ ...this.getClientRect({relativeTo: this.getLayer()}),
+ color: '#58817b',
+ radius: 8,
+ };
}
_sceneFunc(context: Context) {
+ context.save();
context._context.imageSmoothingEnabled = false;
- context.drawImage(this.image, -24 * 20, -24 * 20, 24 * 40, 24 * 40);
+ context.drawImage(
+ this.computeCanvas,
+ 0,
+ 0,
+ this.frame.width,
+ this.frame.height,
+ 0,
+ 0,
+ this.frame.width,
+ this.frame.height,
+ );
+ context.restore();
+ }
+
+ private recalculate() {
+ const skin = this.animationData.skins[this.skin()];
+ const animation = this.animationData.animations[this.animation()];
+ if (!animation || animation.frames.length === 0) return;
+
+ this.frameId %= animation.frames.length;
+ this.frame = animation.frames[this.frameId];
+ this.width(this.frame.width);
+ this.offsetX(this.frame.width / 2);
+ this.height(this.frame.height);
+ this.offsetY(this.frame.height / 2);
+
+ const frameData = this.context.createImageData(this.frame.width, this.frame.height);
+
+ if (skin) {
+ for (let y = 0; y < this.frame.height; y++) {
+ for (let x = 0; x < this.frame.width; x++) {
+ const id = (y * this.frame.width + x) * 4;
+ const skinX = this.frame.data[id];
+ const skinY = this.frame.data[id + 1];
+ const skinId = ((skin.height - 1 - skinY) * skin.width + skinX) * 4;
+
+ frameData.data[id] = skin.data[skinId];
+ frameData.data[id + 1] = skin.data[skinId + 1];
+ frameData.data[id + 2] = skin.data[skinId + 2];
+ frameData.data[id + 3] = this.frame.data[id + 3] * skin.data[skinId + 3];
+ }
+ }
+ } else {
+ frameData.data.set(this.frame.data);
+ }
+
+ this.context.clearRect(0, 0, this.frame.width, this.frame.height);
+ this.context.putImageData(frameData, 0, 0);
+
+ this.fire(SURFACE_CHANGE_EVENT, undefined, true);
+ }
+
+ public play(): Generator {
+ this.stopped = false;
+ return this.playRunner();
+ }
+
+ private *playRunner() {
+ while (!this.stopped) {
+ if (this.playing()) {
+ this.frameId++;
+ this.recalculate();
+ }
+ yield* this.project.waitFor(1 / this.fps());
+ }
+ }
+
+ public stop() {
+ this.stopped = true;
+ }
+
+ public *waitForFrame(frame: number): Generator {
+ let limit = 1000;
+ while (this.frameId !== frame && limit > 0) {
+ limit--;
+ yield;
+ }
+
+ if (limit === 0) {
+ console.warn(`Sprite.waitForFrame cancelled`);
+ }
}
}
+
+Factory.addGetterSetter(Sprite, 'animation', '', getStringValidator());
+Factory.addGetterSetter(Sprite, 'skin', '', getStringValidator());
+Factory.addGetterSetter(Sprite, 'playing', false, getBooleanValidator());
+Factory.addGetterSetter(Sprite, 'fps', 10, getNumberValidator());
diff --git a/src/components/Surface.ts b/src/components/Surface.ts
index abe1ccfb..1c39b7b2 100644
--- a/src/components/Surface.ts
+++ b/src/components/Surface.ts
@@ -281,14 +281,17 @@ export class Surface extends Group {
return super.getClientRect(config);
}
- const position = this.getAbsolutePosition();
+ const position = this.getAbsolutePosition(this.getLayer());
+ const scale = this.getAbsoluteScale(this.getLayer());
const offset = this.calculateOffset(this.maskData);
+ offset.x *= scale.x;
+ offset.y *= scale.y;
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,
+ x: position.x - offset.x - this.maskData.width * scale.x / 2,
+ y: position.y - offset.y - this.maskData.height * scale.y / 2,
+ width: this.maskData.width * scale.x,
+ height: this.maskData.height * scale.y,
};
}
diff --git a/src/components/TextContent.ts b/src/components/TextContent.ts
index 0987010e..b06d8a35 100644
--- a/src/components/TextContent.ts
+++ b/src/components/TextContent.ts
@@ -35,7 +35,7 @@ export class TextContent extends Text implements ISurfaceChild {
getSurfaceData(): SurfaceData {
return {
- ...this.getClientRect(),
+ ...this.getClientRect({relativeTo: this.getLayer()}),
radius: 40,
color: '#c0b3a3',
};
diff --git a/tsconfig.json b/tsconfig.json
index 6c142629..7fbd1a31 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -8,6 +8,7 @@
"target": "es6",
"declaration": true,
"moduleResolution": "node",
+ "resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"paths": {
"MC/*": [
diff --git a/tsconfig.project.json b/tsconfig.project.json
index a8d0fd37..b8b15e08 100644
--- a/tsconfig.project.json
+++ b/tsconfig.project.json
@@ -7,6 +7,7 @@
"module": "esnext",
"target": "es6",
"moduleResolution": "node",
+ "resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"paths": {
"MC/*": [