danieldourado_2 Posted March 31, 2022 Share Posted March 31, 2022 (edited) 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 March 31, 2022 by danieldourado_2 Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted March 31, 2022 Share Posted March 31, 2022 you have to search for "extract" in pixijs issues and discussions. This was discussed many times, and there are some esoteric things about it. Quote Link to comment Share on other sites More sharing options...
danieldourado_2 Posted June 1, 2022 Author Share Posted June 1, 2022 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? munasinghecm 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.