question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

putImageData is not drawing anything in the canvas

See original GitHub issue

canvasContext.putImageData(data,0,0) not drawing anything

I’m trying to pass an ImageData element to the canvas so it’s displayed on it, my code looks like:

import Canvas, {ImageData} from 'react-native-canvas';

...

const handleCanvas = async canvas => {
    canvas.width = sizes.width;
    canvas.height = sizes.height;
    const ctx = canvas.getContext('2d');

    const data = await ctx.createImageData(
      encodedImage.width,
      encodedImage.height,
      encodedImage.data,
    );

    console.log(
      'data',
      data,
      data instanceof ImageData,
      'encoded',
      encodedImage,
      encodedImage instanceof ImageData,
    );

    await ctx.putImageData(data, 0, 0);
  };

...

{encodedImage && (
            <Canvas
              ref={handleCanvas}
              style={{borderWidth: 1, borderColor: 'red', zIndex: 1000000}}
            />
)}

Outputs:

Screenshot 2020-04-23 at 12 09 49

encodedImage is a black and white ImageData mask from https://www.npmjs.com/package/@tensorflow-models/body-pix#returns-4

Canvas is that red box:

Screenshot 2020-04-23 at 12 27 56

Nothing is painted inside when using putImageData with Outputs data.

If I try to pass the ImageData to another canvas context I get the following error: Error: Argument 1(‘imagedata’) to CanvasRenderingContext2D.putImageData must be an instance of ImageData

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
QuevedoIBcommented, Apr 23, 2020

My workaround is to initialize with new ImageData(…) and then add the properties to the instance.

 const handleCanvas = async canvas => {
    canvas.width = sizes.width;
    canvas.height = sizes.height;
    const ctx = canvas.getContext('2d');

    const data = new ImageData(canvas);

    data.data = encodedImage.data;
    data.width = encodedImage.width;
    data.height = encodedImage.height;

    console.log(data, data instanceof ImageData);

    await ctx.putImageData(data, 0, 0);
  };
Screenshot 2020-04-23 at 14 15 56

That way I get the desired ImageData instance with the proper data, but putImageData still does nothing to the canvas. (Nothing is drawn) Did someone get putImageData working before?

Screenshot 2020-04-23 at 14 17 18
0reactions
bbhopeshcommented, May 14, 2020

I am also facing bug with putDataImage. When I run example app without any changes, it renders differently on different platforms. Check top left rectangle in two images below, one is from an emulator and one is from real phone. However, both are rendering wrong. If code was behaving correctly, entire top left rectangle would have been black. image

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does Canvas's putImageData not work when I specify ...
The height to use to draw the image on the canvas. As Loktar states above, the CORRECT synopsis is as follows: Correct Synopsis:...
Read more >
CanvasRenderingContext2D.putImageData() - Web APIs | MDN
The CanvasRenderingContext2D.putImageData() method of the Canvas 2D API paints data from the given ImageData object onto the canvas.
Read more >
HTML canvas putImageData() Method - W3Schools
The putImageData() method puts the image data (from a specified ImageData object) back onto the canvas. ... The width to use to draw...
Read more >
Canvas putImageData does not work with GPU Accelerated ...
Issue 68495 : Canvas putImageData does not work with GPU Accelerated Canvas ... when canvas A was not visible, it would not draw...
Read more >
Ejecta getImageData() and putImageData() Problem - ImpactJS
For some reason, putImageData will not display the image regularly. ... The thing with drawing though, is that you have to do it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found