@flighthq/node 0.2.0 → 0.2.1-next.410.a23f189
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/boundsRectangle.d.ts.map +1 -1
- package/dist/boundsRectangle.js +9 -9
- package/dist/boundsRectangle.js.map +1 -1
- package/dist/hasAppearance.d.ts.map +1 -1
- package/dist/hasAppearance.js +0 -1
- package/dist/hasAppearance.js.map +1 -1
- package/dist/hasBlendMode.d.ts +3 -0
- package/dist/hasBlendMode.d.ts.map +1 -0
- package/dist/hasBlendMode.js +4 -0
- package/dist/hasBlendMode.js.map +1 -0
- package/dist/hasTransform2d.js +2 -2
- package/dist/hasTransform2d.js.map +1 -1
- package/dist/hasTransform3d.d.ts.map +1 -1
- package/dist/hasTransform3d.js +7 -3
- package/dist/hasTransform3d.js.map +1 -1
- package/dist/hierarchy.js +4 -4
- package/dist/hierarchy.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +3 -0
- package/dist/node.js.map +1 -1
- package/dist/revision.d.ts.map +1 -1
- package/dist/revision.js +16 -5
- package/dist/revision.js.map +1 -1
- package/dist/transform2d.d.ts +8 -5
- package/dist/transform2d.d.ts.map +1 -1
- package/dist/transform2d.js +53 -20
- package/dist/transform2d.js.map +1 -1
- package/dist/transform3d.d.ts +10 -3
- package/dist/transform3d.d.ts.map +1 -1
- package/dist/transform3d.js +79 -13
- package/dist/transform3d.js.map +1 -1
- package/dist/viewport.d.ts.map +1 -1
- package/dist/viewport.js +3 -2
- package/dist/viewport.js.map +1 -1
- package/package.json +5 -5
- package/src/boundsRectangle.test.ts +2 -2
- package/src/hasAppearance.test.ts +0 -10
- package/src/hasBlendMode.test.ts +25 -0
- package/src/hasTransform2d.test.ts +2 -2
- package/src/hasTransform3d.test.ts +18 -19
- package/src/hierarchy.test.ts +13 -13
- package/src/transform2d.test.ts +113 -51
- package/src/transform3d.test.ts +302 -100
package/src/transform2d.test.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { getEntityRuntime } from '@flighthq/entity';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
cloneMatrix,
|
|
4
|
+
createMatrix,
|
|
5
|
+
createTransform2D,
|
|
6
|
+
createVector2,
|
|
7
|
+
equalsMatrix,
|
|
8
|
+
multiplyMatrix,
|
|
9
|
+
setMatrix,
|
|
10
|
+
} from '@flighthq/geometry';
|
|
3
11
|
import { addNodeChild, createNode } from '@flighthq/node';
|
|
4
12
|
import type { HasTransform2D, HasTransform2DRuntime, Matrix, Node, NodeRuntime } from '@flighthq/types';
|
|
5
13
|
|
|
@@ -8,10 +16,13 @@ import { invalidateNodeLocalTransform } from './revision';
|
|
|
8
16
|
import {
|
|
9
17
|
convertNodeVector2GlobalToLocal,
|
|
10
18
|
convertNodeVector2LocalToGlobal,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
ensureNodeLocalMatrix,
|
|
20
|
+
ensureNodeWorldMatrix,
|
|
21
|
+
getNodeLocalMatrix,
|
|
22
|
+
getNodeTransform2D,
|
|
23
|
+
getNodeWorldMatrix,
|
|
24
|
+
setNodeLocalMatrix,
|
|
25
|
+
setNodeTransform2D,
|
|
15
26
|
} from './transform2d';
|
|
16
27
|
|
|
17
28
|
function createTestNode(): TestNode {
|
|
@@ -139,48 +150,48 @@ describe('convertNodeVector2LocalToGlobal', () => {
|
|
|
139
150
|
});
|
|
140
151
|
});
|
|
141
152
|
|
|
142
|
-
describe('
|
|
153
|
+
describe('ensureNodeLocalMatrix', () => {
|
|
143
154
|
it('computes local transform the first time', () => {
|
|
144
155
|
const runtime = getEntityRuntime(node) as HasTransform2DRuntime;
|
|
145
|
-
expect(runtime.
|
|
146
|
-
|
|
147
|
-
expect(runtime.
|
|
156
|
+
expect(runtime.localMatrix).toBeNull();
|
|
157
|
+
ensureNodeLocalMatrix(node);
|
|
158
|
+
expect(runtime.localMatrix).not.toBeNull();
|
|
148
159
|
});
|
|
149
160
|
|
|
150
161
|
it('recomputes if the local transform ID has changed', () => {
|
|
151
162
|
const runtime = getEntityRuntime(node) as NodeRuntime<HasTransform2D> & HasTransform2DRuntime;
|
|
152
|
-
|
|
153
|
-
const cache = cloneAndInvalidateMatrix(runtime.
|
|
163
|
+
ensureNodeLocalMatrix(node);
|
|
164
|
+
const cache = cloneAndInvalidateMatrix(runtime.localMatrix);
|
|
154
165
|
runtime.localTransformId++;
|
|
155
|
-
|
|
156
|
-
expect(equalsMatrix(runtime.
|
|
166
|
+
ensureNodeLocalMatrix(node);
|
|
167
|
+
expect(equalsMatrix(runtime.localMatrix, cache)).toBe(true);
|
|
157
168
|
});
|
|
158
169
|
});
|
|
159
170
|
|
|
160
|
-
describe('
|
|
171
|
+
describe('ensureNodeWorldMatrix', () => {
|
|
161
172
|
it('computes world transform the first time', () => {
|
|
162
173
|
const runtime = getEntityRuntime(node) as NodeRuntime<HasTransform2D> & HasTransform2DRuntime;
|
|
163
|
-
expect(runtime.
|
|
164
|
-
|
|
165
|
-
expect(runtime.
|
|
174
|
+
expect(runtime.worldMatrix).toBeNull();
|
|
175
|
+
ensureNodeWorldMatrix(node);
|
|
176
|
+
expect(runtime.worldMatrix).not.toBeNull();
|
|
166
177
|
});
|
|
167
178
|
|
|
168
179
|
it('computes world transform for a parent for the first time', () => {
|
|
169
180
|
const parent = createTestNode();
|
|
170
181
|
addNodeChild(parent, node);
|
|
171
182
|
const parentState = getEntityRuntime(node) as HasTransform2DRuntime;
|
|
172
|
-
expect(parentState.
|
|
173
|
-
|
|
174
|
-
expect(parentState.
|
|
183
|
+
expect(parentState.worldMatrix).toBeNull();
|
|
184
|
+
ensureNodeWorldMatrix(node);
|
|
185
|
+
expect(parentState.worldMatrix).not.toBeNull();
|
|
175
186
|
});
|
|
176
187
|
|
|
177
188
|
it('recomputes if the local transform ID has changed', () => {
|
|
178
189
|
const runtime = getEntityRuntime(node) as NodeRuntime<HasTransform2D> & HasTransform2DRuntime;
|
|
179
|
-
|
|
180
|
-
const cache = cloneAndInvalidateMatrix(runtime.
|
|
190
|
+
ensureNodeWorldMatrix(node);
|
|
191
|
+
const cache = cloneAndInvalidateMatrix(runtime.worldMatrix);
|
|
181
192
|
runtime.localTransformId++;
|
|
182
|
-
|
|
183
|
-
expect(equalsMatrix(runtime.
|
|
193
|
+
ensureNodeWorldMatrix(node);
|
|
194
|
+
expect(equalsMatrix(runtime.worldMatrix, cache)).toBe(true);
|
|
184
195
|
});
|
|
185
196
|
|
|
186
197
|
it('recomputes if the parent transform ID has changed', () => {
|
|
@@ -188,25 +199,25 @@ describe('ensureNodeWorldTransformMatrix', () => {
|
|
|
188
199
|
addNodeChild(parent, node);
|
|
189
200
|
const runtime = getEntityRuntime(node) as HasTransform2DRuntime;
|
|
190
201
|
const parentState = getEntityRuntime(parent) as NodeRuntime<HasTransform2D> & HasTransform2DRuntime;
|
|
191
|
-
|
|
192
|
-
const cache = cloneAndInvalidateMatrix(runtime.
|
|
202
|
+
ensureNodeWorldMatrix(node);
|
|
203
|
+
const cache = cloneAndInvalidateMatrix(runtime.worldMatrix);
|
|
193
204
|
parentState.worldTransformId++;
|
|
194
|
-
|
|
195
|
-
expect(equalsMatrix(runtime.
|
|
205
|
+
ensureNodeWorldMatrix(node);
|
|
206
|
+
expect(equalsMatrix(runtime.worldMatrix, cache)).toBe(true);
|
|
196
207
|
});
|
|
197
208
|
});
|
|
198
209
|
|
|
199
|
-
describe('
|
|
210
|
+
describe('getNodeLocalMatrix', () => {
|
|
200
211
|
it('ensures local transform', () => {
|
|
201
212
|
const runtime = getEntityRuntime(node) as HasTransform2DRuntime;
|
|
202
|
-
expect(runtime.
|
|
203
|
-
|
|
204
|
-
expect(runtime.
|
|
213
|
+
expect(runtime.localMatrix).toBeNull();
|
|
214
|
+
getNodeLocalMatrix(node);
|
|
215
|
+
expect(runtime.localMatrix).not.toBeNull();
|
|
205
216
|
});
|
|
206
217
|
|
|
207
218
|
it('returns local transform', () => {
|
|
208
|
-
const transform =
|
|
209
|
-
expect(transform).equals((getEntityRuntime(node) as HasTransform2DRuntime).
|
|
219
|
+
const transform = getNodeLocalMatrix(node);
|
|
220
|
+
expect(transform).equals((getEntityRuntime(node) as HasTransform2DRuntime).localMatrix);
|
|
210
221
|
});
|
|
211
222
|
|
|
212
223
|
it('offsets translation by the pivot so the pivot point lands at (x, y)', () => {
|
|
@@ -214,7 +225,7 @@ describe('getNodeLocalTransformMatrix', () => {
|
|
|
214
225
|
node.y = 50;
|
|
215
226
|
node.pivotX = 10;
|
|
216
227
|
node.pivotY = 20;
|
|
217
|
-
const transform =
|
|
228
|
+
const transform = getNodeLocalMatrix(node);
|
|
218
229
|
expect(transform.tx).toBeCloseTo(90); // 100 - 1 * 10
|
|
219
230
|
expect(transform.ty).toBeCloseTo(30); // 50 - 1 * 20
|
|
220
231
|
});
|
|
@@ -224,23 +235,74 @@ describe('getNodeLocalTransformMatrix', () => {
|
|
|
224
235
|
node.scaleY = 3;
|
|
225
236
|
node.pivotX = 10;
|
|
226
237
|
node.pivotY = 10;
|
|
227
|
-
const transform =
|
|
238
|
+
const transform = getNodeLocalMatrix(node);
|
|
228
239
|
expect(transform.tx).toBeCloseTo(-20); // 0 - 2 * 10
|
|
229
240
|
expect(transform.ty).toBeCloseTo(-30); // 0 - 3 * 10
|
|
230
241
|
});
|
|
231
242
|
});
|
|
232
243
|
|
|
233
|
-
describe('
|
|
244
|
+
describe('getNodeTransform2D', () => {
|
|
245
|
+
it('reads the node fields into a carrier', () => {
|
|
246
|
+
node.x = 3;
|
|
247
|
+
node.y = 4;
|
|
248
|
+
node.rotation = 30;
|
|
249
|
+
node.scaleX = 2;
|
|
250
|
+
node.skewY = 5;
|
|
251
|
+
const out = createTransform2D();
|
|
252
|
+
getNodeTransform2D(out, node);
|
|
253
|
+
expect(out).toMatchObject({ rotation: 30, scaleX: 2, skewY: 5, x: 3, y: 4 });
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
describe('getNodeWorldMatrix', () => {
|
|
234
258
|
it('ensures world transform', () => {
|
|
235
259
|
const runtime = getEntityRuntime(node) as HasTransform2DRuntime;
|
|
236
|
-
expect(runtime.
|
|
237
|
-
|
|
238
|
-
expect(runtime.
|
|
260
|
+
expect(runtime.worldMatrix).toBeNull();
|
|
261
|
+
getNodeWorldMatrix(node);
|
|
262
|
+
expect(runtime.worldMatrix).not.toBeNull();
|
|
239
263
|
});
|
|
240
264
|
|
|
241
265
|
it('returns local transform', () => {
|
|
242
|
-
const transform =
|
|
243
|
-
expect(transform).equals((getEntityRuntime(node) as HasTransform2DRuntime).
|
|
266
|
+
const transform = getNodeWorldMatrix(node);
|
|
267
|
+
expect(transform).equals((getEntityRuntime(node) as HasTransform2DRuntime).worldMatrix);
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
describe('setNodeLocalMatrix', () => {
|
|
272
|
+
it('round-trips the effective transform through the local matrix', () => {
|
|
273
|
+
setNodeLocalMatrix(node, createMatrix(2, 0, 0, 3, 10, 20));
|
|
274
|
+
expect(node.scaleX).toBeCloseTo(2);
|
|
275
|
+
expect(node.scaleY).toBeCloseTo(3);
|
|
276
|
+
expect(node.x).toBeCloseTo(10);
|
|
277
|
+
expect(node.y).toBeCloseTo(20);
|
|
278
|
+
const m = getNodeLocalMatrix(node);
|
|
279
|
+
expect(m.a).toBeCloseTo(2);
|
|
280
|
+
expect(m.d).toBeCloseTo(3);
|
|
281
|
+
expect(m.tx).toBeCloseTo(10);
|
|
282
|
+
expect(m.ty).toBeCloseTo(20);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('resets pivot and absorbs its offset into translation', () => {
|
|
286
|
+
node.pivotX = 5;
|
|
287
|
+
node.pivotY = 5;
|
|
288
|
+
invalidateNodeLocalTransform(node);
|
|
289
|
+
setNodeLocalMatrix(node, createMatrix(1, 0, 0, 1, 7, 8));
|
|
290
|
+
expect(node.pivotX).toBe(0);
|
|
291
|
+
expect(node.pivotY).toBe(0);
|
|
292
|
+
expect(node.x).toBeCloseTo(7);
|
|
293
|
+
expect(node.y).toBeCloseTo(8);
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
describe('setNodeTransform2D', () => {
|
|
298
|
+
it('copies carrier fields into the node and rebuilds the matrix', () => {
|
|
299
|
+
setNodeTransform2D(node, createTransform2D(10, 20, 0, 2, 4));
|
|
300
|
+
expect(node).toMatchObject({ scaleX: 2, scaleY: 4, x: 10, y: 20 });
|
|
301
|
+
const m = getNodeLocalMatrix(node);
|
|
302
|
+
expect(m.a).toBeCloseTo(2);
|
|
303
|
+
expect(m.d).toBeCloseTo(4);
|
|
304
|
+
expect(m.tx).toBeCloseTo(10);
|
|
305
|
+
expect(m.ty).toBeCloseTo(20);
|
|
244
306
|
});
|
|
245
307
|
});
|
|
246
308
|
|
|
@@ -262,8 +324,8 @@ describe('skew', () => {
|
|
|
262
324
|
withoutSkew.scaleY = 3;
|
|
263
325
|
invalidateNodeLocalTransform(withoutSkew);
|
|
264
326
|
|
|
265
|
-
const mSkew =
|
|
266
|
-
const mNoSkew =
|
|
327
|
+
const mSkew = getNodeLocalMatrix(withSkew);
|
|
328
|
+
const mNoSkew = getNodeLocalMatrix(withoutSkew);
|
|
267
329
|
expect(mSkew.a).toBeCloseTo(mNoSkew.a);
|
|
268
330
|
expect(mSkew.b).toBeCloseTo(mNoSkew.b);
|
|
269
331
|
expect(mSkew.c).toBeCloseTo(mNoSkew.c);
|
|
@@ -280,7 +342,7 @@ describe('skew', () => {
|
|
|
280
342
|
node.rotation = 0;
|
|
281
343
|
invalidateNodeLocalTransform(node);
|
|
282
344
|
|
|
283
|
-
const m =
|
|
345
|
+
const m = getNodeLocalMatrix(node);
|
|
284
346
|
const radX = 45 * DEG_TO_RAD;
|
|
285
347
|
expect(m.a).toBeCloseTo(1);
|
|
286
348
|
expect(m.b).toBeCloseTo(0);
|
|
@@ -296,7 +358,7 @@ describe('skew', () => {
|
|
|
296
358
|
node.rotation = 0;
|
|
297
359
|
invalidateNodeLocalTransform(node);
|
|
298
360
|
|
|
299
|
-
const m =
|
|
361
|
+
const m = getNodeLocalMatrix(node);
|
|
300
362
|
const radY = 30 * DEG_TO_RAD;
|
|
301
363
|
expect(m.a).toBeCloseTo(Math.cos(radY));
|
|
302
364
|
expect(m.b).toBeCloseTo(Math.sin(radY));
|
|
@@ -312,7 +374,7 @@ describe('skew', () => {
|
|
|
312
374
|
node.scaleY = 1;
|
|
313
375
|
invalidateNodeLocalTransform(node);
|
|
314
376
|
|
|
315
|
-
const m =
|
|
377
|
+
const m = getNodeLocalMatrix(node);
|
|
316
378
|
const radY = (45 + 20) * DEG_TO_RAD;
|
|
317
379
|
const radX = (45 + 15) * DEG_TO_RAD;
|
|
318
380
|
expect(m.a).toBeCloseTo(Math.cos(radY));
|
|
@@ -337,12 +399,12 @@ describe('skew', () => {
|
|
|
337
399
|
|
|
338
400
|
addNodeChild(parent, child);
|
|
339
401
|
|
|
340
|
-
const parentLocal =
|
|
341
|
-
const childLocal =
|
|
402
|
+
const parentLocal = getNodeLocalMatrix(parent);
|
|
403
|
+
const childLocal = getNodeLocalMatrix(child);
|
|
342
404
|
const expected = createMatrix();
|
|
343
405
|
multiplyMatrix(expected, parentLocal, childLocal);
|
|
344
406
|
|
|
345
|
-
const world =
|
|
407
|
+
const world = getNodeWorldMatrix(child);
|
|
346
408
|
expect(world.a).toBeCloseTo(expected.a);
|
|
347
409
|
expect(world.b).toBeCloseTo(expected.b);
|
|
348
410
|
expect(world.c).toBeCloseTo(expected.c);
|
|
@@ -363,7 +425,7 @@ describe('skew', () => {
|
|
|
363
425
|
node.rotation = 0;
|
|
364
426
|
invalidateNodeLocalTransform(node);
|
|
365
427
|
|
|
366
|
-
const m =
|
|
428
|
+
const m = getNodeLocalMatrix(node);
|
|
367
429
|
const radY = 45 * DEG_TO_RAD;
|
|
368
430
|
const radX = 30 * DEG_TO_RAD;
|
|
369
431
|
const a = Math.cos(radY) * 2;
|