在Pixi 6中,载入提供的窗口大小会导致错误。解决这个问题的方法是,通过手动设置画布大小来避免这个问题。我们可以在Angular组件中的ngOnInit()生命周期钩子中,手动获取画布元素并设置宽度和高度。示例代码如下:
import { Component, OnInit } from '@angular/core'; import * as PIXI from 'pixi.js';
@Component({ selector: 'app-pixi-canvas', templateUrl: './pixi-canvas.component.html', styleUrls: ['./pixi-canvas.component.css'] }) export class PixiCanvasComponent implements OnInit { app: PIXI.Application; constructor() { }
ngOnInit(): void { let canvas = document.getElementById("my-canvas") as HTMLCanvasElement; canvas.width = window.innerWidth; canvas.height = window.innerHeight; this.app = new PIXI.Application({ view: canvas, width: canvas.width, height: canvas.height }); // Add your Pixi elements here } }
在HTML模板中,我们可以简单地添加一个画布元素,示例代码如下:
这样,当Pixi应用程序初始化时,画布元素的大小将被正确设置,从而避免了初始化错误。