A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/en-US/docs/Web/API/SVGImageElement/decode below:

SVGImageElement: decode() method - Web APIs

SVGImageElement: decode() method

Baseline 2025

Newly available

The decode() method of the SVGImageElement interface initiates asynchronous decoding of an image, returning a Promise that resolves once the image is decoded and it is safe to append it to the DOM.

Syntax Parameters

None.

Return value

A Promise that fulfills with undefined once the image data is ready to be used, for example by appending it to the DOM, replacing an existing image, and so forth.

Exceptions

None.

Examples

In the below example, you'll likely get an empty image shown on the page as the image is downloaded:

const SVG_NS = "http://www.w3.org/2000/svg";
const svg = document.querySelector("svg");

const img = document.createElementNS(SVG_NS, "image");
img.src = "img/logo.svg";
svg.appendChild(img);

Using decode() will delay inserting the image into the DOM until it is fully downloaded and decoded, thereby avoiding the empty image problem:

const SVG_NS = "http://www.w3.org/2000/svg";
const svg = document.querySelector("svg");

async function getImage() {
  const img = document.createElementNS(SVG_NS, "image");
  img.src = "img/logo.svg";
  await img.decode();
  svg.appendChild(img);
  const text = document.createElementNS(SVG_NS, "text");
  text.textContent = "Image is fully loaded!";
  svg.appendChild(text);
}

This is particularly useful if you're dynamically swapping an existing image for a new one, and also prevents unrelated paints outside of this code from being held up while the image is decoding.

Specifications Browser compatibility See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3