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.

canvas.toDataURL() returns image with width 4 times and height 2 times bigger

See original GitHub issue

Hello. Thank you for this awesome package.

I’m testing getting image data from canvas and i found that image size returned after canvas.toDataURL() call is 4 times in width and 2 times in height bigger than canvas width and height. Could you point me what i’m doing wrong please or tell me if it’s a bug. My code looks the following way:

handleCanvas(canvas) {
       this.canvas = canvas;
        canvas.width = 200;
        canvas.height = 300;
        const context = canvas.getContext('2d');
        const image = new CanvasImage(canvas);
            image.addEventListener('load', () => {
                    context.drawImage(
                        image,
                        0,
                        0
                    );
            });
            image.src = `data:image/jpeg;base64,${this.imageData}`;
        });
    }

...

this.canvas.toDataURL('image/jpeg')
     .then((data) => {
          // By the way here data is wrapped in quotes by some reason, so we need to remove them
          data = data.substring(1);
          data = data.slice(0, -1);

          if (data.indexOf('data:image/jpeg;base64,') > -1) {
               // Removing "data:image/jpeg;base64," for saving into file as base64 data
                data = data.substring(23);
          }
          RNFetchBlob.fs.writeFile(this.path, data, 'base64')
               .then(() => {
                    // Image data saved and has 200x4 pixels width and 300x2 pixels height
               })
     }

For saving image react-native-fetch-blob package is used

Thanks in advance

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Dec 1, 2017

Ok, i researched the sources and found reason. The problem is that autoScale function is called twice - one time when we set width for canvas and second time when we set height. I fixed locally by removing this.autoScale() call from width and height setters and made this function public by adding it in Canvas.js:

(0, _webviewBinders.webviewMethods)(['toDataURL', 'autoScale'])

Then i set width and height of canvas in the following way:

this.canvas.width = 600;
this.canvas.height = 800;
this.canvas.autoScale();
0reactions
iddancommented, Oct 6, 2018

Please open a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

2
toDataURL it returns "data:;" . When the canvas.width / height is more reasonable, the result is correct. Is there any good way to...
Read more >
Capturing an image from an HTML5 Canvas or Video ...
Here's where the magic takes place. I create a method that captures the element like this: let image = canvas.toDataURL('image/jpeg');.
Read more >
Pixel manipulation with canvas - Web APIs | MDN
The ImageData object represents the underlying pixel data of an area of a canvas object. It contains the following read-only attributes: width.
Read more >
HTML canvas getImageData() Method
The getImageData() method returns an ImageData object that copies the pixel data for the specified rectangle on a canvas. Note: The ImageData object...
Read more >
HTML5 Canvas to Data URL Tutorial
How to export canvas content into image?To get the data URL of the stage with Konva, we can use the toDataURL()method which requires...
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