Demo and full code like this : https://jsfiddle.net/q93c7Lpf/
It works
It uses document.body.appendChild(img);
to display the image. And the result like this:
<canvas width="660" height="1100" style="width: 600px; height: 1000px;"></canvas>
I want to change it to be tag img. So I want to use file reader.
I read here html image blob to base64 and Convert blob to base64
And then I try implement it
I add this code :
var dataURI;
var reader = new FileReader();
reader.onload = function(){
// here you'll call what to do with the base64 string result
dataURI = this.result;
console.log(dataURI);
};
reader.readAsDataURL(blob);
I add the code after loadImage(...)
, then I run, I see on the console exist error like this:
Uncaught ReferenceError: blob is not defined
Demo and full code like this : https://jsfiddle.net/q93c7Lpf/1/
How can I solve this problem?
canvas-to-blob.min.js
? – Sprayberryblob
doesn't exists (not sure what you wanted it to be). So to fix this, simply move your code in theimg.toBLob
's callback: jsfiddle.net/q93c7Lpf/2 But if what you want is to display the image, then don't use a FileReader at all, simply useURL.createObjectURL
. jsfiddle.net/q93c7Lpf/4 – NofreteteURL.createObjectURL
jsfiddle.net/q93c7Lpf/4 – NofreteteFileReader.readAsDataURL
it should only be used when you want to generate standalones documents. For any other cases, use either the Blob directly, or a blobURI. jsfiddle.net/t6UP5/640 – Nofretete