I am struggling to integrate a file uploaded via the input type "file" into my Fabric JS canvas.
The steps of the process are as follows:
- User initiates action by pressing a button (calls onAddImage)
- User selects an image to upload
- Selected image is added to the canvas using Fabric JS
This is the TypeScript code I have implemented so far:
const onAddImage = () => {
document.getElementById("image-file")?.click(); //Trigger image file upload
const fileInput = document.getElementById( // Retrieve file input
"image-file"
) as HTMLInputElement | null;
// Use file?.files?.[0] here in an attempt to access the image
if (fileInput) {
fabric.Image.fromURL(<local image PATH supposed to be HERE>, function (img) { // Image path should be inserted here
editor?.canvas.add(img);
});
}};
How can I pass the image from the input type file to the function as a string, especially when the image does not exist in the local directory? Do I need to cache the image somehow? Additionally, FabricJS offers two other functions (fromObject and fromElement) that may be relevant, but I'm unsure if they are necessary in this case.