From 1c144ea78b3958a4a4dd6d6cd372542b51add7bd Mon Sep 17 00:00:00 2001 From: AZhan Date: Tue, 2 Aug 2022 15:16:47 +0800 Subject: [PATCH] fix: miniprogram cannot recognize htmlcanvaselement (#914) * fix: miniprogram cannot recognize htmlcanvaselement --- packages/core/src/input/InputManager.ts | 2 +- packages/rhi-webgl/src/WebCanvas.ts | 6 +++--- packages/rhi-webgl/src/WebGLRenderer.ts | 4 ++-- rollup.miniprogram.plugin.js | 3 ++- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/core/src/input/InputManager.ts b/packages/core/src/input/InputManager.ts index 0a1268e32..15491bc2d 100644 --- a/packages/core/src/input/InputManager.ts +++ b/packages/core/src/input/InputManager.ts @@ -170,7 +170,7 @@ export class InputManager { constructor(engine: Engine) { // @ts-ignore const canvas = engine._canvas._webCanvas; - if (canvas instanceof HTMLCanvasElement) { + if (!(canvas instanceof OffscreenCanvas)) { this._wheelManager = new WheelManager(canvas); this._pointerManager = new PointerManager(engine, canvas); this._keyboardManager = new KeyboardManager(); diff --git a/packages/rhi-webgl/src/WebCanvas.ts b/packages/rhi-webgl/src/WebCanvas.ts index 01e545e12..4d81966e0 100644 --- a/packages/rhi-webgl/src/WebCanvas.ts +++ b/packages/rhi-webgl/src/WebCanvas.ts @@ -47,7 +47,7 @@ export class WebCanvas implements Canvas { */ get scale(): Vector2 { const webCanvas = this._webCanvas; - if (webCanvas instanceof HTMLCanvasElement) { + if (!(webCanvas instanceof OffscreenCanvas)) { this._scale.set( (webCanvas.clientWidth * devicePixelRatio) / webCanvas.width, (webCanvas.clientHeight * devicePixelRatio) / webCanvas.height @@ -58,7 +58,7 @@ export class WebCanvas implements Canvas { set scale(value: Vector2) { const webCanvas = this._webCanvas; - if (webCanvas instanceof HTMLCanvasElement) { + if (!(webCanvas instanceof OffscreenCanvas)) { webCanvas.style.transformOrigin = `left top`; webCanvas.style.transform = `scale(${value.x}, ${value.y})`; } @@ -70,7 +70,7 @@ export class WebCanvas implements Canvas { */ resizeByClientSize(pixelRatio: number = window.devicePixelRatio): void { const webCanvas = this._webCanvas; - if (webCanvas instanceof HTMLCanvasElement) { + if (!(webCanvas instanceof OffscreenCanvas)) { this.width = webCanvas.clientWidth * pixelRatio; this.height = webCanvas.clientHeight * pixelRatio; } diff --git a/packages/rhi-webgl/src/WebGLRenderer.ts b/packages/rhi-webgl/src/WebGLRenderer.ts index b6ce70250..70ab081d9 100644 --- a/packages/rhi-webgl/src/WebGLRenderer.ts +++ b/packages/rhi-webgl/src/WebGLRenderer.ts @@ -114,7 +114,7 @@ export class WebGLRenderer implements IHardwareRenderer { if (webGLMode == WebGLMode.Auto || webGLMode == WebGLMode.WebGL2) { gl = webCanvas.getContext("webgl2", option); - if (!gl && webCanvas instanceof HTMLCanvasElement) { + if (!gl && !(webCanvas instanceof OffscreenCanvas)) { gl = webCanvas.getContext("experimental-webgl2", option); } this._isWebGL2 = true; @@ -128,7 +128,7 @@ export class WebGLRenderer implements IHardwareRenderer { if (!gl) { if (webGLMode == WebGLMode.Auto || webGLMode == WebGLMode.WebGL1) { gl = webCanvas.getContext("webgl", option); - if (!gl && webCanvas instanceof HTMLCanvasElement) { + if (!gl && !(webCanvas instanceof OffscreenCanvas)) { gl = webCanvas.getContext("experimental-webgl", option); } this._isWebGL2 = false; diff --git a/rollup.miniprogram.plugin.js b/rollup.miniprogram.plugin.js index 92cdf8e47..009fb4538 100644 --- a/rollup.miniprogram.plugin.js +++ b/rollup.miniprogram.plugin.js @@ -35,7 +35,8 @@ const adapterArray = [ "WebGLRenderingContext", "WebGL2RenderingContext", "ImageData", - "location" + "location", + "OffscreenCanvas" ]; const adapterVars = {};