Jump to content

How to take screenshot of current state of canvas. I'm using pixi.js library for my game.


Abhishek Kargawal
 Share

Recommended Posts

I'm facing problem in getting screenshot of current state of canvas, which is get rendered dynamically using pixi.js library.

Below written code i'm using to generate canvas and when i use toDataURL() function on dynamically generated canvas it always returned black image.

  /* PIXI Renderer object of the game. */
  var renderer = PIXI.autoDetectRenderer(FIELD_SQUARE_SIZE_PIXELS, FIELD_SQUARE_SIZE_PIXELS, { backgroundColor: 0xF2F2F2 });  

  /* This code i'm using to change color of button on canvas. Which work fine.*/

 var whiteButtonsArr = whiteButtons.graphicsData;

    for (i = 0; i < whiteButtonsArr.length; i++) {
      var button = whiteButtonsArr;

        button.fillColor = WHITE_BUTTON_FILL_COLOR;
        whiteButtons.dirty = true;
        whiteButtons.clearDirty = true;
        return true;

    }

After some analysis i have changed the code to render canvas (added preserveDrawingBuffer attribute )to get current screenshot of canvas. After adding preserveDrawingBuffer attribute i'm able to get the current state of canvas using toDataURL() function. But button's color is not getting changed on UI. Although while debugging button.fillColor pick the color but doesn't reflect on UI.

/* PIXI Renderer object of the game. */
  var renderer = PIXI.autoDetectRenderer(FIELD_SQUARE_SIZE_PIXELS, FIELD_SQUARE_SIZE_PIXELS, { backgroundColor: 0xF2F2F2 },
{ preserveDrawingBuffer: true });  

Please let me know if any other approach or way is available to take screenshot. Thanks in advance.

Link to comment
Share on other sites

Also, you gave two "options" arguments to autoDetectRenderer. That will be correct.

 

var renderer = PIXI.autoDetectRenderer(FIELD_SQUARE_SIZE_PIXELS, FIELD_SQUARE_SIZE_PIXELS, { backgroundColor: 0xF2F2F2 ,  preserveDrawingBuffer: true });  

So, you have two ways:
1. preserveDrawingBuffer - slows down rendering process
2.`PIXI.extract` - uses readPixels(), doesn't affect rendering process

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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