From cd03f2da2d76782c8fba5bc80446561ae2c5b549 Mon Sep 17 00:00:00 2001 From: PP Date: Fri, 8 Apr 2022 14:48:15 +0800 Subject: [PATCH] fix rendering not working on wechat subcontextview (#10698) --- cocos/ui/sub-context-view.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/cocos/ui/sub-context-view.ts b/cocos/ui/sub-context-view.ts index 54f4500531..295066eac0 100644 --- a/cocos/ui/sub-context-view.ts +++ b/cocos/ui/sub-context-view.ts @@ -29,7 +29,7 @@ */ import { ccclass, help, menu, executionOrder, requireComponent, tooltip, serializable } from 'cc.decorator'; -import { EDITOR } from 'internal:constants'; +import { EDITOR, WECHAT } from 'internal:constants'; import { minigame } from 'pal/minigame'; import { screenAdapter } from 'pal/screen-adapter'; import { Component } from '../core/components/component'; @@ -156,8 +156,22 @@ export class SubContextView extends Component { private _initSharedCanvas () { if (this._openDataContext) { const sharedCanvas = this._openDataContext.canvas; - sharedCanvas.width = this._designResolutionSize.width; - sharedCanvas.height = this._designResolutionSize.height; + let designWidth = this._designResolutionSize.width; + let designHeight = this._designResolutionSize.height; + if (WECHAT) { + // HACK: on WeChat platform, at least one side of the width and height of sharedCanvas is greater than 513 + // When the sharedCanvas is smaller than this size, the rendering doesn't work. + const minimumSize = 513; + if (designWidth <= minimumSize && designHeight <= minimumSize) { + const scaleWidth = minimumSize / designWidth; + const scaleHeight = minimumSize / designHeight; + const targetScale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight; + designWidth *= targetScale; + designHeight *= targetScale; + } + } + sharedCanvas.width = designWidth; + sharedCanvas.height = designWidth; } }