KKitForma.

Language

EnglishEnglishTürkçeTurkishGuide unavailable in this language · open toolsDeutschGermanGuide unavailable in this language · open toolsEspañolSpanishGuide unavailable in this language · open toolsFrançaisFrenchGuide unavailable in this language · open toolsPortuguêsPortugueseGuide unavailable in this language · open toolsItalianoItalianGuide unavailable in this language · open toolsNederlandsDutchGuide unavailable in this language · open toolsPolskiPolishGuide unavailable in this language · open toolsРусскийRussianGuide unavailable in this language · open toolsУкраїнськаUkrainianGuide unavailable in this language · open toolsSvenskaSwedishGuide unavailable in this language · open toolsNorskNorwegianGuide unavailable in this language · open toolsDanskDanishGuide unavailable in this language · open toolsSuomiFinnishGuide unavailable in this language · open toolsČeštinaCzechGuide unavailable in this language · open toolsRomânăRomanianGuide unavailable in this language · open toolsΕλληνικάGreekGuide unavailable in this language · open toolsالعربيةArabicGuide unavailable in this language · open toolsעבריתHebrewGuide unavailable in this language · open toolsفارسیPersianGuide unavailable in this language · open toolsاردوUrduGuide unavailable in this language · open toolsहिन्दीHindiGuide unavailable in this language · open toolsবাংলাBengaliGuide unavailable in this language · open toolsதமிழ்TamilGuide unavailable in this language · open toolsతెలుగుTeluguGuide unavailable in this language · open toolsमराठीMarathiGuide unavailable in this language · open toolsગુજરાતીGujaratiGuide unavailable in this language · open tools简体中文Chinese SimplifiedGuide unavailable in this language · open tools繁體中文Chinese TraditionalGuide unavailable in this language · open tools日本語JapaneseGuide unavailable in this language · open tools한국어KoreanGuide unavailable in this language · open toolsTiếng ViệtVietnameseGuide unavailable in this language · open toolsไทยThaiGuide unavailable in this language · open toolsBahasa IndonesiaIndonesianGuide unavailable in this language · open toolsBahasa MelayuMalayGuide unavailable in this language · open toolsFilipinoFilipinoGuide unavailable in this language · open toolsKiswahiliSwahiliGuide unavailable in this language · open toolsAfrikaansAfrikaansGuide unavailable in this language · open toolsMagyarHungarianGuide unavailable in this language · open toolsБългарскиBulgarianGuide unavailable in this language · open toolsHrvatskiCroatianGuide unavailable in this language · open toolsSrpskiSerbianGuide unavailable in this language · open toolsSlovenčinaSlovakGuide unavailable in this language · open toolsSlovenščinaSlovenianGuide unavailable in this language · open toolsLietuviųLithuanianGuide unavailable in this language · open toolsLatviešuLatvianGuide unavailable in this language · open toolsEestiEstonianGuide unavailable in this language · open toolsCatalàCatalanGuide unavailable in this language · open toolsEuskaraBasqueGuide unavailable in this language · open tools
← Questions & answers

KITFORMA · EDITORIAL ANSWER

Why does canvas export throw after drawing a cross-origin image?

KitForma editorial guide. Separate successful image display from permission to read canvas pixels, and set CORS mode before starting the image request.

HTML, CSS & accessibility →

Step-by-step answer

A cross-origin image can display normally yet taint a canvas when drawn. Set crossOrigin before src and make the image server return compatible CORS headers. Run this local-blob smoke test in a browser console; it needs no remote image. Runnable example:
javascript
async function exportPng(url) {
  const image = new Image();
  image.crossOrigin = "anonymous";
  image.src = url;
  await image.decode();
  const canvas = document.createElement("canvas");
  canvas.width = image.naturalWidth;
  canvas.height = image.naturalHeight;
  canvas.getContext("2d").drawImage(image, 0, 0);
  return new Promise((resolve, reject) => canvas.toBlob(
    blob => blob ? resolve(blob) : reject(new Error("PNG export failed")),
    "image/png"));
}
const svg = '<svg xmlns="http://www.w3.org/2000/svg" width="2" height="1"><rect width="2" height="1" fill="red"/></svg>';
const url = URL.createObjectURL(new Blob([svg], { type: "image/svg+xml" }));
try {
  const png = await exportPng(url);
  console.log(png.type, png.size > 0);
} finally {
  URL.revokeObjectURL(url);
}
Expected result: image/png true Why it works: The local blob confirms that drawing and PNG export work with an authorized source. To test remote behavior, substitute an image URL you control. Anonymous requests need Access-Control-Allow-Origin allowing the page’s origin (or * for this credentialless case); setting crossOrigin alone cannot grant permission. Scope and checks: A modern browser Window console with top-level await. The local test does not verify your remote server. Inspect redirects and the final image response. Without compatible CORS, an anonymous image load can fail; drawing a cross-origin image loaded without CORS can instead make export throw SecurityError. Use an authorized source if the server policy cannot be changed. Related tools and guides: Convert a local image file: https://kitforma.com/en/convert/ Preview URL cleanup: https://kitforma.com/en/community/questions/7aa10000-0000-4000-8000-000000003900/ Primary sources (reviewed 2026-10-08): WHATWG HTML — canvas origin-clean security: https://html.spec.whatwg.org/multipage/canvas.html#security-with-canvas-elements WHATWG Fetch — CORS response headers: https://fetch.spec.whatwg.org/#http-responses

Sources and verification

Sources checked:

This editorial answer was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.

Discussion and community answers

Loading community…
KitForma

What would you like to do?