Masked Sprite to Dynamic Texture
Demonstrates how to apply an external mask to a sprite and draw the resulting masked image into a Dynamic Texture.
800 × 600
class Example extends Phaser.Scene
{
constructor ()
{
super();
}
preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
this.load.image('mask', 'assets/pics/mask-test2.png');
this.load.image('pic', 'assets/pics/hotshot-chaos-in-tokyo.png');
}
create ()
{
const texture = this.textures.addDynamicTexture('maskedPic', 368, 290);
const pic = this.make.image({ key: 'pic', origin: { x: 0, y: 0 }, add: true });
const maskImage = this.make.image({ key: 'mask', origin: { x: 0, y: 0 }, add: false });
pic.enableFilters().filters.external.addMask(maskImage);
texture.draw(pic).render();
this.add.sprite(560, 300, 'maskedPic');
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
parent: 'game-container',
width: 800,
height: 600,
backgroundColor: '#2d2d6d',
scene: Example
};
const game = new Phaser.Game(config);