Skip to content

Commit 45dc5f7

Browse files
committed
Animation: Fix ReferenceError in non-broswer environment
Node.js and Deno do not support `self`.
1 parent 541455a commit 45dc5f7

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/renderers/common/Animation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getGlobalThis } from '../../utils.js';
12

23
/**
34
* This module manages the internal animation loop of the renderer.
@@ -34,7 +35,7 @@ class Animation {
3435
*
3536
* @type {Window|XRSession}
3637
*/
37-
this._context = self;
38+
this._context = getGlobalThis();
3839

3940
/**
4041
* The user-defined animation loop.

src/utils.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,16 @@ function toReversedProjectionMatrix( projectionMatrix ) {
149149

150150
}
151151

152-
export { arrayMin, arrayMax, arrayNeedsUint32, getTypedArray, createElementNS, createCanvasElement, warnOnce, probeAsync, toNormalizedProjectionMatrix, toReversedProjectionMatrix };
152+
// referred https://github.com/zloirock/core-js/blob/78474627edab1ab1a1510721185cfc5b66c36770/packages/core-js/internals/global-this.js
153+
// Three.js nearly always runs on broswers, so it would directly return `self` in most cases.
154+
// We do not check `window`, because in our situation `self` is enough.
155+
function getGlobalThis() {
156+
157+
return typeof self !== 'undefined' ? self : // broswer or Web Worker
158+
typeof globalThis !== 'undefined' ? globalThis : // ES2020
159+
typeof global !== 'undefined' ? global : // Node.js
160+
Function( 'return this' )(); // may break CSP, but it should never run on here
161+
162+
}
163+
164+
export { arrayMin, arrayMax, arrayNeedsUint32, getTypedArray, createElementNS, createCanvasElement, warnOnce, probeAsync, toNormalizedProjectionMatrix, toReversedProjectionMatrix, getGlobalThis };

0 commit comments

Comments
 (0)