Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

298
Views
Canvas, problem with exporting image using javascript

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!