weil Posted September 13, 2018 Share Posted September 13, 2018 Hi there. I was able to create an image from Phaser.Graphics instance in such way: var graphics = new Phaser.Graphics(this.game, 0, 0); graphics.beginFill(0x90EE90); graphics.drawRect(0, 0, len, len); graphics.endFill(); var img = new Phaser.Image(this.game); img.loadTexture(graphics.generateTexture()); Now I'd like to cache this image so I could easily create sprites using its key. Unfortunately I'm unable to find a way to do so. Is that possible? Link to comment Share on other sites More sharing options...
samme Posted September 13, 2018 Share Posted September 13, 2018 There's a key argument to generateTexture(). Link to comment Share on other sites More sharing options...
weil Posted September 13, 2018 Author Share Posted September 13, 2018 Hmm, I can't find it. I'm using phaser-ce. Signature of `generateTexture` seems to be: `generateTexture( [resolution] [, scaleMode] [, padding])` (https://photonstorm.github.io/phaser-ce/Phaser.Graphics.html). Link to comment Share on other sites More sharing options...
samme Posted September 13, 2018 Share Posted September 13, 2018 You're right, I was thinking of the BitmapData method. You can do game.cache.addItem('name', Phaser.Cache.TEXTURE); // … game.cache.getItem('name', Phaser.Cache.TEXTURE); But I don't think you'd be able to use the string key alone, because that searches the image cache only. Link to comment Share on other sites More sharing options...
Yehuda Katz Posted September 14, 2018 Share Posted September 14, 2018 @weil You can do it indirectly. Create BitmapData with key as @samme thought and draw texture on it. You cannot draw texture but you can draw its canvas. This should work: var bmd = new Phaser.BitmapData(this.game, 'texture_key', graphics.width, graphics.height); bmd.draw(graphics.generateTexture().getCanvas()); Link to comment Share on other sites More sharing options...
Recommended Posts