简体   繁体   中英

Pass headers in fabric Image.fromURL function

I am using fabric.js Image.fromURL function to load the image to canvas. For now, the images are loading successfully without any issues. But now I have a requirement to pass headers for authentication purposes is there a way that I could pass headers into the "Image.fromURL" function or any other way I could incorporate my headers to it.

Image.fromUrl doesn't send a request. As you can see here in source code this function uses fabric.util.loadImage , that as you can see here creates an image and set src attribute. Thus the browser makes the request for getting the image itself. And you cannot send your own headers using Image.FromURL function

I think you can get your images something like this (it's just idea):

const image = new Image();
image.onload = () => {
  const fabricImage = new fabric.Image(image);
  canvas.add(fabricImage);
}

fetch("https://your-url", {headers: {}}).then((res)=>{
  image.src = res;
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM