@@ -4,6 +4,7 @@ import { useEffect, useMemo, useRef } from 'react';
44
55import './timeline.less' ;
66import type { ExecutionRecorderItem , ExecutionTask } from '@midscene/core' ;
7+ import { getTextureFromCache , loadTexture } from './pixi-loader' ;
78import { useAllCurrentTasks , useExecutionDump } from './store' ;
89
910interface TimelineItem {
@@ -120,6 +121,7 @@ const TimelineWidget = (props: {
120121 ] ) ;
121122
122123 useEffect ( ( ) => {
124+ let freeFn = ( ) => { } ;
123125 Promise . resolve (
124126 ( async ( ) => {
125127 if ( ! domRef . current ) {
@@ -161,6 +163,9 @@ const TimelineWidget = (props: {
161163 height : canvasHeight ,
162164 backgroundColor : sideBg ,
163165 } ) ;
166+ freeFn = ( ) => {
167+ app . destroy ( ) ;
168+ } ;
164169 if ( ! domRef . current ) {
165170 app . destroy ( ) ;
166171 return ;
@@ -226,44 +231,50 @@ const TimelineWidget = (props: {
226231 const container = new PIXI . Container ( ) ;
227232 shotContainers . push ( container ) ;
228233 app . stage . addChild ( container ) ;
229- const img = new Image ( ) ;
230- img . src = screenshot . img ;
231- img . onload = ( ) => {
232- const screenshotTexture = PIXI . Texture . from ( img ) ;
233- const screenshotSprite = new PIXI . Sprite ( screenshotTexture ) ;
234-
235- // get width / height of img
236- const originalWidth = img . width ;
237- const originalHeight = img . height ;
238-
239- const screenshotHeight = screenshotMaxHeight ;
240- const screenshotWidth = Math . floor (
241- ( screenshotHeight / originalHeight ) * originalWidth ,
242- ) ;
234+ Promise . resolve (
235+ ( async ( ) => {
236+ await loadTexture ( screenshot . img ) ;
237+ const texture = getTextureFromCache ( screenshot . img ) ;
238+ if ( ! texture ) {
239+ return ;
240+ }
241+
242+ // clone the sprite
243+ const screenshotSprite = PIXI . Sprite . from ( texture ) ;
244+
245+ // get width / height of img
246+ const originalWidth = screenshotSprite . width ;
247+ const originalHeight = screenshotSprite . height ;
248+
249+ const screenshotHeight = screenshotMaxHeight ;
250+ const screenshotWidth = Math . floor (
251+ ( screenshotHeight / originalHeight ) * originalWidth ,
252+ ) ;
243253
244- const screenshotX = leftForTimeOffset ( screenshot . timeOffset ) ;
245- allScreenshots [ index ] . x = screenshotX ;
246- allScreenshots [ index ] . y = screenshotTop ;
247- allScreenshots [ index ] . width = screenshotWidth ;
248- allScreenshots [ index ] . height = screenshotMaxHeight ;
249-
250- const border = new PIXI . Graphics ( ) ;
251- border . lineStyle ( sizeRatio , shotBorderColor , 1 ) ;
252- border . drawRect (
253- screenshotX ,
254- screenshotTop ,
255- screenshotWidth ,
256- screenshotMaxHeight ,
257- ) ;
258- border . endFill ( ) ;
259- container . addChild ( border ) ;
260-
261- screenshotSprite . x = screenshotX ;
262- screenshotSprite . y = screenshotTop ;
263- screenshotSprite . width = screenshotWidth ;
264- screenshotSprite . height = screenshotMaxHeight ;
265- container . addChild ( screenshotSprite ) ;
266- } ;
254+ const screenshotX = leftForTimeOffset ( screenshot . timeOffset ) ;
255+ allScreenshots [ index ] . x = screenshotX ;
256+ allScreenshots [ index ] . y = screenshotTop ;
257+ allScreenshots [ index ] . width = screenshotWidth ;
258+ allScreenshots [ index ] . height = screenshotMaxHeight ;
259+
260+ const border = new PIXI . Graphics ( ) ;
261+ border . lineStyle ( sizeRatio , shotBorderColor , 1 ) ;
262+ border . drawRect (
263+ screenshotX ,
264+ screenshotTop ,
265+ screenshotWidth ,
266+ screenshotMaxHeight ,
267+ ) ;
268+ border . endFill ( ) ;
269+ container . addChild ( border ) ;
270+
271+ screenshotSprite . x = screenshotX ;
272+ screenshotSprite . y = screenshotTop ;
273+ screenshotSprite . width = screenshotWidth ;
274+ screenshotSprite . height = screenshotMaxHeight ;
275+ container . addChild ( screenshotSprite ) ;
276+ } ) ( ) ,
277+ ) ;
267278 } ) ;
268279
269280 const highlightMaskUpdater = (
@@ -422,6 +433,10 @@ const TimelineWidget = (props: {
422433 canvas . addEventListener ( 'pointerdown' , onPointerTap ) ;
423434 } ) ( ) ,
424435 ) ;
436+
437+ return ( ) => {
438+ freeFn ( ) ;
439+ } ;
425440 } , [ ] ) ;
426441
427442 return < div className = "timeline-canvas-wrapper" ref = { domRef } /> ;
0 commit comments