Skip to content

Commit c0160a3

Browse files
chubei-oppenliufang
andauthored
add global variable platform (oppenfuture#69)
Co-authored-by: liufang <liufang@oppentech.com>
1 parent efbfc67 commit c0160a3

File tree

7 files changed

+57
-11
lines changed

7 files changed

+57
-11
lines changed

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "three",
3-
"version": "0.137.5",
2+
"name": "@oppentech/three",
3+
"version": "0.137.0",
44
"description": "JavaScript 3D library",
55
"type": "module",
66
"main": "./build/three.js",
@@ -16,9 +16,11 @@
1616
},
1717
"repository": {
1818
"type": "git",
19-
"url": "https://github.com/mrdoob/three.js"
19+
"url": "https://github.com/oppenfuture/three.js"
2020
},
21-
"sideEffects": false,
21+
"sideEffects": [
22+
"src/Three.js"
23+
],
2224
"files": [
2325
"build/three.js",
2426
"build/three.cjs",
@@ -118,7 +120,7 @@
118120
"author": "mrdoob",
119121
"license": "MIT",
120122
"bugs": {
121-
"url": "https://github.com/mrdoob/three.js/issues"
123+
"url": "https://github.com/oppenfuture/three.js/issues"
122124
},
123125
"homepage": "https://threejs.org/",
124126
"devDependencies": {

src/Three.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { REVISION } from './constants.js';
22

3+
export { platform } from './platform.js';
34
export { WebGLMultipleRenderTargets } from './renderers/WebGLMultipleRenderTargets.js';
45
export { WebGLMultisampleRenderTarget } from './renderers/WebGLMultisampleRenderTarget.js';
56
export { WebGLCubeRenderTarget } from './renderers/WebGLCubeRenderTarget.js';

src/audio/AudioContext.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
let _context;
22

3+
import { platform } from '../platform.js';
4+
35
const AudioContext = {
46

57
getContext: function () {
68

79
if ( _context === undefined ) {
810

9-
_context = new ( window.AudioContext || window.webkitAudioContext )();
11+
_context = new platform.AudioContext();
1012

1113
}
1214

src/loaders/FileLoader.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Cache } from './Cache.js';
22
import { Loader } from './Loader.js';
3+
import { platform } from '../platform.js';
34

45
const loading = {};
56

@@ -63,8 +64,8 @@ class FileLoader extends Loader {
6364
} );
6465

6566
// create request
66-
const req = new Request( url, {
67-
headers: new Headers( this.requestHeader ),
67+
const req = new platform.Request( url, {
68+
headers: new platform.Headers( this.requestHeader ),
6869
credentials: this.withCredentials ? 'include' : 'same-origin',
6970
// An abort controller could be added within a future PR
7071
} );
@@ -74,7 +75,7 @@ class FileLoader extends Loader {
7475
const responseType = this.responseType;
7576

7677
// start the fetch
77-
fetch( req )
78+
platform.fetch( req )
7879
.then( response => {
7980

8081
if ( response.status === 200 || response.status === 0 ) {

src/platform.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const platform = {
2+
3+
updateplatform( params ) {
4+
5+
this.AudioContext = params.AudioContext;
6+
this.fetch = params.fetch;
7+
this.Request = params.Request;
8+
this.Headers = params.Headers;
9+
this.createElementNS = params.createElementNS;
10+
this.animationContext = {
11+
12+
requestAnimationFrame: params.requestAnimationFrame,
13+
cancelAnimationFrame: params.cancelAnimationFrame,
14+
15+
};
16+
17+
}
18+
19+
};
20+
21+
if ( typeof window !== 'undefined' && typeof window.window !== 'undefined' ) {
22+
23+
platform.updateplatform( {
24+
25+
AudioContext: ( window.AudioContext || window.webkitAudioContext ).bind( window ),
26+
fetch: window.fetch.bind( window ),
27+
Request: window.Request.bind( window ),
28+
Headers: window.Headers.bind( window ),
29+
createElementNS: document.createElementNS.bind( document ),
30+
requestAnimationFrame: window.requestAnimationFrame.bind( window ),
31+
cancelAnimationFrame: window.cancelAnimationFrame.bind( window ),
32+
33+
} );
34+
35+
}
36+
37+
export { platform };

src/renderers/WebGLRenderer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
NearestFilter,
1414
ClampToEdgeWrapping
1515
} from '../constants.js';
16+
import { platform } from '../platform.js';
1617
import { Frustum } from '../math/Frustum.js';
1718
import { Matrix4 } from '../math/Matrix4.js';
1819
import { Vector3 } from '../math/Vector3.js';
@@ -880,7 +881,7 @@ function WebGLRenderer( parameters = {} ) {
880881
const animation = new WebGLAnimation();
881882
animation.setAnimationLoop( onAnimationFrame );
882883

883-
if ( typeof window !== 'undefined' ) animation.setContext( window );
884+
animation.setContext( platform.animationContext );
884885

885886
this.setAnimationLoop = function ( callback ) {
886887

src/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { platform } from './platform.js';
2+
13
function arrayMin( array ) {
24

35
if ( array.length === 0 ) return Infinity;
@@ -64,7 +66,7 @@ function getTypedArray( type, buffer ) {
6466

6567
function createElementNS( name ) {
6668

67-
return document.createElementNS( 'http://www.w3.org/1999/xhtml', name );
69+
return platform.createElementNS( 'http://www.w3.org/1999/xhtml', name );
6870

6971
}
7072

0 commit comments

Comments
 (0)