client-compress
client-compress copied to clipboard
Failed to execute 'toBlob' on 'HTMLCanvasElement': Tainted canvases may not be exported
On some browser versions (Chrome/Brave), the compression leads to:
DOMException: Failed to execute 'toBlob' on 'HTMLCanvasElement': Tainted canvases may not be exported.
This is due to a CORS security issue as explained here: https://stackoverflow.com/questions/46984372/todataurl-on-htmlcanvaselement-tainted-canvases-may-not-be-exported
In the code for Photo.js the fix would be to
// Create an object URL which points to the File/Blob image data
const objectUrl = URL.createObjectURL(this.data)
const img = new window.Image()
// Fix CORS error
img.crossOrigin = 'anonymous';
await loadImageElement(img, objectUrl)
Adding
https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image
For a better explanation of the issue.