phaser icon indicating copy to clipboard operation
phaser copied to clipboard

Cache as bitmap offsets sprite in group when using canvas renderer

Open mkristo opened this issue 11 years ago • 6 comments

When setting cacheAsBitmap to true on a group the contained sprite will get offset. This didn't happen in Phaser 2.3 and not when using WebGL.

To reproduce, use the below code or visit http://codepen.io/anon/pen/XbPBVm

<html>
<head>
<script src="https://cdn.rawgit.com/photonstorm/phaser/b6ad27ed24d94168a74636a6dc3678cdea5d9059/build/custom/phaser-no-physics.js"></script>
<script>
window.onload = function () {
    var preload = function () {
        this.game.load.crossOrigin = true;
        this.game.load.image('logo', 'https://cdn.rawgit.com/photonstorm/phaser/340635370bfad7bb174114639a89b03a919ce3fc/resources/Project%20Templates/Webpack/assets/phaser.png');
    };

    var create = function () {
        var mySprite = this.game.make.sprite(50, 50, "logo");
        mySprite.position.x = 50;
        mySprite.position.y = 50;

        var myGroup = this.game.add.group();
        myGroup.add(mySprite);

        setInterval(function () {
            myGroup.cacheAsBitmap = !myGroup.cacheAsBitmap;
        }, 1000);
    };

    var game = new Phaser.Game(1024, 768, Phaser.CANVAS, '', {
        preload: preload,
        create: create
    });
}
</script>
</head>
<body></body>
</html>

mkristo avatar Jul 22 '15 14:07 mkristo

Could you please test this against the dev branch. I've been working on a fix for it in there, and my tests are now all passing (including your test code above) but I'd like if you could confirm please.

photonstorm avatar Aug 03 '15 11:08 photonstorm

After my tests I can confirm that positioning works as expected. However, I'm seeing that transparency isn't handled correctly for cached sprites. I'll update the code for repro in a bit.

mkristo avatar Aug 03 '15 15:08 mkristo

I had to fork and rebuild Phaser to get your latest changes to the test case (https://github.com/mkristo/phaser/commit/665e120655bd6cd1f2d50f6ced7ac32ed3fa8b42)

To reproduce, use the below code or visit http://codepen.io/anon/pen/JdweMQ

<html>
<head>
<script src="https://cdn.rawgit.com/mkristo/phaser/665e120655bd6cd1f2d50f6ced7ac32ed3fa8b42/build/custom/phaser-no-physics.js"></script>
<script>
window.onload = function () {
    var preload = function () {
        this.game.load.crossOrigin = true;
        this.game.load.image('logo', 'http://phaser.io/images/img.png');
    };

    var create = function () {
        var mySprite = this.game.make.sprite(50, 50, "logo");
        mySprite.position.x = 50;
        mySprite.position.y = 50;

        var myGroup = this.game.add.group();
        myGroup.add(mySprite);
        myGroup.cacheAsBitmap = true;

        setInterval(function () {
            myGroup.updateCache();
        }, 1000);
    };

    var game = new Phaser.Game(1024, 768, Phaser.CANVAS, '', {
        preload: preload,
        create: create
    });
}
</script>
</head>
<body></body>
</html>

Notice the transparent edges of the Phaser logo get brighter and brighter.

mkristo avatar Aug 03 '15 15:08 mkristo

Please give this a go - it should be fine now as the texture is cleared before render.

photonstorm avatar Aug 06 '15 00:08 photonstorm

Yes, it works great! Thanks!

mkristo avatar Aug 06 '15 07:08 mkristo

I see the same problem when adding Sprite or Text as a child of Sprite (I haven't tested other containers - if the same code is used, then those might need a change too). Positioning when using cacheAsBitmap is incorrect. I've changed the mkristo's example to show this.

http://codepen.io/anon/pen/mAggON

TadejZupancic avatar Oct 28 '16 09:10 TadejZupancic