Jump to content

Fast pixel setting on image/bitmap


Recommended Posts

Hey guys, I'm wondering if there's a faster way to set pixels on an image. This seems to work OK but rendering is something that I'd really like to speed up if possible.

Also, if I eventually only set pixels that changed, maybe I shouldn't be using putImageData to copy all pixels each frame?

Any ideas or advice are welcome.

    for (var x = 0; x < this.map.width; x++)    {        for (var y = 0; y < this.map.height; y++)        {            var cell = this.map.cells[x][y];                        var a = 255;            var r = cell.r;            var g = cell.g;            var b = cell.b;                        this.bmd.setPixel32(x,y, r, g, b, a, immediate=false);        }    }    this.bmd.context.putImageData(this.bmd.imageData, 0, 0);

Thanks very much,


Link to comment
Share on other sites

If your bmd is added as the texture of a Sprite , it should update itself, no need to manually redraw everything. (Might need to set .dirty=true)


Thanks for the reply. I tried with both an image and a sprite - the dirty flag doesn't cause a redraw at all.

this.bmd.dirty = true; //placed instead of this.bmd.context.putImageData(this.bmd.imageData, 0, 0);

And yes , I assume your map doesn't change that often. , so only update parts of the texture as needed

Potentially a large portion could change in some frames, others little. So I need something that best suits both. - I guess by only updating those parts is just done automatically by only using setPixel32 on the pixels that changed, and phaser will handle the rest?


Thanks again

Link to comment
Share on other sites

hmm your method seems to be the way to do it and it's not fast




this is worth a look though. maybe copyRect is faster. i wonder if you could copyRect from a colour palette texture with 1x1 colours on it





edit: no that's slower ...

context.drawImage: http://phaser.io/sandbox/Wbkstglt/play


also look at these


rect: http://phaser.io/sandbox/rmiJgxCD/play


processPixelRGB: http://phaser.io/sandbox/GOmiEhpp/play

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...