Skip to content Skip to sidebar Skip to footer

Javascript- Unable To Convert Canvas To Image Data

guys! I'm trying to make a demo using which a user will be able to capture an image from the web camera enabled through his browser and this image will be saved in the backend by p

Solution 1:

Just call this

var img= convertCanvasToImage();

function convertCanvasToImage() 
{
  var image = new Image();
  image.src = canvas.toDataURL("image/png");
  return image;
}

No need to pass canvas as you defined canvas above. But if you have convertCanvasToImage function in another file then you can use

var img= convertCanvasToImage(canvas);

Solution 2:

This may help.

functiondemoFromHTML() {
    html2canvas(document.getElementById("talltweets"), {
        onrendered: function(canvas) {
            var imageData = canvas.toDataURL('image/png',1.0); 

        }
    });
}

Solution 3:

Pass canvas object instead of cann

var img= convertCanvasToImage(canvas);

Solution 4:

If you can try to avoid dataURL and use blob instead - it will save you more memory and load images faster

$canvas.toBlob(function(blob){
  var url = URL.createObjectURL(blob)
  var img = newImage

  img.onload = function() {
    URL.revokeObjecURL(this.src)
  }
  img.src = url
  
  console.log(blob)
  console.log(url)
})
<canvasid="$canvas">

there is polyfills you can use to support canvas#toBlob

Post a Comment for "Javascript- Unable To Convert Canvas To Image Data"