解决 Konva 中的“Tainted canvases may not be exported”错误
尝试导出 Canvas 时,可能出现如下错误:
Unable to get data URL. Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
Unable to get image data from canvas because the canvas has been tainted by cross-origin data.
应用滤镜时,也可能出现如下错误:
Unable to apply filter. Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.
Unable to apply filter. The operation is insecure.
为什么会出现不安全错误?
这是一个 CORS 错误。出于安全原因,从其他域加载图像时,浏览器可能将 Canvas 标记为已污染。在这种情况下,浏览器会阻止将 Canvas 导出为 dataURL 或 imageData,而导出或使用滤镜时正需要执行此操作。