In the canvas element, it uses the following code to draw pictures.
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d')
let width = canvas.width
let height = canvas.height
ctx.clearRect(0, 0, width, height)
ctx.canvas.width = width
ctx.canvas.height = height
....
let image = new Image
//image.crossOrigin = 'anonymous'
image.src = url
ctx.drawImage(image, x, y)
Everything works fine as long as I don't want to export the resulting artwork. I try to do it like this:
let canvas = document.getElementById('canvas')
var link = document.createElement('a')
let imgUrl = canvas.toDataURL()
link.download = 'graphics.jpg'
link.href = imgUrl
link.click()
Then I receives the following error in the console:
Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
When looking for a solution to this problem, I came across the information that before adding graphics to the canvas, I need to add a line that I commented at the top, namely:
image.crossOrigin = 'anonymous'
The problem is that then these graphics won't load because then it's blocked by cors policy:
Access to image at ... from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
The graphics are taken from the backend written in laravel. How can js export canvas data without changing anything on the backend? If this is not possible, how to change the cors policy in laravel so that it does not block downloading images from the storage folder.