@@ -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
4439export 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 ;
0 commit comments