Skip to content

Commit 461788f

Browse files
committed
fix(playcanvas): remove on load behavior of playcanvas adapter
1 parent 529fe2b commit 461788f

File tree

5 files changed

+74
-16
lines changed

5 files changed

+74
-16
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"babel-preset-minify": "0.5.1",
5050
"babelrc-rollup": "3.0.0",
5151
"chai": "4.3.4",
52+
"mocha": "9.1.1",
5253
"cross-env": "7.0.3",
5354
"eslint": "7.26.0",
5455
"eslint-config-airbnb": "18.2.1",

src/adapter/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ increase `Array Size`
3131

3232
add `https://unpkg.com/@js-basics/vector/build/iife/adapter/playcanvas.min.js` to the array
3333

34+
create a new script asset
35+
36+
set its loading type to `After Engine`
37+
38+
and type into:
39+
40+
```
41+
if (typeof basics !== 'undefined') {
42+
basics.vector.adapter.playcanvas.hijackPlayCanvas(pc);
43+
}
44+
```
45+
3446
## Features
3547

3648
### Short notations

src/adapter/array.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { cachedValueOf } from '../operator';
2+
3+
export const hijackArray = (ArrayClass) => {
4+
const { prototype } = ArrayClass;
5+
6+
Object.defineProperty(prototype, 'x', {
7+
get() {
8+
return this[0] || 0;
9+
},
10+
set(x) {
11+
this[0] = x;
12+
}
13+
});
14+
15+
Object.defineProperty(prototype, 'y', {
16+
get() {
17+
return this[1] || 0;
18+
},
19+
set(y) {
20+
this[1] = y;
21+
}
22+
});
23+
24+
Object.defineProperty(prototype, 'z', {
25+
get() {
26+
return this[2] || 0;
27+
},
28+
set(z) {
29+
this[2] = z;
30+
}
31+
});
32+
33+
cachedValueOf(ArrayClass);
34+
35+
Object.defineProperty(prototype, 'len', {
36+
get() {
37+
return Math.sqrt((this[0] ** 2) + (this[1] ** 2) + (this[2] ** 2)) ** (1 / 2);
38+
},
39+
set() {
40+
throw new Error('set len not allowed');
41+
}
42+
});
43+
};

src/adapter/playcanvas.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import {
66
multiplyMat3Vec, multiplyMat3Mat3, multiplyVecMat3, isNumber, multiplyVecMat4
77
} from '../utils/math';
88

9-
function fallbackWindow() {
10-
return {
11-
addEventListener() { }
12-
};
13-
}
149
export function hijackPlayCanvas(pc) {
1510
const {
1611
Vec2, Vec3, Vec4, Quat, Mat3: AMat3, Mat4: AMat4, math
@@ -357,14 +352,3 @@ export function hijackPlayCanvas(pc) {
357352

358353
pc.mat4 = (...axes) => new Mat4(...axes);
359354
}
360-
361-
// eslint-disable-next-line no-undef
362-
const global = typeof window === 'undefined' ? fallbackWindow() : window;
363-
global.addEventListener('load', () => {
364-
const { pc } = global;
365-
if (!pc) {
366-
console.warn('no playcanvas in global namespace found');
367-
} else {
368-
hijackPlayCanvas(pc);
369-
}
370-
});

test/adapter/array.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { assert } from 'chai';
2+
import { hijackArray } from '../../src/adapter/array';
3+
import { calc } from '../../src/index';
4+
5+
describe('override valueOf of Array', () => {
6+
it('calculations with the arrays created by [] operator', () => {
7+
hijackArray(Array);
8+
9+
const t1 = [3, 4, 5];
10+
const t2 = [6, 7, 8];
11+
const pos = calc(() => t1 + t2 * 2);
12+
13+
assert.instanceOf(pos, Array);
14+
assert.equal(pos.x, 15);
15+
assert.equal(pos.y, 18);
16+
assert.equal(pos.z, 21);
17+
});
18+
});

0 commit comments

Comments
 (0)