在React里面使用pixi.js 写出下面的代码就可以创建出一个全屏的pixi.js画布 const PixiCanvas = () => { const pixiContainerRef = useRef ( null ); useEffect (() => { const app = new PIXI . Application ({ resizeTo : window }) pixiContainerRef . current . appendChild ( app . view ); return () => { app . destroy ( true ); } }, []); return < div ref = { pixiContainerRef } /> ; } 加入一个container, 将背景图片挂载到这个container上面,然后加到场景pixijs app里面 useEffect (() => { const app = new PIXI . Application ({ resizeTo : window }); const container = new PIXI . Container (); // background image const backgroundTexture = PIXI . Texture . from ( backgroundImage ); const backgroundSprite = new PIXI . Sprite ( backgroundTexture ); const resizeBackground = () => { backgroundSprite . width = window . innerWidth ; backgroundSprite . height = window . inner...
对于异步函数, 简单的try-catch并不能直接捕捉到。 比如说 function func1 () { try { func2() } catch (error) { } } function func2() { setTimeout(function () { throw new Error("error") }, 1000) } 这里就无法catch到func2里面throw的error 所以应该是func2返回一个封装在Promise里面的函数,在func1里面用await来接收。因为await是对一个表达式求值。如果await后面是promise的话,就会等待Promise返回才执行。