I want to save my canvas data to file using the Tauri fs module.
This is my code, but it didn't work.
const { writeBinaryFile, BaseDirectory } = window.__TAURI__.fs;
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var grd = ctx.createLinearGradient(0, 0, 200, 0);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);
async function IMAGE() {
await writeBinaryFile('avatar2.png', canvas.toDataURL(), { dir: BaseDirectory.Document });
}
IMAGE()
I have tried to convert DataUrl to other formats, I don't know if it will help or not.