Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
feat: udpate window size before register (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
cptbtptpbcptdtptp authored Jan 5, 2024
1 parent 59fa47e commit b4f982a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@galacean/engine-miniprogram-adapter",
"version": "0.0.13",
"version": "0.0.14",
"description": "adapter of miniprogram",
"packageManager": "[email protected]",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const window = {
};

export * from "./EventIniter/index";
export { registerCanvas, registerCanvas2D, registerMiniGame } from "./register";
export { registerCanvas, registerCanvas2D, registerMiniGame, registerResize } from "./register";
export {
document as $document,
location as $location,
Expand Down
39 changes: 37 additions & 2 deletions src/register.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { document } from "./document";
import { dispatchTouchCancel, dispatchTouchEnd, dispatchTouchMove, dispatchTouchStart } from "./EventIniter/TouchEvent";
import { $window } from "./index";
import { $window, Event, screen } from "./index";
import * as Mixin from "./util/mixin";

declare let my: any;
Expand Down Expand Up @@ -107,4 +107,39 @@ function getCanvas2D() {
return canvas2D;
}

export { registerCanvas, registerCanvas2D, getCanvas, getCanvas2D, registerMiniGame, isMiniGame };
/**
* 监听可渲染区域尺寸改变的回调
* 如:在 onCanvasReady 前后,windowWidth 和 windowHeight 可能会改变(导航栏的高度)
*/
function registerResize() {
// 获取页面宽高
const { screenWidth, screenHeight, windowWidth, windowHeight } = my.getSystemInfoSync();
screen.width = screenWidth;
screen.height = screenHeight;
screen.availWidth = $window.innerWidth = windowWidth;
screen.availHeight = $window.innerHeight = windowHeight;
updateCanvasSize(getCanvas(), windowWidth, windowHeight);
updateCanvasSize(getCanvas2D(), windowWidth, windowHeight);
$window.dispatchEvent(new Event("resize"));
}

function updateCanvasSize(canvas, width, height) {
if (!canvas) return;
const { style } = canvas;
if (style) {
style.width = width + "px";
style.height = height + "px";
}
try {
canvas.clientWidth = width;
} catch (error) {
// Only work in MiniProgram
}
try {
canvas.clientHeight = height;
} catch (error) {
// Only work in MiniProgram
}
}

export { registerCanvas, registerCanvas2D, registerResize, getCanvas, getCanvas2D, registerMiniGame, isMiniGame };

0 comments on commit b4f982a

Please sign in to comment.