The warning "WARNING: Too many active WebGL contexts. Oldest context will be lost." occurs because your application is creating multiple WebGL contexts without properly destroying them. Browsers have a limit on the number of active WebGL contexts (typically 16), and when this limit is exceeded, they start discarding the oldest contexts, which can lead to unexpected behavior in your application.
In your Vue.js component, the onMounted hook is used to initialize the Pixi.js application and create the WebGL context. However, you're not properly disposing of the Pixi.js application when the component is unmounted or re-rendered. This means that each time the component is mounted (e.g., due to navigation, hot module reloading, or re-rendering), a new WebGL context is created, and the old one remains in memory.
To fix this issue, you need to ensure that the Pixi.js application is properly destroyed when the component is unmounted. This can be achieved by using the onBeforeUnmount (or onUnmounted) lifecycle hook in Vue.js to clean up the Pixi.js application and release the WebGL context.
Here's how you can modify your code:
- Declare the Pixi.js application outside of the
onMounted hook so it can be accessed in other lifecycle hooks.
- Add the
onBeforeUnmount hook to properly destroy the Pixi.js application and release resources when the component is unmounted.
Below is the corrected code with these changes:
<script setup lang="ts">
import { onMounted, onBeforeUnmount, ref, reactive, computed, watch, Ref } from 'vue'
import { Pixihttp } from '../systems/http.ts'
import {
Spritesheet,
AnimatedSprite,
Texture,
Application,
Sprite,
Assets,
} from 'pixi.js'
import * as PIXI from 'pixi.js'
import liuxinPath from '@/assets/liuxin/liuxin.png'
import liuxinPathJS from '@/assets/liuxin/liuxin.json'
import housePath from '@/assets/house/house.png'
import housePathGIF from '@/assets/house/house.gif'
import housePathJS from '@/assets/house/house.json'
// Existing state variables
const checked_type = ref('1')
const model_id = ref('')
const canvasdomref = ref(null)
const selectedlist = ref<Array<any>>([])
// Reactive arrays
const stable_diffusion_xls = reactive<Array<any>>([])
const fluxs = reactive<Array<any>>([])
const selected = ref<Array<any>>([])
const msgtext = ref('Hello')
// Pagination state
const currentPage = ref(1)
const itemsPerPage = 50
// Watchers and computed properties (same as before)
// Gallery state
const images = reactive<Array<string>>([])
const galleryCurrentPage = ref(1)
const galleryItemsPerPage = 12
const code_input_ = ref('')
const ondraw = () => {
console.log(code_input_.value)
}
// Text object for Pixi.js
const text = new PIXI.Text(msgtext.value, {
fontFamily: 'Arial',
fontSize: 50,
fill: 0xff1010,
})
watch(msgtext, (newVal) => {
if (text) {
text.text = newVal
}
})
// Computed properties and watchers (same as before)
const dataLoaded = ref(false)
// Declare the Pixi.js application outside of the onMounted hook
let app: Application
onMounted(async () => {
try {
const canvas = canvasdomref.value
let width = window.screen.width
let height = window.screen.height
if (width > height) {
console.log('横屏')
} else {
console.log('竖屏')
}
console.log('屏幕长宽:', width, height)
// Initialize the Pixi.js application
app = new Application({
resizeTo: window,
backgroundAlpha: 0,
})
// Load assets and set up sprites (same as before)
// Append the Pixi.js view (canvas) to the DOM
// @ts-ignore
canvas?.appendChild(app.view)
// Fetch model list and prepare data (same as before)
dataLoaded.value = true
} catch (error) {
console.error('Failed to fetch model list:', error)
}
})
// Add the onBeforeUnmount hook to clean up the Pixi.js application
onBeforeUnmount(() => {
if (app) {
app.destroy(true, { children: true, texture: true, baseTexture: true })
app = null // Set to null to allow garbage collection
}
})
</script>
<template>
<!-- Your template remains the same -->
</template>
<style scoped>
/* Your styles remain the same */
</style>
Explanation of Changes:
-
Declared let app: Application outside of the onMounted hook: This allows you to access the app instance in other lifecycle hooks like onBeforeUnmount.
-
Used onBeforeUnmount to clean up the Pixi.js application:
- Called
app.destroy(true, { children: true, texture: true, baseTexture: true }) to destroy the application and release all associated resources, including the WebGL context.
- Set
app to null afterward to ensure it's garbage collected.
-
Adjusted Pixi.js Application Initialization:
- Changed to use
new Application({ options }) directly instead of calling app.init(), which is not a standard method in Pixi.js.
- Appended
app.view (the canvas element) to the DOM rather than app.canvas.
These changes ensure that the Pixi.js application is properly destroyed when the component is unmounted, preventing the accumulation of WebGL contexts and eliminating the warning about too many active contexts.
Additional Notes:
-
Ensure Correct Usage of Pixi.js Methods:
- Make sure that you're using the correct methods and properties from Pixi.js. For example,
app.view is the canvas element you should append to the DOM, not app.canvas.
- Verify that your usage of
PIXI.Text and other classes aligns with the Pixi.js documentation for the version you're using.
-
Check for Other Potential Memory Leaks:
- If you're dynamically loading assets or creating other resources, ensure they're properly disposed of when no longer needed.
-
Hot Module Replacement (HMR) in Development:
- If you're using HMR during development, components may be reloaded without a full page refresh, which can cause
onMounted to be called multiple times without unmounting the component. This is a common source of accumulated WebGL contexts during development.
- Consider adding checks to prevent re-initializing the Pixi.js application if it already exists during development.
By properly managing the lifecycle of your Pixi.js application within the Vue component, you should eliminate the warning and ensure efficient resource usage in your application.