Jump to content

Get image data from a Pixi canvas


danieldourado_2
 Share

Recommended Posts

Hi. I'm having trouble getting ImageData from a canvas that is being used by Pixi.

I'm sharing the following codepen with an example code that tries to get ImageData from a Pixi canvas, but the data is all zeroes, when the canvas is actually blue:
https://codepen.io/danieldourado/pen/oNGwKRE?editors=1011

const app = new PIXI.Application({
  width: 10,
  height: 10,
  backgroundColor: 0x1099bb,
});
document.body.appendChild(app.view);
const canvas = app.renderer.plugins.extract.canvas();
const context = canvas.getContext('2d');
const imgData = context.getImageData(0, 0, canvas.width, canvas.height);
console.log(imgData.data) //All zeroes, but the canvas is actually blue

 

Edited by danieldourado_2
Link to comment
Share on other sites

  • 2 months later...

Ok, so here is what I found out about how Extract works, and it is confusing sometimes. For example:

I got my previous example to work by adding a graphics to the stage, and passing app.stage as a parameter to extract.canvas():
https://codepen.io/danieldourado/pen/dydedYy?editors=0011

const app = new PIXI.Application({
  width: 10,
  height: 10,
  backgroundColor: 0xcccccc
});
document.body.appendChild(app.view);

const graphics = new PIXI.Graphics();
graphics.beginFill(0xFF0000, 1);
graphics.drawRect(0, 0, 5, 5);
graphics.endFill();
app.stage.addChild(graphics)
const canvas = app.renderer.plugins.extract.canvas(app.stage);


const context = canvas.getContext('2d');
const imgData = context.getImageData(0, 0, canvas.width, canvas.height);
console.log(imgData.data) //Pints pixel data from the 5x5 graphics, but not from the 10x10 canvas background

But the canvas is 10x10, and the graphics is 5x5. I thought the extract.canvas(app.stage) would get the whole 10x10 stage data, but it only gets the 5x5 graphics data.

So I went ahead and found in an example to create a render texture the size of the stage and then extract it:
https://codepen.io/danieldourado/pen/oNGwKRE

 

const app = new PIXI.Application({
  width: 10,
  height: 10,
  backgroundColor: 0xcccccc
});
document.body.appendChild(app.view);

const graphics = new PIXI.Graphics();
graphics.beginFill(0xFF0000, 1);
graphics.drawRect(0, 0, 5, 5);
graphics.endFill();
app.stage.addChild(graphics)

var renderTexture = PIXI.RenderTexture.create({ width: app.screen.width, height: app.screen.height, resolution: app.renderer.resolution });
app.renderer.render(app.stage, renderTexture);
var canvas = app.renderer.plugins.extract.canvas(renderTexture);

const context = canvas.getContext('2d');
const imgData = context.getImageData(0, 0, canvas.width, canvas.height);
console.log(imgData.data) //Pints pixel data from whole 10x10 canvas, but ignores pixel data from the background

Now it does print pixel data from the whole 10x10 canvas, but it still ignores the pixel data from the grey background.

Is there a way for the extract.canvas method to get the pixel data from Pixi's background?

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