Reference · Codes, URLs and SVG helpers
toDataUrifunction
A flag's SVG as a data: URI, ready for an <img src>, a CSS background-image: url(…) or a canvas. Only
the characters that must be escaped are, so it stays readable and close to the SVG's own size (smaller than
base64).
Import it
import { toDataUri } from "@johnmorrisdotca/hata";
Definition
function toDataUri(svg: string): string | null
Parameters
| Name | Type | Description |
|---|---|---|
svg | string | A flag's SVG string, framed or not. |
Returns
string | null
data:image/svg+xml,…, or null when the string is not an SVG.
Examples
import { toDataUri } from "@johnmorrisdotca/hata";
import japan from "@johnmorrisdotca/hata/flags/jp";
const img = document.createElement("img");
img.src = toDataUri(japan) ?? "";
img.alt = "Japan";
console.log(img);