canvas.toDataURL() returns image with width 4 times and height 2 times bigger
See original GitHub issueHello. 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:
- Created 6 years ago
- Reactions:1
- Comments:12 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
Please open a new issue.