You need to enable JavaScript to run this app. 如何在Vue.js组件中使用Pixi.js?复现官方示例仅显示背景无内容如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vue.js组件中使用Pixi.js?复现官方示例仅显示背景无内容如何解决?

问题排查与修复方案

核心问题梳理

  • 静态资源路径无法被构建工具正确解析,导致图片纹理加载失败,是仅显示背景无内容的主要原因
  • 存在不规范的DOM结构写法与实例挂载逻辑,可能引发额外渲染异常

具体修复步骤

1. 修复静态资源引入方式

Vue的构建工具不会处理JS代码字符串内的相对资源路径,需要先通过import导入图片资源,再传入PIXI创建纹理:

// 脚本开头新增导入
import blobImg from './assets/blob.png'
// 创建纹理时改为使用导入的资源
const texture = PIXI.Texture.from(blobImg);

2. 清理非法DOM结构

删除template中的<body>标签,Vue单文件组件不允许在模板中定义body节点,修改后的template如下:

<template>
  <div id='change'>    
      <router-link class="changepage" to='/OrdreCouches'> >Retour </router-link>    
  </div>
  <div id="pixi"></div>
</template>

3. 规范PIXI实例挂载

将PIXI的canvas挂载到模板预留的#pixi容器中,避免挂载到body下被其他元素遮挡:

// 替换document.body.appendChild(app.view)
document.getElementById('pixi').appendChild(app.view)

4. 清理冗余代码

删除data中无用的PIXI实例初始化,避免重复创建实例浪费内存:

// 修改data部分,删除无用的初始化
data(){
  return{
    app: null
  }
},
// init方法中创建实例后赋值给this.app,方便后续调用
this.app = new PIXI.Application({
  width: 800, height: 600, backgroundColor: 0x1099bb, resolution: window.devicePixelRatio || 1,
});

5. 增加异常排查逻辑

添加纹理加载错误监听,快速定位资源加载问题:

const texture = PIXI.Texture.from(blobImg);
texture.baseTexture.on('error', () => {
  console.error('图片纹理加载失败,请检查资源路径是否正确')
})

完整修复后代码

<template>
  <div id='change'>    
      <router-link class="changepage" to='/OrdreCouches'> >Retour </router-link>    
  </div>
  <div id="pixi"></div>
</template>

<script>
import * as PIXI from 'pixi.js'
import blobImg from './assets/blob.png'

export default {
  name: 'Visualisation',
  data(){
    return{
      app: null
    }
  },
  methods: {
    init: function() {
      this.app = new PIXI.Application({
          width: 800, height: 600, backgroundColor: 0x1099bb, resolution: window.devicePixelRatio || 1,
      });
      document.getElementById('pixi').appendChild(this.app.view);

      const container = new PIXI.Container();
      this.app.stage.addChild(container);

      // 创建纹理并添加错误监听
      const texture = PIXI.Texture.from(blobImg);
      texture.baseTexture.on('error', () => {
        console.error('纹理加载失败,请检查资源路径')
      })

      // 创建5x5网格精灵
      for (let i = 0; i < 25; i++) {
          const bunny = new PIXI.Sprite(texture);
          bunny.anchor.set(0.5);
          bunny.x = (i % 5) * 40;
          bunny.y = Math.floor(i / 5) * 40;
          container.addChild(bunny);
      }

      // 居中容器
      container.x = this.app.screen.width / 2;
      container.y = this.app.screen.height / 2;
      container.pivot.x = container.width / 2;
      container.pivot.y = container.height / 2;

      // 帧动画监听
      this.app.ticker.add((delta) => {
          container.rotation -= 0.01 * delta;
      });
    }
  },
  mounted() {
      this.init();
  },
  // 组件销毁时销毁PIXI实例,避免内存泄漏
  beforeUnmount() {
    if(this.app) {
      this.app.destroy(true)
    }
  }
}
</script>

<style scoped>
.changepage{
  position: absolute;
  color: rgb(0, 0, 0);
  font-size: 30px;
}
.changepage:hover{
  color: rgb(53, 20, 199);
  font-size: 30px;
}
#pixi {
  margin-top: 50px;
}
</style>

额外补充:建议给style加scoped属性,避免组件样式污染全局,新增了beforeUnmount生命周期销毁PIXI实例,避免内存泄漏。

内容的提问来源于stack exchange,提问作者Lorine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 10:45:04