Skip to content

Commit e517a99

Browse files
jinwuwu001billyjin
authored andcommitted
Remove support for passing a canvas ID to PAGView.init() in miniprograms. (#2824)
Co-authored-by: billyjin <[email protected]> (cherry picked from commit d7532ff)
1 parent f85df20 commit e517a99

File tree

5 files changed

+11
-90
lines changed

5 files changed

+11
-90
lines changed

web/demo/wechat-miniprogram/pages/benchmark/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Page({
2020
const canvas = res[0].node;
2121
const buffer = await loadFileByRequest(`${origin}${this.data.pagMap[index]}`);
2222
const pagFile = await this.PAG.PAGFile.load(buffer);
23-
const pagView = await this.PAG.PAGView.init(pagFile, canvas, { firstFrame: true }); // 自v4.4.29开始建议传入wxml中定义的canvasId
23+
const pagView = await this.PAG.PAGView.init(pagFile, canvas, { firstFrame: true });
2424
pagView.setRepeatCount(0);
2525
pagView.addListener('onAnimationUpdate', () => {
2626
wx.createSelectorQuery()

web/demo/wechat-miniprogram/pages/index/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Page({
5353
const time = wx.getPerformance().now();
5454
this.pagFile = await this.PAG.PAGFile.load(buffer);
5555
debugData = { ...debugData, decodeTime: wx.getPerformance().now() - time };
56-
this.pagView = await this.PAG.PAGView.init(this.pagFile, canvas); // 自v4.4.29开始建议传入wxml中定义的canvasId
56+
this.pagView = await this.PAG.PAGView.init(this.pagFile, canvas);
5757
this.updateDebugData(this.pagView);
5858
this.pagView.addListener('onAnimationUpdate', () => {
5959
this.updateDebugData(this.pagView);

web/src/wechat/interfaces.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export interface wx {
1313
compInst?: any,
1414
) => OffscreenCanvas;
1515
getPerformance: () => Performance;
16-
createSelectorQuery: () => SelectorQuery;
1716
}
1817

1918
export interface FileSystemManager {
@@ -80,19 +79,3 @@ export interface SystemInfo {
8079
/** 设备像素比 */
8180
pixelRatio: number;
8281
}
83-
84-
interface Fields {
85-
node?: boolean;
86-
size?: boolean;
87-
}
88-
89-
interface NodesRef {
90-
id: string;
91-
fields: (fields: Fields) => SelectorQuery;
92-
}
93-
94-
export interface SelectorQuery {
95-
select: (selector: string) => NodesRef;
96-
selectAll: (selector: string) => NodesRef;
97-
exec: (callback?: (...args: any[]) => any) => NodesRef;
98-
}

web/src/wechat/pag-view.ts

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,9 @@ function isOffscreenCanvas(canvas: any): boolean {
3434
return false;
3535
}
3636

37-
interface CanvasSize {
38-
width: number;
39-
height: number;
40-
}
41-
4237
@destroyVerify
4338
@wasmAwaitRewind
4439
export class PAGView extends NativePAGView {
45-
/**
46-
* Get canvas element by canvas ID
47-
*
48-
* @param {string} canvasId - The ID of the canvas element to find
49-
*
50-
* @returns {Promise<HTMLCanvasElement | OffscreenCanvas | null>}
51-
* - Returns the found canvas element, or null if not found
52-
*/
53-
public static getCanvasElementByCanvasId(canvasId: string): Promise<HTMLCanvasElement | OffscreenCanvas> {
54-
return new Promise((resolve, reject) => {
55-
const query = wx.createSelectorQuery();
56-
query
57-
.select(`#${canvasId}`)
58-
.fields({ node: true, size: true })
59-
.exec((res) => {
60-
if (res[0] && res[0].node) {
61-
resolve(res[0].node);
62-
} else {
63-
reject(new Error(`Cannot find Canvas element with ID ${canvasId}`));
64-
}
65-
});
66-
});
67-
}
68-
6940
/**
7041
* Create pag view.
7142
* @param file pag file.
@@ -75,29 +46,18 @@ export class PAGView extends NativePAGView {
7546
*/
7647
public static async init(
7748
file: PAGComposition,
78-
canvas: HTMLCanvasElement | OffscreenCanvas | string,
49+
canvas: HTMLCanvasElement | OffscreenCanvas,
7950
initOptions: PAGViewOptions = {},
8051
): Promise<PAGView> {
81-
let canvasElement: HTMLCanvasElement | OffscreenCanvas | null = null;
82-
if (typeof canvas === 'string') {
83-
canvasElement = await this.getCanvasElementByCanvasId(canvas);
84-
} else {
85-
canvasElement = canvas;
86-
}
87-
if (!canvasElement) throw new Error('Canvas is not found!');
88-
8952
const pagPlayer = PAGModule.PAGPlayer.create();
90-
const pagView = new PAGView(pagPlayer, canvasElement);
91-
if (typeof canvas === 'string') {
92-
pagView.canvasId = canvas;
93-
}
53+
const pagView = new PAGView(pagPlayer, canvas);
9454
pagView.pagViewOptions = { ...pagView.pagViewOptions, ...initOptions };
95-
await pagView.resetSize(pagView.pagViewOptions.useScale);
96-
pagView.renderCanvas = RenderCanvas.from(canvasElement, { alpha: true });
55+
pagView.resetSize(pagView.pagViewOptions.useScale);
56+
pagView.renderCanvas = RenderCanvas.from(canvas, { alpha: true });
9757
pagView.renderCanvas.retain();
9858
pagView.pagGlContext = BackendContext.from(pagView.renderCanvas.glContext as BackendContext);
9959
pagView.frameRate = file.frameRate();
100-
pagView.pagSurface = this.makePAGSurface(pagView.pagGlContext, canvasElement.width, canvasElement.height);
60+
pagView.pagSurface = this.makePAGSurface(pagView.pagGlContext, canvas.width, canvas.height);
10161
pagView.player.setSurface(pagView.pagSurface);
10262
pagView.player.setComposition(file);
10363
pagView.setProgress(0);
@@ -113,8 +73,6 @@ export class PAGView extends NativePAGView {
11373
useScale: true,
11474
};
11575

116-
private canvasId = '';
117-
11876
/**
11977
* Update size when changed canvas size.
12078
*/
@@ -129,27 +87,7 @@ export class PAGView extends NativePAGView {
12987
this.pagSurface = pagSurface;
13088
}
13189

132-
public getCanvasCssSize(canvasId: string): Promise<CanvasSize> {
133-
return new Promise((resolve) => {
134-
wx.createSelectorQuery()
135-
.select(`#${canvasId}`)
136-
.fields({ node: true, size: true })
137-
.exec((res) => {
138-
resolve({
139-
width: res[0]?.width || 0,
140-
height: res[0]?.height || 0,
141-
});
142-
});
143-
});
144-
}
145-
146-
public async calculateDisplaySize(canvas: any) {
147-
if (this.canvasId !== undefined && this.canvasId !== '') {
148-
const canvasSize = await this.getCanvasCssSize(this.canvasId);
149-
if (canvasSize.width !== 0 && canvasSize.height !== 0) {
150-
return canvasSize;
151-
}
152-
}
90+
public calculateDisplaySize(canvas: any) {
15391
if (canvas.displayWidth === undefined && canvas.displayHeight === undefined) {
15492
canvas.displayWidth = canvas.width;
15593
canvas.displayHeight = canvas.height;
@@ -219,14 +157,14 @@ export class PAGView extends NativePAGView {
219157
}
220158
}
221159

222-
protected override async resetSize(useScale = true) {
160+
protected override resetSize(useScale = true) {
223161
if (!this.canvasElement) {
224162
throw new Error('Canvas element is not found!');
225163
}
226164
if (!useScale || isOffscreenCanvas(this.canvasElement)) {
227165
return;
228166
}
229-
const displaySize = await this.calculateDisplaySize(this.canvasElement);
167+
const displaySize = this.calculateDisplaySize(this.canvasElement);
230168
const dpr = wx.getSystemInfoSync().pixelRatio;
231169
this.canvasElement!.width = displaySize.width * dpr;
232170
this.canvasElement!.height = displaySize.height * dpr;

web/wechat/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ wx.createSelectorQuery()
5353
const canvas = res[0].node;
5454
const buffer = await loadFileByRequest('https://pag.io/file/test.pag');
5555
const pagFile = await this.PAG.PAGFile.load(buffer);
56-
const pagView = await this.PAG.PAGView.init(pagFile, canvas); // 自v4.4.29开始建议传入wxml中定义的canvasId
56+
const pagView = await this.PAG.PAGView.init(pagFile, canvas);
5757
pagView.play();
5858
});
5959

0 commit comments

Comments
 (0)