Snapshot Feature Crashes After Second Use (v3.80)

I’m working on a special effect that requires post-processing of a segment of the screen. The perfect tool for this would be the renderer.snapshotArea function. So I followed the sample:

https://phaser.io/examples/v3.85.0/snapshot/view/snapshot-area

Seemed to work, until I modified the sample to add an image to the scene based on the snapshotted texture:

        this.input.on('pointerdown', (pointer) => {
            const textureManager = this.textures;

            this.game.renderer.snapshotArea(pointer.x, pointer.y, 128, 128, (image) => {
                document.body.appendChild(image);
                if (textureManager.exists('area')) {
                    textureManager.remove('area');
                }

                textureManager.addImage('area', image);
                this.add.image(0, 0, 'area');
            });
        });

This code crashes the second time it calls this callback. It works the first click, and fails the second click, giving the following error message:

phaser.min.js:1 Uncaught TypeError: Cannot read properties of null (reading ‘glTexture’)
at initialize.get [as glTexture] (phaser.min.js:1:1032467)
at initialize.batchSprite (phaser.min.js:1:867108)
at initialize.renderWebGL (phaser.min.js:1:247628)
at initialize.render (phaser.min.js:1:846334)
at initialize.render (phaser.min.js:1:48511)
at initialize.render (phaser.min.js:1:967090)
at initialize.render (phaser.min.js:1:954248)
at initialize.step (phaser.min.js:1:76227)
at initialize.step (phaser.min.js:1:80333)
at e (phaser.min.js:1:141058)

Am I doing something wrong? Or is this a bug in the engine? Any help would be appreciated, thanks!

Hi, you also need to destroy this game object when you remove the “area” texture, or you need to switch its texture to the new one.

1 Like

That worked perfectly. Thank you! I hope they change the snapshot code example to reflect this. As it stands, if you use their code but just comment back in the line:

// particles.setTexture('area');

, you’ll get the crash I was getting after the second click, presumably for the same reason.