Skip to content

Commit

Permalink
fix(playcanvas): remove on load behavior of playcanvas adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Sep 7, 2021
1 parent 529fe2b commit 461788f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"babel-preset-minify": "0.5.1",
"babelrc-rollup": "3.0.0",
"chai": "4.3.4",
"mocha": "9.1.1",
"cross-env": "7.0.3",
"eslint": "7.26.0",
"eslint-config-airbnb": "18.2.1",
Expand Down
12 changes: 12 additions & 0 deletions src/adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ increase `Array Size`

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

create a new script asset

set its loading type to `After Engine`

and type into:

```
if (typeof basics !== 'undefined') {
basics.vector.adapter.playcanvas.hijackPlayCanvas(pc);
}
```

## Features

### Short notations
Expand Down
43 changes: 43 additions & 0 deletions src/adapter/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { cachedValueOf } from '../operator';

export const hijackArray = (ArrayClass) => {
const { prototype } = ArrayClass;

Object.defineProperty(prototype, 'x', {
get() {
return this[0] || 0;
},
set(x) {
this[0] = x;
}
});

Object.defineProperty(prototype, 'y', {
get() {
return this[1] || 0;
},
set(y) {
this[1] = y;
}
});

Object.defineProperty(prototype, 'z', {
get() {
return this[2] || 0;
},
set(z) {
this[2] = z;
}
});

cachedValueOf(ArrayClass);

Object.defineProperty(prototype, 'len', {
get() {
return Math.sqrt((this[0] ** 2) + (this[1] ** 2) + (this[2] ** 2)) ** (1 / 2);
},
set() {
throw new Error('set len not allowed');
}
});
};
16 changes: 0 additions & 16 deletions src/adapter/playcanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import {
multiplyMat3Vec, multiplyMat3Mat3, multiplyVecMat3, isNumber, multiplyVecMat4
} from '../utils/math';

function fallbackWindow() {
return {
addEventListener() { }
};
}
export function hijackPlayCanvas(pc) {
const {
Vec2, Vec3, Vec4, Quat, Mat3: AMat3, Mat4: AMat4, math
Expand Down Expand Up @@ -357,14 +352,3 @@ export function hijackPlayCanvas(pc) {

pc.mat4 = (...axes) => new Mat4(...axes);
}

// eslint-disable-next-line no-undef
const global = typeof window === 'undefined' ? fallbackWindow() : window;
global.addEventListener('load', () => {
const { pc } = global;
if (!pc) {
console.warn('no playcanvas in global namespace found');
} else {
hijackPlayCanvas(pc);
}
});
18 changes: 18 additions & 0 deletions test/adapter/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { assert } from 'chai';
import { hijackArray } from '../../src/adapter/array';
import { calc } from '../../src/index';

describe('override valueOf of Array', () => {
it('calculations with the arrays created by [] operator', () => {
hijackArray(Array);

const t1 = [3, 4, 5];
const t2 = [6, 7, 8];
const pos = calc(() => t1 + t2 * 2);

assert.instanceOf(pos, Array);
assert.equal(pos.x, 15);
assert.equal(pos.y, 18);
assert.equal(pos.z, 21);
});
});

0 comments on commit 461788f

Please sign in to comment.