Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

299
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda