From 208e736309886e02ec0ef1c6bca0922d17ab415b Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 22 Jan 2025 22:34:15 +0800 Subject: [PATCH 1/3] Optimize code size for WebView & VideoPlayer and fix type issues. --- cocos/video/video-player-impl-web.ts | 14 ++++++++++---- cocos/video/video-player-impl.ts | 21 ++++++++++++--------- cocos/video/video-player.ts | 17 +++++++++-------- cocos/web-view/web-view-impl-web.ts | 4 +++- cocos/web-view/web-view-impl.ts | 1 + cocos/web-view/web-view.ts | 7 ++++--- 6 files changed, 39 insertions(+), 25 deletions(-) diff --git a/cocos/video/video-player-impl-web.ts b/cocos/video/video-player-impl-web.ts index 600b9f2a0a8..1f6b7c29502 100644 --- a/cocos/video/video-player-impl-web.ts +++ b/cocos/video/video-player-impl-web.ts @@ -32,6 +32,8 @@ import { VideoPlayerImpl } from './video-player-impl'; import { ClearFlagBit } from '../gfx'; import { BrowserType, OS } from '../../pal/system-info/enum-type'; import { ccwindow } from '../core/global-exports'; +import type { VideoPlayer } from './video-player'; +import type { VideoClip } from './assets/video-clip'; const ccdocument = ccwindow.document; @@ -39,6 +41,7 @@ const MIN_ZINDEX = -(2 ** 15); const _mat4_temp = mat4(); +/** @mangle */ export class VideoPlayerImplWeb extends VideoPlayerImpl { protected _eventList: Map void)> = new Map(); @@ -46,7 +49,7 @@ export class VideoPlayerImplWeb extends VideoPlayerImpl { protected _clearColorA = -1; protected _clearFlag; - constructor (component) { + constructor (component: VideoPlayer) { super(component); } @@ -108,7 +111,7 @@ export class VideoPlayerImplWeb extends VideoPlayerImpl { } } - public syncClip (clip: any): void { + public syncClip (clip: VideoClip | null): void { this.removeVideoPlayer(); if (!clip) { return; } this.createVideoPlayer(clip.nativeUrl); @@ -171,8 +174,11 @@ export class VideoPlayerImplWeb extends VideoPlayerImpl { canFullScreen (enabled: boolean): void { // NOTE: below we visited some non-standard web interfaces to complement browser compatibility - // we need to mark video as any type. - const video = this._video as any; + const video = this._video as HTMLVideoElement & { + webkitEnterFullscreen?: () => void; + webkitExitFullscreen?: () => void; + webkitDisplayingFullscreen: boolean; + }; if (!video || video.readyState !== READY_STATE.HAVE_ENOUGH_DATA) { return; } diff --git a/cocos/video/video-player-impl.ts b/cocos/video/video-player-impl.ts index 235ec8576fe..16383397cde 100644 --- a/cocos/video/video-player-impl.ts +++ b/cocos/video/video-player-impl.ts @@ -24,13 +24,15 @@ import { legacyCC } from '../core/global-exports'; import { UITransform } from '../2d/framework'; -import { VideoPlayer } from './video-player'; import { VideoPlayerEventType } from './video-player-enums'; import { error } from '../core/platform'; import { director } from '../game/director'; import { Node } from '../scene-graph'; import type { Camera } from '../render-scene/scene'; +import type { VideoPlayer } from './video-player'; +import type { VideoClip } from './assets/video-clip'; +/** @mangle */ export abstract class VideoPlayerImpl { protected _componentEventList: Map void> = new Map(); protected _state = VideoPlayerEventType.NONE; @@ -71,7 +73,7 @@ export abstract class VideoPlayerImpl { protected _m12 = 0; protected _m13 = 0; - constructor (component) { + constructor (component: VideoPlayer) { this._component = component; this._node = component.node; this._uiTrans = component.node.getComponent(UITransform); @@ -107,7 +109,7 @@ export abstract class VideoPlayerImpl { public abstract disable(noPause?: boolean): void; // synchronizing video player data - public abstract syncClip(clip: any): void; + public abstract syncClip(clip: VideoClip | null): void; public abstract syncURL(url: string): void; public abstract syncStayOnBottom(enabled: boolean): void; public abstract syncKeepAspectRatio(enabled: boolean): void; @@ -225,18 +227,19 @@ export abstract class VideoPlayerImpl { } } - protected dispatchEvent (key): void { + protected dispatchEvent (key: string): void { const callback = this._componentEventList.get(key); if (callback) { - this._state = key; + this._state = key as VideoPlayerEventType; callback.call(this); } } - protected syncUITransform (width, height): void { - if (this._uiTrans) { - this._uiTrans.width = width; - this._uiTrans.height = height; + protected syncUITransform (width: number, height: number): void { + const uiTrans = this._uiTrans; + if (uiTrans) { + uiTrans.width = width; + uiTrans.height = height; } } diff --git a/cocos/video/video-player.ts b/cocos/video/video-player.ts index 1d2c2552a18..f34fadd9c3d 100644 --- a/cocos/video/video-player.ts +++ b/cocos/video/video-player.ts @@ -397,14 +397,15 @@ export class VideoPlayer extends Component { this._impl = VideoPlayerImplManager.getImpl(this); this.syncSource(); - this._impl.componentEventList.set(VideoPlayerEventType.META_LOADED, this.onMetaLoaded.bind(this)); - this._impl.componentEventList.set(VideoPlayerEventType.READY_TO_PLAY, this.onReadyToPlay.bind(this)); - this._impl.componentEventList.set(VideoPlayerEventType.PLAYING, this.onPlaying.bind(this)); - this._impl.componentEventList.set(VideoPlayerEventType.PAUSED, this.onPaused.bind(this)); - this._impl.componentEventList.set(VideoPlayerEventType.STOPPED, this.onStopped.bind(this)); - this._impl.componentEventList.set(VideoPlayerEventType.COMPLETED, this.onCompleted.bind(this)); - this._impl.componentEventList.set(VideoPlayerEventType.ERROR, this.onError.bind(this)); - this._impl.componentEventList.set(VideoPlayerEventType.CLICKED, this.onClicked.bind(this)); + const { componentEventList } = this._impl; + componentEventList.set(VideoPlayerEventType.META_LOADED, this.onMetaLoaded.bind(this)); + componentEventList.set(VideoPlayerEventType.READY_TO_PLAY, this.onReadyToPlay.bind(this)); + componentEventList.set(VideoPlayerEventType.PLAYING, this.onPlaying.bind(this)); + componentEventList.set(VideoPlayerEventType.PAUSED, this.onPaused.bind(this)); + componentEventList.set(VideoPlayerEventType.STOPPED, this.onStopped.bind(this)); + componentEventList.set(VideoPlayerEventType.COMPLETED, this.onCompleted.bind(this)); + componentEventList.set(VideoPlayerEventType.ERROR, this.onError.bind(this)); + componentEventList.set(VideoPlayerEventType.CLICKED, this.onClicked.bind(this)); if (this._playOnAwake && this._impl.loaded) { this.play(); } diff --git a/cocos/web-view/web-view-impl-web.ts b/cocos/web-view/web-view-impl-web.ts index b77ffde0ed4..b4977e2fb16 100644 --- a/cocos/web-view/web-view-impl-web.ts +++ b/cocos/web-view/web-view-impl-web.ts @@ -30,13 +30,15 @@ import { game } from '../game'; import { mat4 } from '../core/math'; import { contains } from '../core/utils/misc'; import { ccwindow } from '../core/global-exports'; +import type { WebView } from './web-view'; const ccdocument = ccwindow.document; const _mat4_temp = mat4(); +/** @mangle */ export class WebViewImplWeb extends WebViewImpl { - constructor (component: any) { + constructor (component: WebView) { super(component); } diff --git a/cocos/web-view/web-view-impl.ts b/cocos/web-view/web-view-impl.ts index 8cbbabe350b..902260fde23 100644 --- a/cocos/web-view/web-view-impl.ts +++ b/cocos/web-view/web-view-impl.ts @@ -30,6 +30,7 @@ import { director } from '../game/director'; import { Node } from '../scene-graph'; import type { Camera } from '../render-scene/scene'; +/** @mangle */ export abstract class WebViewImpl { protected _componentEventList: Map void> = new Map(); protected _state = WebViewEventType.NONE; diff --git a/cocos/web-view/web-view.ts b/cocos/web-view/web-view.ts index c1eedb998d6..667f903c97d 100644 --- a/cocos/web-view/web-view.ts +++ b/cocos/web-view/web-view.ts @@ -170,10 +170,11 @@ export class WebView extends Component { return; } this._impl = WebViewImplManager.getImpl(this); + const { componentEventList } = this._impl; // must be register the event listener - this._impl.componentEventList.set(WebViewEventType.LOADING, this.onLoading.bind(this)); - this._impl.componentEventList.set(WebViewEventType.LOADED, this.onLoaded.bind(this)); - this._impl.componentEventList.set(WebViewEventType.ERROR, this.onError.bind(this)); + componentEventList.set(WebViewEventType.LOADING, this.onLoading.bind(this)); + componentEventList.set(WebViewEventType.LOADED, this.onLoaded.bind(this)); + componentEventList.set(WebViewEventType.ERROR, this.onError.bind(this)); this._impl.loadURL(this._url); } From f1d0f24a0a9df28802e48f354c5b6786f21ff1dd Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 22 Jan 2025 22:45:13 +0800 Subject: [PATCH 2/3] Remove mangle tag for VideoPlayerImpl & WebViewImpl since they're used in js file in platforms directory. --- cocos/video/video-player-impl.ts | 1 - cocos/web-view/web-view-impl-manager.ts | 3 ++- cocos/web-view/web-view-impl.ts | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cocos/video/video-player-impl.ts b/cocos/video/video-player-impl.ts index 16383397cde..c46b2176a30 100644 --- a/cocos/video/video-player-impl.ts +++ b/cocos/video/video-player-impl.ts @@ -32,7 +32,6 @@ import type { Camera } from '../render-scene/scene'; import type { VideoPlayer } from './video-player'; import type { VideoClip } from './assets/video-clip'; -/** @mangle */ export abstract class VideoPlayerImpl { protected _componentEventList: Map void> = new Map(); protected _state = VideoPlayerEventType.NONE; diff --git a/cocos/web-view/web-view-impl-manager.ts b/cocos/web-view/web-view-impl-manager.ts index 445aff22acd..fcf9a9d546c 100644 --- a/cocos/web-view/web-view-impl-manager.ts +++ b/cocos/web-view/web-view-impl-manager.ts @@ -23,11 +23,12 @@ */ import { legacyCC } from '../core/global-exports'; +import type { WebView } from './web-view'; import { WebViewImplWeb } from './web-view-impl-web'; export class WebViewImplManager { // default web - static getImpl (component): WebViewImplWeb { + static getImpl (component: WebView): WebViewImplWeb { return new WebViewImplWeb(component); } } diff --git a/cocos/web-view/web-view-impl.ts b/cocos/web-view/web-view-impl.ts index 902260fde23..8cbbabe350b 100644 --- a/cocos/web-view/web-view-impl.ts +++ b/cocos/web-view/web-view-impl.ts @@ -30,7 +30,6 @@ import { director } from '../game/director'; import { Node } from '../scene-graph'; import type { Camera } from '../render-scene/scene'; -/** @mangle */ export abstract class WebViewImpl { protected _componentEventList: Map void> = new Map(); protected _state = WebViewEventType.NONE; From 3fa2126ee7789ada6d1347aa4242ae15f0a91e0f Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 22 Jan 2025 22:46:06 +0800 Subject: [PATCH 3/3] Fix errors in VideoPlayer.js and jsb-webview.js --- .../wechat/wrapper/engine/VideoPlayer.js | 139 +++++++++--------- platforms/native/engine/jsb-webview.js | 89 ++++++----- 2 files changed, 112 insertions(+), 116 deletions(-) diff --git a/platforms/minigame/platforms/wechat/wrapper/engine/VideoPlayer.js b/platforms/minigame/platforms/wechat/wrapper/engine/VideoPlayer.js index 74aec0f29f3..745c29b57e3 100644 --- a/platforms/minigame/platforms/wechat/wrapper/engine/VideoPlayer.js +++ b/platforms/minigame/platforms/wechat/wrapper/engine/VideoPlayer.js @@ -24,28 +24,26 @@ THE SOFTWARE. ****************************************************************************/ if (cc.internal.VideoPlayer) { - const { EventType } = cc.internal.VideoPlayer; - let vec3 = cc.Vec3; - let _mat4_temp = cc.mat4(); + const _mat4_temp = cc.mat4(); - let _topLeft = new vec3(); - let _bottomRight = new vec3(); + const _topLeft = new cc.Vec3(); + const _bottomRight = new cc.Vec3(); + // eslint-disable-next-line no-undef const dpr = wx.getSystemInfoSync().pixelRatio; - cc.internal.VideoPlayerImplManager.getImpl = function(componenet) { + cc.internal.VideoPlayerImplManager.getImpl = function (componenet) { return new VideoPlayerImplMiniGame(componenet); }; class VideoPlayerImplMiniGame extends cc.internal.VideoPlayerImpl { - - constructor(componenet) { + constructor (componenet) { super(componenet); } - syncClip(clip) { + syncClip (clip) { this.removeVideoPlayer(); if (!clip) { return; @@ -53,7 +51,7 @@ if (cc.internal.VideoPlayer) { this.createVideoPlayer(clip._nativeAsset); } - syncURL(url) { + syncURL (url) { this.removeVideoPlayer(); if (!url) { return; @@ -71,40 +69,39 @@ if (cc.internal.VideoPlayer) { this.delayedPlay(); } - _bindEvent() { - let video = this._video, - self = this; + _bindEvent () { + const video = this._video; + const self = this; if (!video) { return; } - video.onPlay(function () { + video.onPlay(() => { if (self._video !== video) return; self._playing = true; self.dispatchEvent(EventType.PLAYING); }); - video.onEnded(function () { + video.onEnded(() => { if (self._video !== video) return; self._playing = false; self._currentTime = self._duration; // ensure currentTime is at the end of duration self.dispatchEvent(EventType.COMPLETED); }); - video.onPause(function () { + video.onPause(() => { if (self._video !== video) return; self._playing = false; self.dispatchEvent(EventType.PAUSED); }); - video.onTimeUpdate(function (res) { + video.onTimeUpdate((res) => { self._duration = res.duration; self._currentTime = res.position; }); // onStop not supported, implemented in promise returned by video.stop call. - } _unbindEvent () { - let video = this._video; + const video = this._video; if (!video) { return; } @@ -117,13 +114,15 @@ if (cc.internal.VideoPlayer) { // offStop not supported } - createVideoPlayer(url) { + createVideoPlayer (url) { + // eslint-disable-next-line no-undef if (!__globalAdapter.createVideo) { console.warn('VideoPlayer not supported'); return; } if (!this._video) { + // eslint-disable-next-line no-undef this._video = __globalAdapter.createVideo(); this._video.showCenterPlayBtn = false; this._video.controls = false; @@ -140,7 +139,7 @@ if (cc.internal.VideoPlayer) { } setURL (path) { - let video = this._video; + const video = this._video; if (!video || video.src === path) { return; } @@ -149,7 +148,7 @@ if (cc.internal.VideoPlayer) { video.autoplay = true; // HACK: to implement onCanplay callback video.src = path; video.muted = true; - let self = this; + const self = this; this._loaded = false; function loadedCallback () { video.offPlay(); @@ -165,8 +164,8 @@ if (cc.internal.VideoPlayer) { video.onPlay(loadedCallback); } - removeVideoPlayer() { - let video = this.video; + removeVideoPlayer () { + const video = this.video; if (video) { video.stop(); video.destroy(); @@ -180,14 +179,13 @@ if (cc.internal.VideoPlayer) { } setVisible (value) { - let video = this._video; + const video = this._video; if (!video || this._visible === value) { return; } if (value) { video.width = this._actualWidth || 0; - } - else { + } else { video.width = 0; // hide video } this._visible = value; @@ -201,10 +199,10 @@ if (cc.internal.VideoPlayer) { return this._duration; } - syncPlaybackRate(value) { - let video = this._video; - if(video && value !== video.playbackRate) { - if (value === 0.5 | value === 0.8 |value === 1.0 |value === 1.25 | value === 1.5) { + syncPlaybackRate (value) { + const video = this._video; + if (video && value !== video.playbackRate) { + if (value === 0.5 | value === 0.8 | value === 1.0 | value === 1.25 | value === 1.5) { video.playbackRate = value; } else { console.warn('The platform does not support this PlaybackRate!'); @@ -212,29 +210,29 @@ if (cc.internal.VideoPlayer) { } } - syncVolume() { + syncVolume () { console.warn('The platform does not support'); } - syncMute(enable) { - let video = this._video; + syncMute (enable) { + const video = this._video; if (video && video.muted !== enable) { video.muted = enable; } } - syncLoop(enable) { - let video = this._video; + syncLoop (enable) { + const video = this._video; if (video && video.loop !== enable) { video.loop = enable; } } - syncStayOnBottom() { + syncStayOnBottom () { console.warn('The platform does not support'); } - getCurrentTime() { + getCurrentTime () { if (this.video) { return this.currentTime(); } @@ -246,12 +244,12 @@ if (cc.internal.VideoPlayer) { } seekTo (time) { - let video = this._video; + const video = this._video; if (!video || !this._loaded) return; video.seek(time); } - disable(noPause) { + disable (noPause) { if (this._video) { if (!noPause) { this._video.pause(); @@ -261,7 +259,7 @@ if (cc.internal.VideoPlayer) { } } - enable() { + enable () { if (this._video) { this.setVisible(true); this._visible = true; @@ -273,30 +271,30 @@ if (cc.internal.VideoPlayer) { this.syncCurrentTime(); } - resume() { - let video = this._video; + resume () { + const video = this._video; if (this._playing || !video) return; video.play(); } - pause() { - let video = this._video; + pause () { + const video = this._video; if (!this._playing || !video) return; video.pause(); } - stop() { - let self = this; - let video = this._video; + stop () { + const self = this; + const video = this._video; if (!video || !this._visible) return; // HACK: this is to unify inconsistent behavior (on Android, video won't play from begin if called pause() before called stop(). // but if called play() before stop(), video will play from begin.) of wechat official interface on Android & iOS. if (!this._playing) { video.play(); } - video.stop().then(function (res) { + video.stop().then((res) => { if (res.errMsg && !res.errMsg.includes('ok')) { console.error('failed to stop video player'); return; @@ -307,31 +305,31 @@ if (cc.internal.VideoPlayer) { }); } - canFullScreen(enabled) { + canFullScreen (enabled) { if (this._video) { this.setFullScreenEnabled(enabled); } } setFullScreenEnabled (enable) { - let video = this._video; + const video = this._video; if (!video || this._fullScreenEnabled === enable) { return; } if (enable) { video.requestFullScreen(); - } - else { + } else { video.exitFullScreen(); } this._fullScreenEnabled = enable; } - syncKeepAspectRatio(enabled) { + // eslint-disable-next-line no-unused-vars + syncKeepAspectRatio (enabled) { console.warn('On wechat game videoPlayer is always keep the aspect ratio'); } - syncMatrix() { + syncMatrix () { if (!this._video || !this._component || !this._uiTrans) return; const camera = this.UICamera; @@ -341,11 +339,11 @@ if (cc.internal.VideoPlayer) { this._component.node.getWorldMatrix(_mat4_temp); const { width, height } = this._uiTrans.contentSize; - if (!this._forceUpdate && - this._m00 === _mat4_temp.m00 && this._m01 === _mat4_temp.m01 && - this._m04 === _mat4_temp.m04 && this._m05 === _mat4_temp.m05 && - this._m12 === _mat4_temp.m12 && this._m13 === _mat4_temp.m13 && - this._w === width && this._h === height) { + if (!this._forceUpdate + && this._m00 === _mat4_temp.m00 && this._m01 === _mat4_temp.m01 + && this._m04 === _mat4_temp.m04 && this._m05 === _mat4_temp.m05 + && this._m12 === _mat4_temp.m12 && this._m13 === _mat4_temp.m13 + && this._w === width && this._h === height) { return; } @@ -359,29 +357,28 @@ if (cc.internal.VideoPlayer) { this._w = width; this._h = height; - let canvas_width = cc.game.canvas.width; - let canvas_height = cc.game.canvas.height; + // const canvasWidth = cc.game.canvas.width; + const canvasHeight = cc.game.canvas.height; - let ap = this._uiTrans.anchorPoint; + const ap = this._uiTrans.anchorPoint; // Vectors in node space - vec3.set(_topLeft, -ap.x * this._w, (1.0 - ap.y) * this._h, 0); - vec3.set(_bottomRight, (1 - ap.x) * this._w, -ap.y * this._h, 0); + cc.Vec3.set(_topLeft, -ap.x * this._w, (1.0 - ap.y) * this._h, 0); + cc.Vec3.set(_bottomRight, (1 - ap.x) * this._w, -ap.y * this._h, 0); // Convert to world space - vec3.transformMat4(_topLeft, _topLeft, _mat4_temp); - vec3.transformMat4(_bottomRight, _bottomRight, _mat4_temp); + cc.Vec3.transformMat4(_topLeft, _topLeft, _mat4_temp); + cc.Vec3.transformMat4(_bottomRight, _bottomRight, _mat4_temp); // Convert to Screen space camera.worldToScreen(_topLeft, _topLeft); camera.worldToScreen(_bottomRight, _bottomRight); - let finalWidth = _bottomRight.x - _topLeft.x; - let finalHeight = _topLeft.y - _bottomRight.y; + const finalWidth = _bottomRight.x - _topLeft.x; + const finalHeight = _topLeft.y - _bottomRight.y; this._video.x = _topLeft.x / dpr; - this._video.y = (canvas_height - _topLeft.y) / dpr; + this._video.y = (canvasHeight - _topLeft.y) / dpr; this._actualWidth = this._video.width = finalWidth / dpr; this._video.height = finalHeight / dpr; this._forceUpdate = false; } - } } diff --git a/platforms/native/engine/jsb-webview.js b/platforms/native/engine/jsb-webview.js index b75bdf6d2cc..2d5d2969fea 100644 --- a/platforms/native/engine/jsb-webview.js +++ b/platforms/native/engine/jsb-webview.js @@ -23,39 +23,35 @@ THE SOFTWARE. ****************************************************************************/ -'use strict'; - if (cc.internal.WebView) { - const { EventType } = cc.internal.WebView; - let vec3 = cc.Vec3; - let mat4 = cc.Mat4; - let _mat4_temp = new mat4(); + const Vec3 = cc.Vec3; + const Mat4 = cc.Mat4; + const _mat4_temp = new Mat4(); - let _topLeft = new vec3(); - let _bottomRight = new vec3(); + const _topLeft = new Vec3(); + const _bottomRight = new Vec3(); - cc.internal.WebViewImplManager.getImpl = function(componenet) { + cc.internal.WebViewImplManager.getImpl = function (componenet) { return new WebViewImplJSB(componenet); }; class WebViewImplJSB extends cc.internal.WebViewImpl { - - constructor(componenet) { + constructor (componenet) { super(componenet); this.jsCallback = null; this.interfaceSchema = null; - this._matViewProj_temp = new mat4(); + this._matViewProj_temp = new Mat4(); } - _bindEvent() { - let onLoaded = () => { + _bindEvent () { + const onLoaded = () => { this._forceUpdate = true; this.dispatchEvent(EventType.LOADED); }; - let onError = () => { + const onError = () => { this.dispatchEvent(EventType.ERROR); }; this.webview.setOnDidFinishLoading(onLoaded); @@ -67,37 +63,39 @@ if (cc.internal.WebView) { this.interfaceSchema = null; } - createWebView() { + createWebView () { + // eslint-disable-next-line no-undef if (!jsb.WebView) { console.warn('jsb.WebView is null'); return; } + // eslint-disable-next-line no-undef this._webview = jsb.WebView.create(); this._bindEvent(); } - removeWebView() { - let webview = this.webview; + removeWebView () { + const webview = this.webview; if (webview) { this.webview.destroy(); this.reset(); } } - disable() { + disable () { if (this.webview) { this.webview.setVisible(false); } } - enable() { + enable () { if (this.webview) { this.webview.setVisible(true); } } - setOnJSCallback(callback) { - let webview = this.webview; + setOnJSCallback (callback) { + const webview = this.webview; if (webview) { webview.setOnJSCallback(callback); } else { @@ -105,8 +103,8 @@ if (cc.internal.WebView) { } } - setJavascriptInterfaceScheme(scheme) { - let webview = this.webview; + setJavascriptInterfaceScheme (scheme) { + const webview = this.webview; if (webview) { webview.setJavascriptInterfaceScheme(scheme); } else { @@ -114,8 +112,8 @@ if (cc.internal.WebView) { } } - loadURL(url) { - let webview = this.webview; + loadURL (url) { + const webview = this.webview; if (webview) { webview.src = url; webview.loadURL(url); @@ -123,14 +121,15 @@ if (cc.internal.WebView) { } } - evaluateJS(str) { - let webview = this.webview; + evaluateJS (str) { + const webview = this.webview; if (webview) { return webview.evaluateJS(str); } + return null; } - syncMatrix() { + syncMatrix () { if (!this._webview || !this._component || !this._uiTrans) return; const camera = this.UICamera; @@ -140,12 +139,12 @@ if (cc.internal.WebView) { this._component.node.getWorldMatrix(_mat4_temp); const { width, height } = this._uiTrans.contentSize; - if (!this._forceUpdate && - camera.matViewProj.equals(this._matViewProj_temp) && - this._m00 === _mat4_temp.m00 && this._m01 === _mat4_temp.m01 && - this._m04 === _mat4_temp.m04 && this._m05 === _mat4_temp.m05 && - this._m12 === _mat4_temp.m12 && this._m13 === _mat4_temp.m13 && - this._w === width && this._h === height) { + if (!this._forceUpdate + && camera.matViewProj.equals(this._matViewProj_temp) + && this._m00 === _mat4_temp.m00 && this._m01 === _mat4_temp.m01 + && this._m04 === _mat4_temp.m04 && this._m05 === _mat4_temp.m05 + && this._m12 === _mat4_temp.m12 && this._m13 === _mat4_temp.m13 + && this._w === width && this._h === height) { return; } @@ -160,25 +159,25 @@ if (cc.internal.WebView) { this._w = width; this._h = height; - let canvas_width = cc.game.canvas.width; - let canvas_height = cc.game.canvas.height; + // const canvas_width = cc.game.canvas.width; + const canvasHeight = cc.game.canvas.height; - let ap = this._uiTrans.anchorPoint; + const ap = this._uiTrans.anchorPoint; // Vectors in node space - vec3.set(_topLeft, -ap.x * this._w, (1.0 - ap.y) * this._h, 0); - vec3.set(_bottomRight, (1 - ap.x) * this._w, -ap.y * this._h, 0); + Vec3.set(_topLeft, -ap.x * this._w, (1.0 - ap.y) * this._h, 0); + Vec3.set(_bottomRight, (1 - ap.x) * this._w, -ap.y * this._h, 0); // Convert to world space - vec3.transformMat4(_topLeft, _topLeft, _mat4_temp); - vec3.transformMat4(_bottomRight, _bottomRight, _mat4_temp); + Vec3.transformMat4(_topLeft, _topLeft, _mat4_temp); + Vec3.transformMat4(_bottomRight, _bottomRight, _mat4_temp); // need update camera data camera.update(); // Convert to Screen space camera.worldToScreen(_topLeft, _topLeft); camera.worldToScreen(_bottomRight, _bottomRight); - let finalWidth = _bottomRight.x - _topLeft.x; - let finalHeight = _topLeft.y - _bottomRight.y; - this._webview.setFrame(_topLeft.x, canvas_height - _topLeft.y, finalWidth, finalHeight); + const finalWidth = _bottomRight.x - _topLeft.x; + const finalHeight = _topLeft.y - _bottomRight.y; + this._webview.setFrame(_topLeft.x, canvasHeight - _topLeft.y, finalWidth, finalHeight); this._forceUpdate = false; } }