I am having an undefined value when I tried to convert my canvas to a blob. I have this code below which works fine, but when I tried to move the console log below to the function then it gives me undefined.
Here is the working code:
const handleSave = (params) => {
let previewUrl;
previewCanvasRef.current.toBlob(
(blob) => {
previewUrl = window.URL.createObjectURL(blob);
console.log(previewUrl);
},
'image/png',
1
);
}
when I tried to make the console log below. then It gives me an undefiend value:
const handleSave = (params) => {
let previewUrl;
previewCanvasRef.current.toBlob(
(blob) => {
previewUrl = window.URL.createObjectURL(blob);
},
'image/png',
1
);
console.log(previewUrl);
}
Here is what I've tried but error:
const handleSave = (params) => {
let previewUrl;
previewCanvasRef.current.toBlob(
async (blob) => {
previewUrl = window.URL.createObjectURL(blob);
},
'image/png',
1
).then((res) => console.log(res));
}